"use strict"; const test = require("node:test"); const assert = require("node:assert"); const { inferTranscriptionFilename, sanitizeTranscriptionFilename, } = require("./dashboard-assistant-service"); // The transcription upload filename is client-controlled end to end: multer // 2.4.0 decodes %0D/%0A/%22 back into CR/LF/quotes in originalname, and npm // form-data emits the Groq part header as `filename="..."` with no escaping. // Whatever comes out of these helpers must therefore never re-introduce // multipart structure: no CRLF, no quotes, no other ASCII controls — while // leaving legitimate non-ASCII/Unicode filenames byte-identical. test("CRLF injected via the upload filename is stripped outright", () => { assert.strictEqual(inferTranscriptionFilename("report\r\n.wav", "audio/wav"), "report.wav"); assert.strictEqual( inferTranscriptionFilename("leak\r\nContent-Disposition: form-data; name=\"model\"", "audio/wav"), "leakContent-Disposition: form-data; name='model'" ); }); test("double quotes are replaced with single quotes, not deleted", () => { // Deleting the quote would fuse tokens ("report".wav" → "report.wav" hides // the tamper); mapping to `'` keeps the separation visible. assert.strictEqual(sanitizeTranscriptionFilename('report".wav'), "report'.wav"); assert.strictEqual(inferTranscriptionFilename('report".wav', "audio/wav"), "report'.wav"); }); test("other ASCII control characters (NUL, ESC, DEL) are stripped", () => { assert.strictEqual(inferTranscriptionFilename("re\x00po\x1brt\x7f.wav", "audio/wav"), "report.wav"); assert.strictEqual(sanitizeTranscriptionFilename("a\x0Bb\x0Cc\x1Fd"), "abcd"); }); test("empty filename still yields the static fallback per mime", () => { assert.strictEqual(inferTranscriptionFilename("", "audio/wav"), "dashboard-assistant.wav"); assert.strictEqual(inferTranscriptionFilename("", "audio/ogg"), "dashboard-assistant.ogg"); assert.strictEqual(inferTranscriptionFilename("", "audio/webm"), "dashboard-assistant.webm"); assert.strictEqual(inferTranscriptionFilename("", "audio/mp4"), "dashboard-assistant.m4a"); assert.strictEqual(inferTranscriptionFilename("", "audio/mpeg"), "dashboard-assistant.mp3"); assert.strictEqual(inferTranscriptionFilename(null, "audio/x-unknown"), "dashboard-assistant.webm"); // A filename made ONLY of controls collapses to empty after sanitizing, so // it must fall back instead of returning a control-only name. assert.strictEqual(inferTranscriptionFilename("\r\n\x00\x1b", "audio/wav"), "dashboard-assistant.wav"); }); test("a Unicode filename passes through unchanged", () => { assert.strictEqual(inferTranscriptionFilename("recording-éü.wav", "audio/wav"), "recording-éü.wav"); assert.strictEqual(inferTranscriptionFilename("录音-日本語.wav", "audio/wav"), "录音-日本語.wav"); assert.strictEqual(sanitizeTranscriptionFilename("résumé-\u00e9\u00fc.wav"), "résumé-éü.wav"); }); test("sanitized output contains no CR, LF, quote, or any char below 0x20", () => { // A battery of multipart-forgery-shaped payloads (the classic second-part // smuggling attempt through the filename header). const hostile = [ 'a.wav\r\n--BOUNDARY\r\nContent-Disposition: form-data; name="model"\r\n\r\nopenai/gpt-oss-120b\r\n--BOUNDARY--', '"\x00\x01\x02\x1b\x7f"\r\n', "\n\r\n\r\t\x0b\x0c", 'x"\x08"y', ]; for (const payload of hostile) { for (const produced of [ sanitizeTranscriptionFilename(payload), inferTranscriptionFilename(payload, "audio/webm"), ]) { for (const char of produced) { const code = char.charCodeAt(0); assert.ok( code !== 0x0a && code !== 0x0d && code !== 0x22 && code >= 0x20, `output must not contain CR/LF/quote/controls, got U+${code.toString(16).padStart(4, "0")} in ${JSON.stringify(produced)}` ); } } } }); test("sanitizing is sanitize-before-trim: stripped edge controls leave no stray space", () => { // Controls at the edges must vanish before the trim, not after — else // " \r\nreport.wav\r\n " would come back as " report.wav " with padding. assert.strictEqual(inferTranscriptionFilename(" report.wav \r\n", "audio/wav"), "report.wav"); assert.strictEqual(inferTranscriptionFilename("\r\n\x00report.wav\x7f ", "audio/wav"), "report.wav"); }); test("the sanitizer is idempotent (double application at the Groq emit site is safe)", () => { const samples = [ 're"\x00port\r\n.wav', "recording-éü.wav", 'a"; name="model".wav', ]; for (const sample of samples) { assert.strictEqual( sanitizeTranscriptionFilename(sanitizeTranscriptionFilename(sample)), sanitizeTranscriptionFilename(sample) ); } });