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

+50 -6
View File
@@ -5,10 +5,12 @@ import { SvgRejectedError } from '../errors'
* Allowlist sanitiser for the sheet drawings and the branding SVGs
* (STEP6 §15), on DOM Level 2 only. Elements and attributes outside the
* inventory of the real assets plus structural needs are removed with
* their subtree; `href` and `url(…)` must stay inside the document; a
* `style` attribute may carry no external reference; the halo filters are
* clamped. Element, nesting and `<use>` counters refuse an oversized tree.
* The caller learns what was removed (names only, never values).
* their subtree; `href` and `url(…)` must stay inside the document; no
* value carries a backslash (a CSS escape such as `u\72l(` would hide a
* reference from the rules below); a `style` or presentation attribute may
* call no function but `url(#…)`; the halo filters are clamped. Element,
* nesting and `<use>` counters refuse an oversized tree. The caller learns
* what was removed (names only, never values).
*/
export const SANITIZER_CAPS = {
@@ -126,9 +128,42 @@ const ALLOWED_ATTRIBUTES = new Set([
'style',
])
/** Attributes whose value is CSS-like: a function call there is a reference or an exploit. */
const PRESENTATION_ATTRIBUTES = new Set([
'fill',
'fill-opacity',
'fill-rule',
'stroke',
'stroke-width',
'stroke-linecap',
'stroke-linejoin',
'stroke-opacity',
'stroke-dasharray',
'stroke-dashoffset',
'stroke-miterlimit',
'opacity',
'filter',
'clip-path',
'clip-rule',
'mask',
'display',
'visibility',
'color',
'stop-color',
'stop-opacity',
'flood-color',
'flood-opacity',
'color-interpolation-filters',
'font-family',
'font-size',
'font-weight',
])
const LOCAL_REFERENCE = /^#[A-Za-z_][\w.:-]*$/
const URL_FUNCTION = /url\(\s*['"]?\s*(.?)/gi
const STYLE_FORBIDDEN = /expression\(|@import|javascript:|behavior:/i
const LOCAL_URL_OPEN = /url\(\s*['"]?\s*#/gi
const STYLE_FORBIDDEN =
/expression\(|image-set\(|@import|javascript:|behavior:/i
export interface SanitizeOutcome {
/** Removed element names and `element@attribute` pairs, in document order. */
@@ -157,11 +192,20 @@ function hrefIsLocal(value: string): boolean {
return LOCAL_REFERENCE.test(value.trim())
}
/** Any `(` left once the `url(#` openings are gone is a function we never allow. */
function callsOnlyLocalUrl(value: string): boolean {
return !value.replace(LOCAL_URL_OPEN, '').includes('(')
}
/** Whether an allowed attribute keeps its value, by name-specific rule. */
function attributeAcceptable(name: string, value: string): boolean {
if (value.includes('\\')) return false
if (!urlsAreLocal(value)) return false
if (name === 'href' || name === 'xlink:href') return hrefIsLocal(value)
if (name === 'style') return !STYLE_FORBIDDEN.test(value)
if (name === 'style') {
return !STYLE_FORBIDDEN.test(value) && callsOnlyLocalUrl(value)
}
if (PRESENTATION_ATTRIBUTES.has(name)) return callsOnlyLocalUrl(value)
return true
}
+11 -2
View File
@@ -27,6 +27,11 @@ export interface RenderedSvg {
size: RasterSize
}
/** A usable dimension: finite and strictly positive (`1e999` parses to Infinity). */
function isPositiveFinite(value: number | undefined): value is number {
return value !== undefined && Number.isFinite(value) && value > 0
}
/** Height over width of a `viewBox` value, or the fallback when unusable. */
function aspectOf(viewBox: string | null): number {
const parts = (viewBox ?? '')
@@ -34,10 +39,14 @@ function aspectOf(viewBox: string | null): number {
.split(/[\s,]+/)
.map(Number)
const [, , width, height] = parts
if (parts.length !== 4 || !(width! > 0) || !(height! > 0)) {
if (
parts.length !== 4 ||
!isPositiveFinite(width) ||
!isPositiveFinite(height)
) {
return ASPECT_FALLBACK
}
return height! / width!
return height / width
}
export function rasterSizeOf(viewBox: string | null): RasterSize {