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,
+27 -5
View File
@@ -183,12 +183,27 @@ describe('diff and arguments', () => {
const a = compareRaw(v2)
const b = compareRaw(migrated)
expect(diffLines(a.normalized, b.normalized)).toEqual([])
const edited = { ...migrated, conclusion: 'changed' }
expect(diffLines(a.normalized, compareRaw(edited).normalized)).toEqual([
'+ conclusion "changed"',
const added = { ...migrated, conclusion: 'changed' }
expect(diffLines(a.normalized, compareRaw(added).normalized)).toEqual([
'+ conclusion (present)',
])
})
it('compares placeholders by default and real texts with --full', () => {
const base = readReportDocument(minimalV2(), sheet.sheet.migrationMap)
const original = { ...base.report, conclusion: 'original' }
const edited = { ...base.report, conclusion: 'changed' }
expect(
diffLines(compareRaw(original).normalized, compareRaw(edited).normalized),
).toEqual([])
expect(
diffLines(
compareRaw(original, true).normalized,
compareRaw(edited, true).normalized,
),
).toEqual(['- conclusion "original"', '+ conclusion "changed"'])
})
it('parses files and options, refuses unknown ones', () => {
expect(parseArgs(['a.pdf', '--summary', 'dir', '--locale', 'en'])).toEqual({
files: ['a.pdf', 'dir'],
@@ -223,7 +238,7 @@ describe('terminal safety and privacy', () => {
},
],
freeTextFindings: [`t${ESC}\nu`],
comment: `c${ESC}\nd`,
comment: `c${ESC}\nd\u009b\u202e\u2028`,
},
],
})
@@ -234,6 +249,12 @@ describe('terminal safety and privacy', () => {
expect(text).toContain('\\u001b')
expect(text).toContain('\\u000a')
expect(text).toContain('[o\\u001b[2K\\u000a1]')
for (const raw of ['\u009b', '\u202e', '\u2028']) {
expect(text).not.toContain(raw)
}
for (const escaped of ['\\u009b', '\\u202e', '\\u2028']) {
expect(text).toContain(escaped)
}
})
it('does not throw on prototype-chain names used as legacy ids', () => {
@@ -288,7 +309,8 @@ describe('terminal safety and privacy', () => {
for (const word of ['TEXT', 'COMMENT', 'CONCLUSION', 'RECO', 'WHEN']) {
expect(shown).toContain(`SECRET-${word}`)
}
expect(compareRaw(report).normalized.join('\n')).toContain(
expect(compareRaw(report).normalized.join('\n')).not.toContain('SECRET')
expect(compareRaw(report, true).normalized.join('\n')).toContain(
'SECRET-CONCLUSION',
)
})
+3 -2
View File
@@ -51,7 +51,8 @@ texts; by default only identifiers, catalog labels, snapshots of unresolved
items, counts and placeholders such as `comment (present)` are printed),
`--locale fr|en` (labels), `--diff a.pdf b.pdf` (compare the v3
content of two PDFs: an archive and the PDF exported after importing it
should print `identical v3 content`; a differing text line is printed in
clear, whatever `--full`). Every untrusted string is printed with control
should print `identical v3 content`; by default the texts are compared as
placeholders, so an edited text shows only when it appears or disappears;
`--full` compares the real texts and prints the differing lines). Every untrusted string is printed with control
characters escaped as `\uXXXX`. The tool exits 2 when a file could not
be read (no attachment, unknown version, oversized) and prints the reason.