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:
1 parent
0214ae1537
commit
c48406788a
2 files changed
+54
-2
No files matched your search
+28
-2
@@ -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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { inflateFlate } from '@/pdf/attachment'
|
||||
|
||||
async function deflate(data: Uint8Array): Promise<Uint8Array> {
|
||||
const stream = new Blob([data as BlobPart])
|
||||
.stream()
|
||||
.pipeThrough(new CompressionStream('deflate'))
|
||||
return new Uint8Array(await new Response(stream).arrayBuffer())
|
||||
}
|
||||
|
||||
describe('inflateFlate decompression cap', () => {
|
||||
it('inflates a normal-size payload', async () => {
|
||||
const payload = new TextEncoder().encode(JSON.stringify({ version: 2, hello: 'world' }))
|
||||
const out = await inflateFlate(await deflate(payload))
|
||||
expect(out).not.toBeNull()
|
||||
expect(new TextDecoder().decode(out!)).toContain('hello')
|
||||
})
|
||||
|
||||
it('returns null for a decompression bomb above the cap', async () => {
|
||||
// 24 MB of zeros deflates to a few KB but inflates past the 16 MB cap
|
||||
const bomb = await deflate(new Uint8Array(24 * 1024 * 1024))
|
||||
expect(bomb.byteLength).toBeLessThan(1024 * 1024)
|
||||
const out = await inflateFlate(bomb)
|
||||
expect(out).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user