fix(pdf): inflater fed in 16 KiB slices (browser memory bound), shared 16 MiB candidate budget, monotonic bounded endstream search, JSON object pre-check
This commit is contained in:
1 parent
6adc38b027
commit
3a6e92625a
6 files changed
+310
-32
No files matched your search
@@ -4,6 +4,19 @@
|
||||
*/
|
||||
export const MAX_ATTACHMENT_BYTES = 16 * 1024 * 1024
|
||||
|
||||
/**
|
||||
* Compressed bytes handed to the inflater per write. Chromium's
|
||||
* `DecompressionStream` inflates everything it has been given before the
|
||||
* consumer reads it, so a stream written whole lets a bomb inflate gigabytes
|
||||
* before `reader.cancel()` lands (measured: +2.2 GB RSS on a 4 MB / 4 GiB
|
||||
* bomb). One awaited slice at a time keeps that read-ahead to what 16 KiB
|
||||
* can inflate (about 16 MiB).
|
||||
*/
|
||||
const WRITE_SLICE_BYTES = 16 * 1024
|
||||
|
||||
type Writer = WritableStreamDefaultWriter<Uint8Array<ArrayBuffer>>
|
||||
type Reader = ReadableStreamDefaultReader<Uint8Array>
|
||||
|
||||
function concat(chunks: Uint8Array[], total: number): Uint8Array {
|
||||
const out = new Uint8Array(total)
|
||||
let offset = 0
|
||||
@@ -14,35 +27,56 @@ function concat(chunks: Uint8Array[], total: number): Uint8Array {
|
||||
return out
|
||||
}
|
||||
|
||||
/** Feeds the inflater one slice per awaited write, then closes it. */
|
||||
async function feedSlices(writer: Writer, bytes: Uint8Array): Promise<void> {
|
||||
// The cast compiles under both the engine (node) and the app (DOM) libs.
|
||||
const source = bytes as Uint8Array<ArrayBuffer>
|
||||
for (let at = 0; at < source.byteLength; at += WRITE_SLICE_BYTES) {
|
||||
await writer.write(source.subarray(at, at + WRITE_SLICE_BYTES))
|
||||
}
|
||||
await writer.close()
|
||||
}
|
||||
|
||||
/** Collects the inflated chunks; null as soon as their total exceeds `limit`. */
|
||||
async function drainUnder(
|
||||
reader: Reader,
|
||||
limit: number,
|
||||
): Promise<Uint8Array | null> {
|
||||
const chunks: Uint8Array[] = []
|
||||
let total = 0
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) return concat(chunks, total)
|
||||
total += value.byteLength
|
||||
if (total > limit) return null
|
||||
chunks.push(value)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FlateDecode (zlib framing) under `limit`, the attachment cap by default.
|
||||
* Null on any failure (truncated or corrupt stream, trailing junk, bad
|
||||
* checksum) or as soon as the output exceeds the limit.
|
||||
* checksum) or as soon as the output exceeds the limit. The feed and the
|
||||
* drain run concurrently; over the limit both sides are torn down, which
|
||||
* also rejects the feed's pending write.
|
||||
*/
|
||||
export async function inflateFlate(
|
||||
bytes: Uint8Array,
|
||||
limit: number = MAX_ATTACHMENT_BYTES,
|
||||
): Promise<Uint8Array | null> {
|
||||
if (typeof DecompressionStream === 'undefined') return null
|
||||
const inflater = new DecompressionStream('deflate')
|
||||
const writer = inflater.writable.getWriter()
|
||||
const reader = inflater.readable.getReader()
|
||||
// A stream error surfaces on the drain; the feed's own rejection is noise.
|
||||
const fed = feedSlices(writer, bytes)
|
||||
fed.catch(() => undefined)
|
||||
try {
|
||||
// The cast compiles under both the engine (node) and the app (DOM) libs.
|
||||
const stream = new Blob([bytes as Uint8Array<ArrayBuffer>])
|
||||
.stream()
|
||||
.pipeThrough(new DecompressionStream('deflate'))
|
||||
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 > limit) {
|
||||
await reader.cancel()
|
||||
return null
|
||||
}
|
||||
chunks.push(value)
|
||||
const out = await drainUnder(reader, limit)
|
||||
if (out === null) {
|
||||
await Promise.allSettled([reader.cancel(), writer.abort()])
|
||||
}
|
||||
return concat(chunks, total)
|
||||
return out
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -253,13 +253,44 @@ function declaredLength(
|
||||
return found === tail ? length : null
|
||||
}
|
||||
|
||||
/** Farthest `endstream` worth finding: a longer candidate is refused anyway. */
|
||||
const MEASURE_WINDOW = MAX_ATTACHMENT_BYTES + 16
|
||||
|
||||
/** Index of the first `endstream` at or after `offset`, within MEASURE_WINDOW; -1 when none. */
|
||||
type EndstreamFinder = (offset: number) => number
|
||||
|
||||
/**
|
||||
* Searches `endstream` once over the file instead of once per candidate:
|
||||
* candidates come in offset order, so the last hit serves every offset
|
||||
* before it and a miss clears the bytes it covered for the next search. An
|
||||
* offset out of order (never produced today) just restarts the search.
|
||||
*/
|
||||
function endstreamFinder(bytes: Uint8Array, end: number): EndstreamFinder {
|
||||
let lastHit = -1
|
||||
let clearedTo = 0
|
||||
let lastOffset = 0
|
||||
return (offset) => {
|
||||
if (offset < lastOffset) {
|
||||
lastHit = -1
|
||||
clearedTo = 0
|
||||
}
|
||||
lastOffset = offset
|
||||
if (lastHit >= offset) return lastHit
|
||||
const limit = Math.min(end, offset + MEASURE_WINDOW)
|
||||
const at = indexOf(bytes, ENDSTREAM, Math.max(offset, clearedTo), limit)
|
||||
if (at >= 0) lastHit = at
|
||||
else clearedTo = Math.max(clearedTo, limit - ENDSTREAM.length + 1)
|
||||
return at
|
||||
}
|
||||
}
|
||||
|
||||
/** Fallback for an indirect or wrong `/Length`: up to `endstream`, the EOL before it dropped. */
|
||||
function measuredLength(
|
||||
bytes: Uint8Array,
|
||||
offset: number,
|
||||
end: number,
|
||||
findEndstream: EndstreamFinder,
|
||||
): number | null {
|
||||
const at = indexOf(bytes, ENDSTREAM, offset, end)
|
||||
const at = findEndstream(offset)
|
||||
if (at < 0) return null
|
||||
let stop = at
|
||||
if (stop > offset && bytes[stop - 1] === 0x0a) stop -= 1
|
||||
@@ -275,12 +306,13 @@ function candidateOf(
|
||||
bytes: Uint8Array,
|
||||
dict: DictSpan,
|
||||
end: number,
|
||||
findEndstream: EndstreamFinder,
|
||||
): Omit<EmbeddedFileCandidate, 'name'> | null {
|
||||
const offset = streamDataOffset(bytes, dict.end, end)
|
||||
if (offset < 0) return null
|
||||
const length =
|
||||
declaredLength(bytes, dict.text, offset, end) ??
|
||||
measuredLength(bytes, offset, end)
|
||||
measuredLength(bytes, offset, findEndstream)
|
||||
if (length === null) return null
|
||||
return { offset, length, filter: filterOf(dict.text) }
|
||||
}
|
||||
@@ -365,11 +397,12 @@ export function hasPdfHeader(bytes: Uint8Array): boolean {
|
||||
export function findEmbeddedFiles(bytes: Uint8Array): EmbeddedFileCandidate[] {
|
||||
const end = Math.min(bytes.byteLength, MAX_PDF_BYTES)
|
||||
const names = filespecNames(bytes, end)
|
||||
const findEndstream = endstreamFinder(bytes, end)
|
||||
const found: EmbeddedFileCandidate[] = []
|
||||
for (const pos of findTypedDicts(bytes, EMBEDDED_FILE, end)) {
|
||||
const dict = dictAround(bytes, pos, end)
|
||||
if (!dict) continue
|
||||
const candidate = candidateOf(bytes, dict, end)
|
||||
const candidate = candidateOf(bytes, dict, end, findEndstream)
|
||||
if (!candidate) continue
|
||||
const name =
|
||||
dict.objectNumber === null ? null : (names.get(dict.objectNumber) ?? null)
|
||||
@@ -380,18 +413,31 @@ export function findEmbeddedFiles(bytes: Uint8Array): EmbeddedFileCandidate[] {
|
||||
return found.sort((a, b) => named(a) - named(b))
|
||||
}
|
||||
|
||||
/** Raw or inflated stream data under the attachment cap; null when unusable. */
|
||||
/** Raw or inflated stream data under `budget`; null when unusable or inflating past it. */
|
||||
async function candidateBytes(
|
||||
bytes: Uint8Array,
|
||||
candidate: EmbeddedFileCandidate,
|
||||
budget: number,
|
||||
): Promise<Uint8Array | null> {
|
||||
if (candidate.length > MAX_ATTACHMENT_BYTES) return null
|
||||
const data = bytes.subarray(
|
||||
candidate.offset,
|
||||
candidate.offset + candidate.length,
|
||||
)
|
||||
if (candidate.filter === null) return data
|
||||
return inflateFlate(data)
|
||||
return inflateFlate(data, budget)
|
||||
}
|
||||
|
||||
/** `{` first, past an optional UTF-8 BOM and JSON whitespace: the only shape a version lives in. */
|
||||
function startsJsonObject(data: Uint8Array): boolean {
|
||||
let i = 0
|
||||
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) i = 3
|
||||
while (
|
||||
i < data.byteLength &&
|
||||
' \t\n\r'.includes(String.fromCharCode(data[i]!))
|
||||
) {
|
||||
i += 1
|
||||
}
|
||||
return data[i] === 0x7b
|
||||
}
|
||||
|
||||
/** JSON with a numeric `envelopeVersion` or `version`; undefined otherwise. */
|
||||
@@ -418,13 +464,21 @@ export interface LocatedAttachment {
|
||||
/**
|
||||
* The first candidate whose data is a versioned JSON document, decoded once.
|
||||
* None: `attachment_stripped` when the stamp is visible, else `no_attachment`.
|
||||
* One MAX_ATTACHMENT_BYTES budget of examined content (raw or inflated) is
|
||||
* shared by every candidate: a candidate that does not fit what is left ends
|
||||
* the search, so 32 crafted streams cost at most one attachment's worth of
|
||||
* inflation and parsing. A stream cancelled mid-inflation is not charged.
|
||||
*/
|
||||
export async function locateAttachment(
|
||||
bytes: Uint8Array,
|
||||
): Promise<LocatedAttachment> {
|
||||
let budget = MAX_ATTACHMENT_BYTES
|
||||
for (const candidate of findEmbeddedFiles(bytes)) {
|
||||
const data = await candidateBytes(bytes, candidate)
|
||||
if (candidate.length > budget) break
|
||||
const data = await candidateBytes(bytes, candidate, budget)
|
||||
if (!data) continue
|
||||
budget -= data.byteLength
|
||||
if (!startsJsonObject(data)) continue
|
||||
const parsed = parseVersioned(data)
|
||||
if (parsed !== undefined) return { bytes: data, data: parsed }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user