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

+9 -1
View File
@@ -10,11 +10,19 @@ import { z } from 'zod'
const KIB = 1024
const BASE64_PATTERN = /^[A-Za-z0-9+/]*={0,2}$/
/**
* `http(s)://` then printable ASCII without `"`, `'`, `(`, `)`, `<`, `>`
* or `\`: the URL is written into PDF link actions and must never be able
* to close a string literal or carry a control character. Expressed as the
* allowed ranges of printable ASCII (0x21-0x7e) minus those seven.
*/
const HTTP_URL_PATTERN = /^https?:\/\/[!#-&*-;=?-[\]-~]+$/
const HttpUrlSchema = z
.string()
.max(2048)
.url()
.refine((url) => /^https?:\/\//i.test(url), 'http(s) URL expected')
.regex(HTTP_URL_PATTERN, 'http(s) URL expected')
export const ImageAssetSchema = z.discriminatedUnion('kind', [
z
@@ -97,6 +97,47 @@ describe('BrandingSchema', () => {
})
})
/** Closes a PDF `(` string literal and chains an action: must never reach the engine. */
const INJECTION_URL =
'https://a.example/) /Next << /S /JavaScript /JS (app.alert\\(1\\)) >> /Z ('
describe('BrandingSchema URLs', () => {
it.each([
'https://faunosteo.com',
'https://zenquality.fr',
'https://www.instagram.com/paws.iin.harmony',
'http://localhost:5173',
'https://x.test/a-b_c~d?e=1&f=2#frag',
`https://x.test/${'a'.repeat(2048 - 'https://x.test/'.length)}`,
])('accepts %s', (url) => {
const branding = { ...sampleBranding(), siteUrl: url }
expect(BrandingSchema.safeParse(branding).success).toBe(true)
})
it.each([
['a closing parenthesis', 'https://a.example/)'],
['an opening parenthesis', 'https://a.example/('],
['a PDF action injection', INJECTION_URL],
['a backslash', 'https://a.example/a\\b'],
['a space', 'https://a.example/a b'],
['a tab', 'https://a.example/a\tb'],
['a control character', 'https://a.example/a\u0001b'],
['DEL', 'https://a.example/a\u007fb'],
['an angle bracket', 'https://a.example/<x>'],
['a double quote', 'https://a.example/"x'],
['a single quote', "https://a.example/'x"],
['a non-ASCII character', 'https://a.example/é'],
['no host', 'https://'],
['a scheme in upper case', 'HTTPS://a.example/'],
])('rejects %s in the site URL and in a credit URL', (_name, url) => {
const site = { ...sampleBranding(), siteUrl: url }
expect(BrandingSchema.safeParse(site).success).toBe(false)
const base = sampleBranding()
const credit = { ...base, partner: { ...base.partner!, url } }
expect(BrandingSchema.safeParse(credit).success).toBe(false)
})
})
describe('PdfLabelSetSchema', () => {
it('lists 25 distinct keys and accepts a complete set in both locales', () => {
expect(PDF_LABEL_KEYS).toHaveLength(25)