Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/server/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const FILE_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-

export function sanitizeFileName(name: string): string {
const base = name.split(/[\\/]/).at(-1) ?? "document.pdf";
// Slice code points, not UTF-16 units, so a surrogate pair is never split in half.
const cleaned = Array.from(base)
.filter((character) => character.charCodeAt(0) >= 32 && character.charCodeAt(0) !== 127)
.join("")
.slice(0, 180);
.slice(0, 180)
.join("");
return cleaned || "document.pdf";
}

Expand Down
5 changes: 5 additions & 0 deletions tests/resource-ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ test("file names never collapse to an empty display name", () => {
const long = sanitizeFileName(`${"a".repeat(300)}.pdf`);
assert.ok(long.length <= 180);
assert.ok(long.length > 0);
// The 180-character bound slices whole code points, never half a surrogate pair.
const emoji = sanitizeFileName(`${"x".repeat(179)}😀.pdf`);
assert.equal(emoji, `${"x".repeat(179)}😀`);
assert.equal([...emoji].length, 180);
assert.equal([...sanitizeFileName(`${"x".repeat(180)}😀.pdf`)].length, 180);
});

test("file lookups reject malformed IDs instead of probing storage", async (t) => {
Expand Down
Loading