Files
Kelx/tests/upload-filename-parity.test.js
2026-11-30 16:32:37 +00:00

55 lines
2.1 KiB
JavaScript

"use strict";
const { test } = require("node:test");
const assert = require("node:assert");
const { sanitizeUploadFilename } = require("../utils/upload-filename");
// Pins the shared JS mirror of the daemon's Go sanitizeUploadFilename
// (main.go). Two bug classes are locked out here:
// 1. The audit-era disagreement: the panel stripped dotfiles/non-ASCII
// while the daemon preserved them, so .env uploaded as env.
// 2. The trim divergence: Go strings.TrimSpace strips U+0085 (NEL) but JS
// String.trim() does not, so "evil.bat\u0085" once passed the extension
// blocklist (extname saw ".bat\u0085") and the daemon trimmed it to
// "evil.bat" on disk.
test("sanitizeUploadFilename mirrors the daemon semantics", () => {
const cases = [
[".env", ".env"],
["café.txt", "café.txt"],
["a/b.txt", "b.txt"],
["a\\b.txt", "a_b.txt"],
["..", null],
[".", null],
["/", null],
["", null],
[null, null],
["x\0x", "x_x"],
["\x1b[31mred", "_[31mred"],
["sp ace", "sp ace"],
[" .env\t", ".env"],
// NEL edges are trimmed like Go TrimSpace so the extension policy sees
// the true extension (blocklist bypass regression test).
["evil.bat\u0085", "evil.bat"],
["x\u0085", "x"],
["\u0085", null],
// U+FEFF edges: JS trim strips them, Go TrimSpace keeps them — the panel
// being stricter is the safe direction; pin it so a future edit doesn't
// silently flip it.
["x\uFEFF", "x"],
["\uFEFF.env", ".env"],
["🎮.zip", "🎮.zip"],
["a\"b.txt", "a\"b.txt"],
];
for (const [input, expected] of cases) {
assert.strictEqual(sanitizeUploadFilename(input), expected, `input ${JSON.stringify(input)}`);
}
});
test("sanitizeUploadFilename caps at 200 bytes on a code-point boundary", () => {
const cjk = "世".repeat(80); // 240 bytes
const out = sanitizeUploadFilename(cjk);
assert.ok(Buffer.byteLength(out) <= 200, `capped output ${Buffer.byteLength(out)} bytes`);
assert.strictEqual(out, "世".repeat(66)); // 198 bytes; whole runes kept
assert.strictEqual(sanitizeUploadFilename("a".repeat(500)), "a".repeat(200));
});