Files
faunosteo/.claude/tasks/contracts/2026-10-11-step-06-security-fix.md

24 lines
4.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CONTRACT — step-06 security fix (after SECURITY BLOCK(1) + MEDIUM/LOW findings)
- date: 2026-10-11 | flow: ship-feature (executor, security loop) | branch: feature/step-06-pdf-archives
- status: verified (CONFORME on substance after oracle fix; security re-audit pending)
## REQUEST (IMMUTABLE)
Close the HIGH finding (PDF object injection through an archived branding URL) at the sink and at the schema, and the cheap MEDIUM/LOW findings listed below. No behaviour change for valid inputs: the Step 6 goldens must stay green. Findings deferred with a documented limit: pdf-lib uncapped object-stream inflation (pre-existing, needs a Worker or a pre-scan that would refuse our own PDFs), `<use>` nesting expansion (resvg already refuses), Report v3 collection caps on read.
## ACCEPTANCE CRITERIA
1. HIGH sink: `packages/pdf/src/render/linkAnnotation.ts` writes the URI as `PDFHexString.fromText(url)` (or escapes `\`, `(`, `)` and refuses control characters) so no URL can close the string literal; a test exports a PDF whose branding URL is `https://a.example/) /Next << /S /JavaScript /JS (app.alert\(1\)) >> /Z (` through `exportFromEnvelope` AFTER bypassing the schema with a cast, reloads the bytes with pdf-lib and asserts no `/JavaScript` action exists and the link's `/URI` decodes to the full original string (or the export is refused by criterion 3, in which case the test asserts the typed refusal and a second test proves the sink escaping on a URL with `)` that the schema accepts, if any).
CHECK: cd packages/pdf && corepack pnpm vitest run tests/security.test.ts 2>&1 | grep -E "Tests " | tail -1
EXPECT: Tests .* passed
EVIDENCE: 2026-10-11 verifier pass 1 ECARTS(1) = this oracle (`tail -2` kept only the Duration line), behaviour MET (33/33, sink + schema + refusal tests read). Oracle corrected by the orchestrator; literal run → `Tests 33 passed (33)`. MET.
2. Schema: `HttpUrlSchema` in `packages/domain/src/archive/branding.ts` accepts only `^https?://[^\s()\\<>"'\x00-\x1f\x7f]+$` (ASCII, no parentheses, backslash, quotes, angle brackets, whitespace, control characters) and ≤ 2048 chars; the current real URLs (`https://faunosteo.com`, `https://zenquality.fr`, `https://www.instagram.com/paws.iin.harmony`) pass; domain tests cover accept/reject.
3. Export re-validates its inputs: `exportReportArchive` runs `BrandingSchema.parse(input.branding)` and `PdfLabelSetSchema.parse(input.labels)` first; a failure → `ArchiveExportError('branding_invalid')` (new kind, documented) before any page is drawn; test.
4. PNG budget: `packages/pdf/src/branding.ts` reads the PNG IHDR (signature + width/height at bytes 16-23) before `embedPng`; refuse when width × height > 16,777,216 (4096²) or either side > 8192, or when the signature is wrong → that slot fails alone with a `logo_failed` warning; test with a crafted IHDR.
5. Sanitiser hardening in `packages/pdf/src/svg/sanitize.ts`: any attribute value containing a backslash `\` is removed; in `style` attributes and in every presentation attribute, any `(` not part of `url(#` is refused (attribute removed); `image-set(`, `expression(`, `@import`, `javascript:`, `behavior:` refused as today. `horse.svg` and `Paws_in_harmony.svg` still sanitise with `removed: []` (existing test stays green); new tests for `u\72l(`, `image-set(`, `fill="u\72l(http://evil/x)"`.
6. NaN guard: `packages/pdf/src/svg/view.ts` raster size uses `Number.isFinite` on the view box numbers; a non-finite or ≤ 0 box falls back to the 0.6 ratio; test with `viewBox="0 0 1e999 1e999"` → a valid size, no NaN.
7. `apps/app/src/app/routes/HomeRoute.tsx` checks `file.size > MAX_PDF_BYTES` before `arrayBuffer()` and maps it to the `invalid_pdf` message; route test updated (`routes.HomeRoute.import.test.tsx` gains one case, others unchanged).
8. `docs/PDF-FORMAT.md` "Limits" gains: the URL rule, the PNG budget, and a "Known limits" paragraph: pdf-lib inflates PDF object streams without a cap when loading (a crafted PDF of a few hundred KB can expand to hundreds of MB in the tab; mitigation planned: parse in a Worker or locate the attachment without a full load), `<use>` expansion bounded by the rasterizer, Report collections not capped on read (export refuses above 500 observations).
9. Green: `corepack pnpm typecheck && corepack pnpm lint && corepack pnpm test:run` from the root; goldens untouched (`git diff --stat 8b86b92 HEAD -- apps/app/tests/fixtures/goldens` empty).
## FILE SCOPE
packages/pdf/src/{render/linkAnnotation.ts,export.ts,errors.ts,branding.ts,svg/sanitize.ts,svg/view.ts,index.ts}, packages/pdf/tests/**, packages/domain/src/archive/branding.ts, packages/domain/tests/archive.branding-labels.test.ts, apps/app/src/app/routes/HomeRoute.tsx, apps/app/tests/unit/routes.HomeRoute.import.test.tsx, docs/PDF-FORMAT.md. Nothing else.