fix(pdf): security hardening — hex-encoded link URIs, strict branding URL schema, branding and labels re-validated at export, PNG dimension budget, CSS escape and parenthesis rules in the sanitiser, finite raster size, file size checked before read

This commit is contained in:
bchanot committed 2026-10-11 13:54:18 +02:00
1 parent 4930704d8a
commit 1f417b21fd
12 files changed
+437 -25

No files matched your search

@@ -23,7 +23,7 @@ import {
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import type { ArchiveReadOutcome } from '@faunosteo/domain'
import HomeRoute from '@/app/routes/HomeRoute'
import { ImportError, importReportFromPdf } from '@/pdf/import'
import { ImportError, importReportFromPdf, MAX_PDF_BYTES } from '@/pdf/import'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
@@ -72,7 +72,14 @@ function outcome(patch: {
}
}
async function mountAndPickPdf(): Promise<void> {
/** A 4-byte "%PDF" file; the import is mocked, the bytes only travel. */
function pdfFile(): File {
return new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
type: 'application/pdf',
})
}
async function mountAndPickPdf(file: File = pdfFile()): Promise<void> {
render(
<MemoryRouter initialEntries={['/']}>
<Routes>
@@ -84,9 +91,6 @@ async function mountAndPickPdf(): Promise<void> {
const button = screen.getByRole('button', { name: fr['home.import.cta'] })
await waitFor(() => expect(button).toBeEnabled())
const input = document.querySelector('input[type="file"]')!
const file = new File([new Uint8Array([37, 80, 68, 70])], 'bilan.pdf', {
type: 'application/pdf',
})
fireEvent.change(input, { target: { files: [file] } })
}
@@ -160,3 +164,18 @@ describe('HomeRoute import: error paths', () => {
expect(alertMock()).not.toHaveBeenCalled()
})
})
describe('HomeRoute import: size gate', () => {
it('refuses a file over MAX_PDF_BYTES before reading it: unreadable PDF message', async () => {
const file = pdfFile()
Object.defineProperty(file, 'size', { value: MAX_PDF_BYTES + 1 })
const read = vi.spyOn(file, 'arrayBuffer')
await mountAndPickPdf(file)
await waitFor(() => expect(errorText()).not.toBeNull())
expect(errorText()!.textContent).toBe(fr['home.import.error_invalid_pdf'])
expect(read).not.toHaveBeenCalled()
expect(importMock).not.toHaveBeenCalled()
expect(screen.queryByTestId('editor-route')).toBeNull()
expect(useReportStore.getState().report).toBeNull()
})
})