fix(pdf): cap inflated attachment size on import

SEC-8 (user-approved change to the protected import path): the flate
fallback materialized the whole decompressed stream unbounded — a
crafted PDF with a deflate bomb could freeze the tab. Stream is now
read chunk-wise and cancelled past 16 MB (valid report JSON is orders
of magnitude smaller); bomb → null → import fails with the generic
error. TDD: real CompressionStream bomb fixture, 180/180 green.
This commit is contained in:
Bastien Chanot committed 2026-07-12 16:27:40 +02:00
1 parent 0214ae1537
commit c48406788a
2 files changed
+54 -2

No files matched your search

+28 -2
View File
@@ -61,11 +61,37 @@ interface RawObj {
dict?: { entries?: () => Array<[unknown, unknown]> }
}
async function inflateFlate(bytes: Uint8Array): Promise<Uint8Array | null> {
/**
* Hard ceiling on inflated attachment size. A valid report JSON is far
* smaller; anything bigger is a decompression bomb or corruption.
*/
const MAX_INFLATED_BYTES = 16 * 1024 * 1024
/** Exported for tests. Returns null on failure or when the cap is exceeded. */
export async function inflateFlate(bytes: Uint8Array): Promise<Uint8Array | null> {
if (typeof DecompressionStream === 'undefined') return null
try {
const stream = new Blob([bytes as BlobPart]).stream().pipeThrough(new DecompressionStream('deflate'))
return new Uint8Array(await new Response(stream).arrayBuffer())
const reader = stream.getReader()
const chunks: Uint8Array[] = []
let total = 0
for (;;) {
const { done, value } = await reader.read()
if (done) break
total += value.byteLength
if (total > MAX_INFLATED_BYTES) {
await reader.cancel()
return null
}
chunks.push(value)
}
const out = new Uint8Array(total)
let offset = 0
for (const c of chunks) {
out.set(c, offset)
offset += c.byteLength
}
return out
} catch {
return null
}