palico-bot/test/downloadAudio.test.js
Shinuwa 3795ee06cb Add file headers, tidy the stylesheet and document the conventions
Headers:
- Every JavaScript file (sources, front, scripts, tests) starts with a
  /** @file … */ header, in the three levels used by sokko-g: a short
  description for simple files; Utilisé par / Attention / @see for
  shared modules; a full header (context, entry points, callers,
  dependencies, pitfalls) for index.js, the music queue, the web
  server, the storage manager and the jukebox page script.
- No @author, @version or @since: git already knows.
- test/fileHeaders.test.js fails `npm run check` when a header is
  missing or a central file's header is incomplete.

Stylesheet (no visual change, checked by comparing every element's
computed styles in Chromium at 1280, 800 and 400 px, hover included):
- Role header and section comments.
- Transparent shades derive from the colour variables with
  color-mix() instead of copied rgba() values; new tokens for repeated
  values (text, text on accent, accent gradient, white veils, shadow).
- Native nesting for buttons, the live indicator, upload rows and
  home cards; drop the unused .hero__visual--large and redundant rules.
- Utility classes move to the end so they win over components. The
  library list's u-mt-1, which never applied (.list reset it), is
  removed to keep the layout as it was.

Docs: CONTRIBUTING.md describes the language, header and stylesheet
conventions; CLAUDE.md points to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 21:05:59 +01:00

119 lines
3.9 KiB
JavaScript

/**
* @file Vérifie le téléchargement par lien avec un serveur local : nom du fichier, adresses internes
* refusées (même après une redirection), taille maximale et protocoles acceptés.
*/
import test from "node:test";
import assert from "node:assert/strict";
import dns from "node:dns";
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import { downloadAudio, isBlockedAddress } from "../src/media/downloadAudio.js";
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-download-"));
test.after(() => fs.rmSync(directory, { recursive: true, force: true }));
// Le serveur de test écoute en local, adresse que le téléchargeur refuse : les tests qui
// doivent l'atteindre passent une résolution DNS sans filtre.
const allowLocal = (hostname, options, callback) => dns.lookup(hostname, options, callback);
async function withServer(handler, run) {
const server = http.createServer(handler);
server.listen(0, "127.0.0.1");
await new Promise((resolve) => server.once("listening", resolve));
try {
await run(`http://localhost:${server.address().port}`);
} finally {
await new Promise((resolve) => server.close(resolve));
}
}
test("internal and special addresses are blocked", () => {
for (const address of ["127.0.0.1", "10.1.2.3", "192.168.0.100", "172.20.0.1", "169.254.169.254", "100.64.0.1", "::1", "fd00::1", "fe80::1", "::ffff:10.0.0.1"]) {
assert.equal(isBlockedAddress(address), true, address);
}
for (const address of ["1.1.1.1", "151.101.130.132", "2606:4700::1111"]) {
assert.equal(isBlockedAddress(address), false, address);
}
});
test("a direct link is downloaded with its file name", async () => {
await withServer(
(req, res) => {
res.writeHead(200, {
"content-type": "audio/mpeg",
"content-disposition": "attachment; filename=\"chanson.mp3\"",
});
res.end("fake audio");
},
async (base) => {
const file = await downloadAudio(`${base}/download?id=1`, {
destinationDir: directory,
lookup: allowLocal,
});
assert.equal(file.originalname, "chanson.mp3");
assert.equal(file.mimetype, "audio/mpeg");
assert.equal(file.size, 10);
assert.equal(fs.readFileSync(file.path, "utf8"), "fake audio");
}
);
});
test("local addresses are refused, written as an IP or behind a host name", async () => {
await assert.rejects(downloadAudio("http://127.0.0.1:1/a.mp3", { destinationDir: directory }), {
status: 400,
});
await withServer(
(req, res) => res.end("secret"),
async (base) => {
await assert.rejects(downloadAudio(`${base}/a.mp3`, { destinationDir: directory }), {
message: /non autorisée/,
});
}
);
});
test("a redirect towards an internal address is refused", async () => {
await withServer(
(req, res) => {
res.writeHead(302, { location: "http://192.168.0.100/admin" });
res.end();
},
async (base) => {
await assert.rejects(
downloadAudio(`${base}/a.mp3`, { destinationDir: directory, lookup: allowLocal }),
{ message: /non autorisée/ }
);
}
);
});
test("files over the size limit are refused and removed", async () => {
await withServer(
(req, res) => {
res.writeHead(200);
res.write(Buffer.alloc(600));
res.end(Buffer.alloc(600));
},
async (base) => {
const before = fs.readdirSync(directory).length;
await assert.rejects(
downloadAudio(`${base}/big.mp3`, {
destinationDir: directory,
lookup: allowLocal,
maxSize: 1000,
}),
{ status: 413 }
);
assert.equal(fs.readdirSync(directory).length, before);
}
);
});
test("only http and https links are accepted", async () => {
await assert.rejects(downloadAudio("file:///etc/passwd", { destinationDir: directory }), {
status: 400,
});
});