diff --git a/TODO.md b/TODO.md index 01646ee..2b7f631 100644 --- a/TODO.md +++ b/TODO.md @@ -1,14 +1,5 @@ # 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 - Resume interrupted transfers: the receiver already reports how much the download manager has diff --git a/src/core.test.ts b/src/core.test.ts index 68d67d4..0858868 100644 --- a/src/core.test.ts +++ b/src/core.test.ts @@ -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 () => { const { channel, session, handshake, download } = await downloading(3 * CHUNK_SIZE); expect(handshake.start).toHaveBeenCalled(); diff --git a/src/core.ts b/src/core.ts index 58e1597..d5e1317 100644 --- a/src/core.ts +++ b/src/core.ts @@ -62,6 +62,10 @@ export const PBKDF2_ITERATIONS = 600_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. 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 = { local: string; @@ -779,10 +783,10 @@ export function receiveFile( if (typeof name !== 'string' || name.length > MAX_NAME || !Number.isSafeInteger(size) || size < 0) { return handshake.end('the sender offered no valid file'); } - meta = { name, size }; - events.log(`offered ${name} (${size} bytes)`); + meta = { name: name.replace(HIDDEN, ''), size }; + events.log(`offered ${meta.name} (${size} bytes)`); events.status('wait', 'Click Download to save the file'); - events.incoming({ name, size, download }); + events.incoming({ ...meta, download }); } else if (downloading && !complete) { received += data.byteLength; if (received > meta!.size) return fail('the sender sent more than it offered');