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

+29 -1
View File
@@ -161,6 +161,8 @@ Two operations are distinct:
| snapshot assets | ≤ 4, ≤ 2 MiB each, ≤ 6 MiB total |
| snapshot document | ≤ 64 views, ≤ 1,024 annotations, ≤ 8,192 finding nodes, depth ≤ 8, names ≤ 512 chars |
| branding | PNG base64 ≤ 512 KiB, SVG ≤ 256 KiB, http(s) URLs ≤ 2,048 chars |
| branding URL | `http(s)://` then printable ASCII only, without `"` `'` `(` `)` `<` `>` `\`, no space or control character |
| branding PNG | valid signature and IHDR, each side ≤ 8,192 px, ≤ 16,777,216 px (4096²) |
| report at export | ≤ 500 observations, ≤ 8 views per observation |
| SVG sanitiser | ≤ 20,000 elements, nesting ≤ 64, ≤ 256 `<use>`, blur `stdDeviation` ≤ 100 |
@@ -170,10 +172,36 @@ sanitised by allowlist (shapes, groups, gradients, the filter primitives the
drawings use, `clipPath`, `mask`, `switch`, `use`, `title`, `desc`; geometry,
presentation, gradient and filter attributes; `href` and `url()` only to a
`#fragment`; no `style` element, no `script`, `image`, `a`, `foreignObject`,
`text`, no event handler). The reference drawings pass unchanged (tested).
`text`, no event handler). An attribute value with a backslash is removed (a
CSS escape such as `u\72l(` would hide a reference), and a `style` or
presentation attribute may call no function other than `url(#…)`: `rgb(`,
`image-set(`, `expression(` and the like are removed with the attribute. The
reference drawings pass unchanged (tested).
The same caps are checked at export, so an export can never write a file its
own reader refuses.
The branding URL rule exists because a URL is written into every page's
link action: pdf-lib does not escape a literal string, so the engine writes
the URI as a hex string and the schema refuses the characters that could
close a literal. The export re-validates the branding and the labels it
receives (`ArchiveExportError('branding_invalid')` before any page is
drawn), even from an archive that passed the reader. A PNG logo whose header
is missing, wrong or over the pixel budget is never decoded: that slot alone
prints its fallback text and the export reports `logo_failed`.
### Known limits
- 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 before the
attachment is even looked at. The 50 MiB file cap bounds the input, not
the expansion. Mitigation planned: parse in a Worker, or locate the
attachment without a full load.
- `<use>` expansion is bounded by the rasterizer (resvg refuses an
oversized tree), not by the sanitiser, which only counts the `<use>`
elements (≤ 256).
- Report v3 collections are not capped on read; the export refuses above
500 observations and 8 views per observation.
## Checking an archive against its migration
`pnpm report:compare <file.pdf | folder>...` reads the attachment of one or