Strip controls, line separators and bidi controls from offered file names

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-30 00:38:10 +01:00
1 parent 7f4811485a
commit af0a8c9fee
3 files changed
+18 -12

No files matched your search

-9
View File
@@ -1,14 +1,5 @@
# TODO # TODO
## Hardening
From a security review, none of them urgent.
- Sanitize file names for display: `MAX_NAME` only limits the length. Control and bidi characters
(e.g. U+202E) can fake log lines or disguise an extension, `invoice‮fdp.exe` shows as
`invoiceexe.pdf`. Strip C0/C1 and bidi controls where the name is shown and logged, maybe for
`Content-Disposition` in `sw.js` too, though browsers sanitize download names themselves.
## Ideas ## Ideas
- Resume interrupted transfers: the receiver already reports how much the download manager has - Resume interrupted transfers: the receiver already reports how much the download manager has
+11
View File
@@ -1142,6 +1142,17 @@ describe('receiving', () => {
} }
}); });
it('strips controls, line separators and bidi controls from the offered name', async () => {
const { channel, session, check } = receiving();
check();
await until(() => channel.sent.length);
channel.receive({ name: 'invoice\u202Efdp.exe\n\u2028done\u0000\u009B\u2066', size: 1 });
expect(session.files).toMatchObject([{ name: 'invoicefdp.exedone' }]);
expect(session.logs.at(-1)).toBe('offered invoicefdp.exedone (1 bytes)');
session.files[0]!.download();
expect(saved.at(-1)!.file.name).toBe('invoicefdp.exedone');
});
it("saves the chunks in order, reports the download manager's reads and when it finished", async () => { it("saves the chunks in order, reports the download manager's reads and when it finished", async () => {
const { channel, session, handshake, download } = await downloading(3 * CHUNK_SIZE); const { channel, session, handshake, download } = await downloading(3 * CHUNK_SIZE);
expect(handshake.start).toHaveBeenCalled(); expect(handshake.start).toHaveBeenCalled();
+7 -3
View File
@@ -62,6 +62,10 @@ export const PBKDF2_ITERATIONS = 600_000;
export const PROOF_TIMEOUT = 10_000; export const PROOF_TIMEOUT = 10_000;
// Far beyond what a file system stores, but keeps an absurd name out of the page and the worker. // Far beyond what a file system stores, but keeps an absurd name out of the page and the worker.
export const MAX_NAME = 1024; export const MAX_NAME = 1024;
// Controls and line separators in an offered name could fake log lines, bidi controls disguise its
// extension: invoice\u202Efdp.exe shows as invoiceexe.pdf. They go before the name is shown, logged
// or saved.
const HIDDEN = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/gu;
export type Check = { export type Check = {
local: string; local: string;
@@ -779,10 +783,10 @@ export function receiveFile(
if (typeof name !== 'string' || name.length > MAX_NAME || !Number.isSafeInteger(size) || size < 0) { if (typeof name !== 'string' || name.length > MAX_NAME || !Number.isSafeInteger(size) || size < 0) {
return handshake.end('the sender offered no valid file'); return handshake.end('the sender offered no valid file');
} }
meta = { name, size }; meta = { name: name.replace(HIDDEN, ''), size };
events.log(`offered ${name} (${size} bytes)`); events.log(`offered ${meta.name} (${size} bytes)`);
events.status('wait', 'Click Download to save the file'); events.status('wait', 'Click Download to save the file');
events.incoming({ name, size, download }); events.incoming({ ...meta, download });
} else if (downloading && !complete) { } else if (downloading && !complete) {
received += data.byteLength; received += data.byteLength;
if (received > meta!.size) return fail('the sender sent more than it offered'); if (received > meta!.size) return fail('the sender sent more than it offered');