diff --git a/apps/app/src/app/routes/HomeRoute.tsx b/apps/app/src/app/routes/HomeRoute.tsx index bfdcc237..b9a8be52 100644 --- a/apps/app/src/app/routes/HomeRoute.tsx +++ b/apps/app/src/app/routes/HomeRoute.tsx @@ -103,6 +103,8 @@ function importErrorMessage(err: unknown, t: Translate): string { return t('home.import.error_attachment_stripped') case 'no_attachment': return t('home.import.error_no_attachment') + case 'lone_surrogate': + return t('home.import.error_lone_surrogate') case 'core_unavailable': return t('home.import.error_core_unavailable') } diff --git a/apps/app/src/core/sheetOps.ts b/apps/app/src/core/sheetOps.ts index 3f8c9fcb..07bb144b 100644 --- a/apps/app/src/core/sheetOps.ts +++ b/apps/app/src/core/sheetOps.ts @@ -82,7 +82,8 @@ export class SheetOps { /** * Reads a PDF attachment (any supported shape, as the byte scan parsed it) and rebases - * it on this sheet. Failures: `incompatible_version` (with `found`), `no_attachment`. + * it on this sheet. Failures: `incompatible_version` (with `found`), `no_attachment`, + * `lone_surrogate` (a text the core would alter: refused, never rewritten). */ importArchive(attachment: unknown): CoreOutcome { return this.run('archive.import', attachment) diff --git a/apps/app/src/i18n/en.ts b/apps/app/src/i18n/en.ts index 3762f768..790d0fe7 100644 --- a/apps/app/src/i18n/en.ts +++ b/apps/app/src/i18n/en.ts @@ -52,6 +52,8 @@ export const en: Dict = { "Report opened on the current sheet, which differs from the archive's. Items not found: {count} (kept with their labels).", 'home.import.error_attachment_stripped': 'This Faunosteo PDF lost its embedded data (attachment removed by a viewer or an optimiser). Use the original file.', + 'home.import.error_lone_surrogate': + 'This Faunosteo PDF contains malformed text that this version cannot read without altering it. Import refused; the file was not modified.', 'home.no_sheet.title': 'No anatomical sheet', 'home.no_sheet.description': 'This build of the application carries no anatomical sheet: no report can be created or imported.', diff --git a/apps/app/src/i18n/fr.ts b/apps/app/src/i18n/fr.ts index 11fd45e2..7a2511b2 100644 --- a/apps/app/src/i18n/fr.ts +++ b/apps/app/src/i18n/fr.ts @@ -54,6 +54,8 @@ export const fr: Dict = { "Bilan ouvert sur la fiche actuelle, différente de celle de l'archive. Éléments non retrouvés : {count} (conservés avec leurs libellés).", 'home.import.error_attachment_stripped': "Ce PDF Faunosteo a perdu ses données embarquées (pièce jointe supprimée par un lecteur ou un optimiseur). Utilisez le fichier d'origine.", + 'home.import.error_lone_surrogate': + "Ce PDF Faunosteo contient un texte mal encodé que cette version ne peut pas relire sans l'altérer. Import refusé ; le fichier n'a pas été modifié.", 'home.no_sheet.title': 'Aucune fiche anatomique', 'home.no_sheet.description': "Cette version de l'application n'embarque aucune fiche anatomique : aucun bilan ne peut être créé ni importé.", diff --git a/apps/app/src/pdf/import.ts b/apps/app/src/pdf/import.ts index 65a40d46..f97d281a 100644 --- a/apps/app/src/pdf/import.ts +++ b/apps/app/src/pdf/import.ts @@ -8,7 +8,14 @@ import type { SheetIndex } from '@/services/sheetIndex' export { MAX_PDF_BYTES } from '@faunosteo/pdf' -export type ImportErrorKind = ArchiveReadErrorKind | 'core_unavailable' +/** + * `lone_surrogate`: the attachment escapes a lone UTF-16 surrogate, which the + * core cannot read without altering it (L1): refused, the file untouched. + */ +export type ImportErrorKind = + | ArchiveReadErrorKind + | 'core_unavailable' + | 'lone_surrogate' /** * Localization-agnostic import error. Carries a kind discriminant + an @@ -46,7 +53,8 @@ export async function readAttachment(pdfBytes: Uint8Array): Promise { * attachment (report first, v2 migrated through the sheet's map, then the * envelope kept or its snapshot dropped with a reason) and the rebase onto * that sheet. An unknown numeric report version is `incompatible_version`, - * any other unreadable content `no_attachment`, as the engine reported them. + * a text the core would alter `lone_surrogate`, any other unreadable content + * `no_attachment`, as the engine reported them. */ export function importAttachment( attachment: unknown, @@ -59,6 +67,8 @@ export function importAttachment( throw new ImportError('incompatible_version', outcome.found ?? null) case 'no_attachment': throw new ImportError('no_attachment') + case 'lone_surrogate': + throw new ImportError('lone_surrogate') case 'core_unavailable': case 'internal': throw new ImportError('core_unavailable') diff --git a/apps/app/src/state/reportStore.ts b/apps/app/src/state/reportStore.ts index 04dc9fad..e9e0e8b3 100644 --- a/apps/app/src/state/reportStore.ts +++ b/apps/app/src/state/reportStore.ts @@ -53,7 +53,7 @@ export interface ActionResult { /** What a draft restore did; `null` from the action when there was nothing to restore. */ export type RestoreResult = | { kind: 'restored'; rebase: RebaseOutcome } - /** The draft is unreadable by this build: set aside under a quarantine key. */ + /** The draft is unreadable by this build: set aside, verbatim, under a quarantine key. */ | { kind: 'quarantined' } /** * The draft is unreadable but could not be set aside (storage full): it stays in @@ -123,6 +123,17 @@ function applyCore( return OK } +/** + * The core's content kinds for a draft: the text itself is unreadable by this build + * (`lone_surrogate`: reading it would alter a string, L1). Any other failure is the core's + * or the sheet's and leaves the draft in place. + */ +const UNREADABLE_DRAFT_KINDS: ReadonlySet = new Set([ + 'invalid', + 'unsupported_version', + 'lone_surrogate', +]) + /** Reads the draft through the core; unreadable content goes to quarantine, never away. */ function restore( index: SheetIndex, @@ -135,7 +146,7 @@ function restore( const outcome = index.ops.restoreDraft(draft, archiveText) if (!outcome.ok) { // Only an unreadable draft is set aside; a core or sheet failure leaves it in place. - if (outcome.kind !== 'invalid' && outcome.kind !== 'unsupported_version') { + if (!UNREADABLE_DRAFT_KINDS.has(outcome.kind)) { return { result: { kind: 'core_unavailable' } } } const setAside = quarantine('draft', draft, DRAFT_KEY) diff --git a/apps/app/tests/unit/pdf.import.test.ts b/apps/app/tests/unit/pdf.import.test.ts index b4fb5b76..84e6b211 100644 --- a/apps/app/tests/unit/pdf.import.test.ts +++ b/apps/app/tests/unit/pdf.import.test.ts @@ -7,9 +7,14 @@ */ import { describe, it, expect, beforeAll } from 'vitest' import { PDFDocument } from 'pdf-lib' -import { importReportFromPdf, ImportError, MAX_PDF_BYTES } from '@/pdf/import' +import { + importAttachment, + importReportFromPdf, + ImportError, + MAX_PDF_BYTES, +} from '@/pdf/import' import type { SheetIndex } from '@/services/sheetIndex' -import { loadTestSheet } from '../helpers/sheet' +import { loadTestSheet, minimalReport } from '../helpers/sheet' let sheet: SheetIndex beforeAll(async () => { @@ -46,3 +51,24 @@ describe('importReportFromPdf error kinds', () => { await rejectsWith(new Uint8Array(MAX_PDF_BYTES + 1), 'invalid_pdf') }) }) + +describe('importAttachment on the core (L1)', () => { + it('refuses an attachment with a lone surrogate as lone_surrogate, never rewritten', () => { + for (const text of ['a\ud800b', '\udfff']) { + const attachment = minimalReport(sheet, { conclusion: text }) + expect(() => importAttachment(attachment, sheet)).toThrow( + expect.objectContaining({ kind: 'lone_surrogate' }), + ) + expect(attachment.conclusion).toBe(text) + } + }) + + it('imports normal text with pairs and literal backslashes unchanged', () => { + const text = 'é 😀 \\ud800 \ufffd' + const imported = importAttachment( + minimalReport(sheet, { conclusion: text }), + sheet, + ) + expect(imported.rebase.report.conclusion).toBe(text) + }) +}) diff --git a/apps/app/tests/unit/routes.HomeRoute.import.test.tsx b/apps/app/tests/unit/routes.HomeRoute.import.test.tsx index a72d610c..f129bc31 100644 --- a/apps/app/tests/unit/routes.HomeRoute.import.test.tsx +++ b/apps/app/tests/unit/routes.HomeRoute.import.test.tsx @@ -4,7 +4,7 @@ * then `importAttachment`, the core's layered read and rebase on the loaded * sheet): the migration alert (degraded headline, then the count), the three * ImportError kinds plus the generic error mapped to the FR messages, and the - * navigation to /editor on success. + * navigation to /editor on success; `lone_surrogate` (L1) has its own message. */ import { describe, @@ -171,6 +171,11 @@ describe('HomeRoute import: error paths', () => { new ImportError('no_attachment'), fr['home.import.error_no_attachment']!, ], + [ + 'lone_surrogate → malformed text message, file untouched', + new ImportError('lone_surrogate'), + fr['home.import.error_lone_surrogate']!, + ], [ 'generic Error → generic message', new Error('disk on fire'), diff --git a/apps/app/tests/unit/state.quarantine.test.ts b/apps/app/tests/unit/state.quarantine.test.ts index 24d2bc67..d07f8809 100644 --- a/apps/app/tests/unit/state.quarantine.test.ts +++ b/apps/app/tests/unit/state.quarantine.test.ts @@ -1,7 +1,8 @@ /** * R2 plan P12 (rev 2): a draft or an archive the running build cannot read is * set aside under a quarantine key, never dropped; only the content failures - * of the core (`invalid`, `unsupported_version`) quarantine, any other failure + * of the core (`invalid`, `unsupported_version`, `lone_surrogate`: L1, a text + * the core would alter) quarantine, any other failure * leaves both keys where they were; the source key is removed only once the * quarantine write succeeded; a draft rebased with unresolved items keeps its * raw pre-rebase text. @@ -57,6 +58,18 @@ describe('an unreadable draft', () => { expect(useReportStore.getState().report).toBeNull() }) + it('with a lone surrogate (L1) is moved verbatim, the surrogate kept', () => { + const raw = JSON.stringify(minimalReport(sheet, { conclusion: 'a\ud800b' })) + expect(raw).toContain('\\ud800') + sessionStorage.setItem(DRAFT_KEY, raw) + expect(restore()).toEqual({ kind: 'quarantined' }) + const kept = sessionStorage.getItem(QUARANTINE_KEYS.draft) + expect(kept).toBe(raw) + expect(JSON.parse(kept!).conclusion).toBe('a\ud800b') + expect(sessionStorage.getItem(DRAFT_KEY)).toBeNull() + expect(useReportStore.getState().report).toBeNull() + }) + it('stays where it is when the quarantine write fails (storage full)', () => { const raw = JSON.stringify({ version: 7 }) sessionStorage.setItem(DRAFT_KEY, raw) @@ -108,6 +121,40 @@ describe('an unreadable archive beside a readable draft', () => { }) }) +describe('a lone surrogate (L1) beside a readable draft or in the open report', () => { + it('sets the archive aside verbatim while the draft restores', () => { + sessionStorage.setItem( + DRAFT_KEY, + JSON.stringify(minimalReport(sheet, { conclusion: 'kept' })), + ) + const archive = JSON.stringify({ format: 'x', note: '\udc00' }) + sessionStorage.setItem(ARCHIVE_KEY, archive) + expect(restore()).toMatchObject({ kind: 'restored' }) + expect(useReportStore.getState().report?.conclusion).toBe('kept') + expect(sessionStorage.getItem(QUARANTINE_KEYS.archive)).toBe(archive) + }) + + it('refuses a core action and leaves the report as it was', () => { + const report = minimalReport(sheet, { conclusion: 'a\ud800b' }) + useReportStore.setState({ report }) + expect(useReportStore.getState().addObservation()).toEqual({ ok: false }) + expect(useReportStore.getState().report).toBe(report) + expect(useReportStore.getState().report?.conclusion).toBe('a\ud800b') + }) + + it('leaves normal text with pairs and literal backslashes unchanged', () => { + const text = 'é 😀 \\ud800 \ufffd' + sessionStorage.setItem( + DRAFT_KEY, + JSON.stringify(minimalReport(sheet, { conclusion: text })), + ) + expect(restore()).toMatchObject({ kind: 'restored' }) + expect(useReportStore.getState().report?.conclusion).toBe(text) + expect(useReportStore.getState().addObservation()).toEqual({ ok: true }) + expect(useReportStore.getState().report?.conclusion).toBe(text) + }) +}) + describe('a draft rebased onto the loaded sheet', () => { const OTHER_SHA = 'c'.repeat(64) diff --git a/packages/core-wasm/tests/facade.test.ts b/packages/core-wasm/tests/facade.test.ts index 186ed660..0529b7e5 100644 --- a/packages/core-wasm/tests/facade.test.ts +++ b/packages/core-wasm/tests/facade.test.ts @@ -36,6 +36,20 @@ describe('core facade (shipped wasm build)', () => { }) }) + it('refuses a lone surrogate with its own kind, never a replacement (L1)', async () => { + const core = await nodeCoreRuntime() + const zone = JSON.stringify({ annotationId: 'a\ud800', viewId: 'v' }) + expect(zone).toContain('\\ud800') + const refused = core.call('report_zone', zone) + expect(refused).toMatchObject({ ok: false, kind: 'lone_surrogate' }) + expect(JSON.stringify(refused)).not.toContain('\ufffd') + const paired = JSON.stringify({ annotationId: 'a😀\\ud800', viewId: 'v' }) + expect(core.call('report_zone', paired)).toEqual({ + ok: true, + value: { annotationId: 'a😀\\ud800', viewId: 'v' }, + }) + }) + it('ships no trap export (the proofs live in pkg-proofs only)', async () => { const glue = (await nodeCoreSource('pkg').importGlue(0)) as unknown as Record< string, diff --git a/packages/supabase/src/anatomical-sheets.ts b/packages/supabase/src/anatomical-sheets.ts index dc586bc8..a516e808 100644 --- a/packages/supabase/src/anatomical-sheets.ts +++ b/packages/supabase/src/anatomical-sheets.ts @@ -25,7 +25,10 @@ export type SheetCheckOutcome = export type SheetValidator = (content: unknown) => SheetCheckOutcome export type SheetPublishErrorKind = - /** The document does not match the strict schema. */ + /** + * The document does not match the strict schema, or carries a text the core cannot read + * without altering it (`lone_surrogate`). + */ | 'invalid_shape' /** Integrity errors (warnings never block). */ | 'not_publishable' @@ -61,11 +64,17 @@ export class SheetPublishError extends Error { } } +/** The validator's kinds that judge the document itself, not the validator's state. */ +const CONTENT_KINDS: ReadonlySet = new Set([ + 'invalid_shape', + 'lone_surrogate', +]) + /** Throws unless the validator says the document is publishable. */ function assertPublishable(validate: SheetValidator, content: unknown): void { const check = validate(content) if (!check.ok) { - throw check.kind === 'invalid_shape' + throw CONTENT_KINDS.has(check.kind) ? new SheetPublishError('invalid_shape', [], check.detail) : new SheetPublishError('validator_unavailable', [], check.kind) } diff --git a/packages/supabase/tests/anatomical-sheets.test.ts b/packages/supabase/tests/anatomical-sheets.test.ts index e472f498..5771280e 100644 --- a/packages/supabase/tests/anatomical-sheets.test.ts +++ b/packages/supabase/tests/anatomical-sheets.test.ts @@ -99,4 +99,15 @@ describe('publishSheetVersion', () => { ).rejects.toMatchObject({ kind: 'invalid_shape' }) expect(rpcCalls).toEqual([]) }) + + it('refuses a document carrying a lone surrogate as content, before any RPC (L1)', async () => { + const { client, rpcCalls } = stubClient({ + ...publishable, + name: { fr: 'Vue \ud800', en: 'View' }, + }) + await expect( + publishSheetVersion(client, 'v1', validate), + ).rejects.toMatchObject({ kind: 'invalid_shape' }) + expect(rpcCalls).toEqual([]) + }) })