fix(pdf): report:compare --diff follows the privacy default, generic reason on a malformed attachment, Unicode control and format characters escaped, regular files only

This commit is contained in:
bchanot committed 2026-10-10 16:01:12 +02:00
1 parent b1b3e4927e
commit 388e209ea9
4 files changed
+48 -20

No files matched your search

+9 -4
View File
@@ -8,9 +8,10 @@
* A v2 archive PDF is shown legacy id → canonical id, line by line; a v3 PDF
* is checked against the sheet. Items the sheet cannot place are marked
* `!! UNRESOLVED` with the reason. `--diff` compares the v3 content of two
* PDFs (an archive and its re-export after import, typically) and prints a
* differing text line in clear. By default only identifiers, catalog labels,
* counts and placeholders are printed; names and free texts need `--full`.
* PDFs (an archive and its re-export after import, typically). By default
* only identifiers, catalog labels, counts and placeholders are printed and
* compared (a text shows as `conclusion (present)`); names and free texts,
* printed and compared, need `--full`.
* Untrusted strings are printed with control characters escaped. It writes
* no output file.
*/
@@ -79,7 +80,9 @@ async function compareFile(
sheet: SheetIndex,
args: CliArgs,
): Promise<Comparison> {
if (statSync(path).size > MAX_PDF_BYTES) throw new Error('file too large')
const st = statSync(path)
if (!st.isFile()) throw new Error('not a regular file')
if (st.size > MAX_PDF_BYTES) throw new Error('file too large')
const bytes = new Uint8Array(readFileSync(path))
const pdf = await PDFDocument.load(bytes, { ignoreEncryption: true })
const raw = await readAttachedDocument(pdf)
@@ -100,6 +103,8 @@ function failureReason(err: unknown): string {
const code = (err as NodeJS.ErrnoException | undefined)?.code
if (code === 'ENOENT') return 'file not found'
if (code === 'EACCES') return 'file not readable'
// JSON.parse messages echo a fragment of the attachment.
if (err instanceof SyntaxError) return 'attachment is not valid JSON'
return err instanceof Error ? err.message : String(err)
}
+9 -9
View File
@@ -46,14 +46,15 @@ export interface Counts {
}
/**
* Escapes C0, DEL and C1 control characters as `\uXXXX` (newline and
* carriage return included): a PDF is untrusted, and a raw escape sequence
* printed to a terminal can rewrite the screen or fake a line.
* Escapes Unicode control (C0, DEL, C1) and format characters (bidi
* overrides, zero-width) plus the line and paragraph separators as `\uXXXX`
* (newline and carriage return included): a PDF is untrusted, and a raw
* escape sequence printed to a terminal can rewrite the screen or fake a line.
*/
export function esc(text: string): string {
return text.replace(
/[\u0000-\u001f\u007f-\u009f]/g,
(c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`,
/[\p{Cc}\p{Cf}\u2028\u2029]/gu,
(c) => `\\u${c.codePointAt(0)!.toString(16).padStart(4, '0')}`,
)
}
@@ -287,11 +288,10 @@ export function compareReport(
const v3Lines = report.observations.map((o) =>
observationLines(o, sheet, opts),
)
// A diff must see a lost conclusion: its lines always carry the full texts.
const fullOpts = { ...opts, full: true }
// Same privacy default as the listing: texts are compared with `--full`.
const normalized = [
...report.observations.flatMap((o) => observationLines(o, sheet, fullOpts)),
...footerLines(report, true),
...report.observations.flatMap((o) => observationLines(o, sheet, opts)),
...footerLines(report, opts.full),
]
return {
source,