From 388e209ea97c4b5296a47d8699d20a4aa74d444c Mon Sep 17 00:00:00 2001 From: bchanot Date: Sat, 10 Oct 2026 16:01:12 +0200 Subject: [PATCH] fix(pdf): report:compare --diff follows the privacy default, generic reason on a malformed attachment, Unicode control and format characters escaped, regular files only --- apps/app/scripts/compare-reports.ts | 13 ++++++--- apps/app/scripts/compare/report.ts | 18 ++++++------ apps/app/tests/unit/scripts.compare.test.ts | 32 +++++++++++++++++---- docs/PDF-FORMAT.md | 5 ++-- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/apps/app/scripts/compare-reports.ts b/apps/app/scripts/compare-reports.ts index 3ca73abc..87ca0b24 100644 --- a/apps/app/scripts/compare-reports.ts +++ b/apps/app/scripts/compare-reports.ts @@ -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 { - 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) } diff --git a/apps/app/scripts/compare/report.ts b/apps/app/scripts/compare/report.ts index e55f1d83..4917841c 100644 --- a/apps/app/scripts/compare/report.ts +++ b/apps/app/scripts/compare/report.ts @@ -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, diff --git a/apps/app/tests/unit/scripts.compare.test.ts b/apps/app/tests/unit/scripts.compare.test.ts index 6f388cab..a3be032c 100644 --- a/apps/app/tests/unit/scripts.compare.test.ts +++ b/apps/app/tests/unit/scripts.compare.test.ts @@ -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', ) }) diff --git a/docs/PDF-FORMAT.md b/docs/PDF-FORMAT.md index e8a5bb60..0a70de92 100644 --- a/docs/PDF-FORMAT.md +++ b/docs/PDF-FORMAT.md @@ -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.