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, }); });