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 -4
View File
@@ -1,4 +1,4 @@
import { PDFName, PDFArray, PDFString, type PDFPage } from 'pdf-lib'
import { PDFName, PDFHexString, type PDFPage } from 'pdf-lib'
export interface LinkRect {
x: number
@@ -8,14 +8,19 @@ export interface LinkRect {
url: string
}
/** A clickable `/Link` annotation opening `url`, appended to the page's Annots. */
/**
* A clickable `/Link` annotation opening `url`, appended to the page's
* Annots. The URI is written as a hex string: pdf-lib does not escape `(`,
* `)` or `\` in a literal string, so a hostile URL could otherwise close
* the literal and inject objects (a `/Next` JavaScript action, for one).
*/
export function addLinkAnnotation(page: PDFPage, rect: LinkRect): void {
const context = page.doc.context
const actionDict = context.obj({
Type: 'Action',
S: 'URI',
URI: PDFString.of(rect.url),
URI: PDFHexString.fromText(rect.url),
})
const annotDict = context.obj({
@@ -26,7 +31,7 @@ export function addLinkAnnotation(page: PDFPage, rect: LinkRect): void {
A: actionDict,
})
const annots = page.node.lookup(PDFName.of('Annots'), PDFArray)
const annots = page.node.Annots()
if (annots) {
annots.push(context.register(annotDict))
} else {