fix(pdf): bounded attachment extraction by byte scan — no pdf-lib parse on import, export without object streams (BLK-009)
This commit is contained in:
1 parent
62b8a43e09
commit
60ee71208f
14 files changed
+1054
-198
No files matched your search
@@ -1,4 +1,3 @@
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import {
|
||||
readArchiveDocument,
|
||||
UnsupportedReportVersionError,
|
||||
@@ -8,23 +7,12 @@ import {
|
||||
import { readAttachedDocument } from './attachment/read'
|
||||
import { ArchiveReadError } from './errors'
|
||||
|
||||
/** A Faunosteo PDF weighs a few hundred KB; anything larger is not one. */
|
||||
export const MAX_PDF_BYTES = 50 * 1024 * 1024
|
||||
|
||||
async function loadPdf(pdfBytes: Uint8Array): Promise<PDFDocument> {
|
||||
if (pdfBytes.byteLength > MAX_PDF_BYTES) {
|
||||
throw new ArchiveReadError('invalid_pdf')
|
||||
}
|
||||
try {
|
||||
return await PDFDocument.load(pdfBytes, { ignoreEncryption: true })
|
||||
} catch {
|
||||
throw new ArchiveReadError('invalid_pdf')
|
||||
}
|
||||
}
|
||||
export { MAX_PDF_BYTES } from './attachment/scan'
|
||||
|
||||
/**
|
||||
* Reads a Faunosteo PDF back: size cap, PDF parse, attachment (filename
|
||||
* first), JSON, then the layered reading of the domain (report first, then
|
||||
* Reads a Faunosteo PDF back: size cap and `%PDF-` header (`invalid_pdf`),
|
||||
* attachment located by a byte scan (filename first, never a PDF parse:
|
||||
* BLK-009), JSON, then the layered reading of the domain (report first, then
|
||||
* the envelope accepted whole or dropped with a reason). An unknown report
|
||||
* version is `incompatible_version` with that version; any other content
|
||||
* failure is `no_attachment`, as today.
|
||||
@@ -33,8 +21,7 @@ export async function readReportArchive(
|
||||
pdfBytes: Uint8Array,
|
||||
map: ReportMigrationMap,
|
||||
): Promise<ArchiveReadOutcome> {
|
||||
const pdfDoc = await loadPdf(pdfBytes)
|
||||
const data = await readAttachedDocument(pdfDoc)
|
||||
const data = await readAttachedDocument(pdfBytes)
|
||||
try {
|
||||
return readArchiveDocument(data, map)
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user