fix(app): set lone-surrogate texts aside or refuse them with a message (L1)
A draft the core refuses as lone_surrogate is unreadable content: it
goes to osteo:quarantine:draft verbatim with the existing notice. An
import is refused as ImportError('lone_surrogate') with the owner's
wording (home.import.error_lone_surrogate, FR/EN), the file untouched.
The publish gate reads the kind as invalid_shape. Tests on the real
core: quarantine keeps the exact text, an editor action leaves the
report as it was, normal text with pairs and literal backslashes reads
unchanged.
This commit is contained in:
1 parent
20d03cf7fd
commit
da8a0370cb
12 files changed
+151
-11
No files matched your search
@@ -103,6 +103,8 @@ function importErrorMessage(err: unknown, t: Translate): string {
|
|||||||
return t('home.import.error_attachment_stripped')
|
return t('home.import.error_attachment_stripped')
|
||||||
case 'no_attachment':
|
case 'no_attachment':
|
||||||
return t('home.import.error_no_attachment')
|
return t('home.import.error_no_attachment')
|
||||||
|
case 'lone_surrogate':
|
||||||
|
return t('home.import.error_lone_surrogate')
|
||||||
case 'core_unavailable':
|
case 'core_unavailable':
|
||||||
return t('home.import.error_core_unavailable')
|
return t('home.import.error_core_unavailable')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -82,7 +82,8 @@ export class SheetOps {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a PDF attachment (any supported shape, as the byte scan parsed it) and rebases
|
* 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<ArchiveImport> {
|
importArchive(attachment: unknown): CoreOutcome<ArchiveImport> {
|
||||||
return this.run('archive.import', attachment)
|
return this.run('archive.import', attachment)
|
||||||
|
|||||||
@@ -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).",
|
"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':
|
'home.import.error_attachment_stripped':
|
||||||
'This Faunosteo PDF lost its embedded data (attachment removed by a viewer or an optimiser). Use the original file.',
|
'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.title': 'No anatomical sheet',
|
||||||
'home.no_sheet.description':
|
'home.no_sheet.description':
|
||||||
'This build of the application carries no anatomical sheet: no report can be created or imported.',
|
'This build of the application carries no anatomical sheet: no report can be created or imported.',
|
||||||
|
|||||||
@@ -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).",
|
"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':
|
'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.",
|
"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.title': 'Aucune fiche anatomique',
|
||||||
'home.no_sheet.description':
|
'home.no_sheet.description':
|
||||||
"Cette version de l'application n'embarque aucune fiche anatomique : aucun bilan ne peut être créé ni importé.",
|
"Cette version de l'application n'embarque aucune fiche anatomique : aucun bilan ne peut être créé ni importé.",
|
||||||
|
|||||||
@@ -8,7 +8,14 @@ import type { SheetIndex } from '@/services/sheetIndex'
|
|||||||
|
|
||||||
export { MAX_PDF_BYTES } from '@faunosteo/pdf'
|
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
|
* Localization-agnostic import error. Carries a kind discriminant + an
|
||||||
@@ -46,7 +53,8 @@ export async function readAttachment(pdfBytes: Uint8Array): Promise<unknown> {
|
|||||||
* attachment (report first, v2 migrated through the sheet's map, then the
|
* 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
|
* envelope kept or its snapshot dropped with a reason) and the rebase onto
|
||||||
* that sheet. An unknown numeric report version is `incompatible_version`,
|
* 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(
|
export function importAttachment(
|
||||||
attachment: unknown,
|
attachment: unknown,
|
||||||
@@ -59,6 +67,8 @@ export function importAttachment(
|
|||||||
throw new ImportError('incompatible_version', outcome.found ?? null)
|
throw new ImportError('incompatible_version', outcome.found ?? null)
|
||||||
case 'no_attachment':
|
case 'no_attachment':
|
||||||
throw new ImportError('no_attachment')
|
throw new ImportError('no_attachment')
|
||||||
|
case 'lone_surrogate':
|
||||||
|
throw new ImportError('lone_surrogate')
|
||||||
case 'core_unavailable':
|
case 'core_unavailable':
|
||||||
case 'internal':
|
case 'internal':
|
||||||
throw new ImportError('core_unavailable')
|
throw new ImportError('core_unavailable')
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ export interface ActionResult {
|
|||||||
/** What a draft restore did; `null` from the action when there was nothing to restore. */
|
/** What a draft restore did; `null` from the action when there was nothing to restore. */
|
||||||
export type RestoreResult =
|
export type RestoreResult =
|
||||||
| { kind: 'restored'; rebase: RebaseOutcome }
|
| { 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' }
|
| { kind: 'quarantined' }
|
||||||
/**
|
/**
|
||||||
* The draft is unreadable but could not be set aside (storage full): it stays in
|
* The draft is unreadable but could not be set aside (storage full): it stays in
|
||||||
@@ -123,6 +123,17 @@ function applyCore(
|
|||||||
return OK
|
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<string> = new Set([
|
||||||
|
'invalid',
|
||||||
|
'unsupported_version',
|
||||||
|
'lone_surrogate',
|
||||||
|
])
|
||||||
|
|
||||||
/** Reads the draft through the core; unreadable content goes to quarantine, never away. */
|
/** Reads the draft through the core; unreadable content goes to quarantine, never away. */
|
||||||
function restore(
|
function restore(
|
||||||
index: SheetIndex,
|
index: SheetIndex,
|
||||||
@@ -135,7 +146,7 @@ function restore(
|
|||||||
const outcome = index.ops.restoreDraft(draft, archiveText)
|
const outcome = index.ops.restoreDraft(draft, archiveText)
|
||||||
if (!outcome.ok) {
|
if (!outcome.ok) {
|
||||||
// Only an unreadable draft is set aside; a core or sheet failure leaves it in place.
|
// 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' } }
|
return { result: { kind: 'core_unavailable' } }
|
||||||
}
|
}
|
||||||
const setAside = quarantine('draft', draft, DRAFT_KEY)
|
const setAside = quarantine('draft', draft, DRAFT_KEY)
|
||||||
|
|||||||
@@ -7,9 +7,14 @@
|
|||||||
*/
|
*/
|
||||||
import { describe, it, expect, beforeAll } from 'vitest'
|
import { describe, it, expect, beforeAll } from 'vitest'
|
||||||
import { PDFDocument } from 'pdf-lib'
|
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 type { SheetIndex } from '@/services/sheetIndex'
|
||||||
import { loadTestSheet } from '../helpers/sheet'
|
import { loadTestSheet, minimalReport } from '../helpers/sheet'
|
||||||
|
|
||||||
let sheet: SheetIndex
|
let sheet: SheetIndex
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
@@ -46,3 +51,24 @@ describe('importReportFromPdf error kinds', () => {
|
|||||||
await rejectsWith(new Uint8Array(MAX_PDF_BYTES + 1), 'invalid_pdf')
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
* then `importAttachment`, the core's layered read and rebase on the loaded
|
* then `importAttachment`, the core's layered read and rebase on the loaded
|
||||||
* sheet): the migration alert (degraded headline, then the count), the three
|
* sheet): the migration alert (degraded headline, then the count), the three
|
||||||
* ImportError kinds plus the generic error mapped to the FR messages, and the
|
* 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 {
|
import {
|
||||||
describe,
|
describe,
|
||||||
@@ -171,6 +171,11 @@ describe('HomeRoute import: error paths', () => {
|
|||||||
new ImportError('no_attachment'),
|
new ImportError('no_attachment'),
|
||||||
fr['home.import.error_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',
|
'generic Error → generic message',
|
||||||
new Error('disk on fire'),
|
new Error('disk on fire'),
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* R2 plan P12 (rev 2): a draft or an archive the running build cannot read is
|
* 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
|
* 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
|
* 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
|
* quarantine write succeeded; a draft rebased with unresolved items keeps its
|
||||||
* raw pre-rebase text.
|
* raw pre-rebase text.
|
||||||
@@ -57,6 +58,18 @@ describe('an unreadable draft', () => {
|
|||||||
expect(useReportStore.getState().report).toBeNull()
|
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)', () => {
|
it('stays where it is when the quarantine write fails (storage full)', () => {
|
||||||
const raw = JSON.stringify({ version: 7 })
|
const raw = JSON.stringify({ version: 7 })
|
||||||
sessionStorage.setItem(DRAFT_KEY, raw)
|
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', () => {
|
describe('a draft rebased onto the loaded sheet', () => {
|
||||||
const OTHER_SHA = 'c'.repeat(64)
|
const OTHER_SHA = 'c'.repeat(64)
|
||||||
|
|
||||||
|
|||||||
@@ -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 () => {
|
it('ships no trap export (the proofs live in pkg-proofs only)', async () => {
|
||||||
const glue = (await nodeCoreSource('pkg').importGlue(0)) as unknown as Record<
|
const glue = (await nodeCoreSource('pkg').importGlue(0)) as unknown as Record<
|
||||||
string,
|
string,
|
||||||
|
|||||||
@@ -25,7 +25,10 @@ export type SheetCheckOutcome =
|
|||||||
export type SheetValidator = (content: unknown) => SheetCheckOutcome
|
export type SheetValidator = (content: unknown) => SheetCheckOutcome
|
||||||
|
|
||||||
export type SheetPublishErrorKind =
|
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'
|
| 'invalid_shape'
|
||||||
/** Integrity errors (warnings never block). */
|
/** Integrity errors (warnings never block). */
|
||||||
| 'not_publishable'
|
| '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<string> = new Set([
|
||||||
|
'invalid_shape',
|
||||||
|
'lone_surrogate',
|
||||||
|
])
|
||||||
|
|
||||||
/** Throws unless the validator says the document is publishable. */
|
/** Throws unless the validator says the document is publishable. */
|
||||||
function assertPublishable(validate: SheetValidator, content: unknown): void {
|
function assertPublishable(validate: SheetValidator, content: unknown): void {
|
||||||
const check = validate(content)
|
const check = validate(content)
|
||||||
if (!check.ok) {
|
if (!check.ok) {
|
||||||
throw check.kind === 'invalid_shape'
|
throw CONTENT_KINDS.has(check.kind)
|
||||||
? new SheetPublishError('invalid_shape', [], check.detail)
|
? new SheetPublishError('invalid_shape', [], check.detail)
|
||||||
: new SheetPublishError('validator_unavailable', [], check.kind)
|
: new SheetPublishError('validator_unavailable', [], check.kind)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,4 +99,15 @@ describe('publishSheetVersion', () => {
|
|||||||
).rejects.toMatchObject({ kind: 'invalid_shape' })
|
).rejects.toMatchObject({ kind: 'invalid_shape' })
|
||||||
expect(rpcCalls).toEqual([])
|
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([])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user