Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(pdfjs): restore CSP relaxation so custom viewer styling applies
Elian Doranceecde112684
diff --git a/packages/pdfjs-viewer/scripts/update-viewer.ts b/packages/pdfjs-viewer/scripts/update-viewer.tsindex 28bb960a27..b6abd4d5de 100644--- a/packages/pdfjs-viewer/scripts/update-viewer.ts+++ b/packages/pdfjs-viewer/scripts/update-viewer.ts@@ -47,9 +47,31 @@ async function main() {async function patchViewerHTML() {const viewerPath = join(__dirname, "../viewer/viewer.html");let content = await fs.readFile(viewerPath, "utf-8");- content = content.replace(` <link rel="stylesheet" href="viewer.css" />`, ` <link rel="stylesheet" href="viewer.css" />\n <link rel="stylesheet" href="custom.css" />`);- content = content.replace(` <script src="viewer.mjs" type="module"></script>`, ` <script src="custom.mjs" type="module"></script>\n <script src="viewer.mjs" type="module"></script>`);++ // Inject the Trilium custom stylesheet and script alongside the upstream ones.+ content = patch(content, ` <link rel="stylesheet" href="viewer.css" />`, ` <link rel="stylesheet" href="viewer.css" />\n <link rel="stylesheet" href="custom.css" />`);+ content = patch(content, ` <script src="viewer.mjs" type="module"></script>`, ` <script src="custom.mjs" type="module"></script>\n <script src="viewer.mjs" type="module"></script>`);++ // Relax the upstream CSP so the injected <style> elements are allowed.+ // Trilium injects inline <style> elements (theme CSS variables, fonts) from+ // the parent client, and the print service injects an inline @page style at+ // print time — both are dynamic and can't be pre-hashed. Upstream ships+ // style-src 'self' (which blocks all inline <style> elements), so we add+ // style-src-elem 'self' 'unsafe-inline'. Without this the custom.css theme+ // variables resolve to nothing and the viewer styling breaks completely.+ content = patch(content, `style-src 'self';`, `style-src 'self'; style-src-elem 'self' 'unsafe-inline';`);+await fs.writeFile(viewerPath, content, "utf-8");}+/** Applies a single string replacement, throwing if the target was not found so a+ * silently-missed patch (e.g. after an upstream markup change) fails the update+ * instead of shipping a broken viewer. */+function patch(content: string, target: string, replacement: string) {+ if (!content.includes(target)) {+ throw new Error(`patchViewerHTML: could not find expected snippet to patch:\n${target}`);+ }+ return content.replace(target, replacement);+}+main();diff --git a/packages/pdfjs-viewer/viewer/viewer.html b/packages/pdfjs-viewer/viewer/viewer.htmlindex aaddee5be5..d231f0be1e 100644--- a/packages/pdfjs-viewer/viewer/viewer.html+++ b/packages/pdfjs-viewer/viewer/viewer.html@@ -27,9 +27,18 @@ See https://github.com/adobe-type-tools/cmap-resources<meta name="google" content="notranslate" /><title>PDF.js viewer</title>+ <!--+ Trilium injects inline <style> elements into this document (theme CSS+ variables, fonts) from the parent client, and the print service injects+ an inline <style>@page { size: … }</style> at print time+ (web/pdf_print_service.js, web/firefox_print_service.js) to match the+ PDF's page dimensions. That content is dynamic and can't be pre-hashed,+ so style-src-elem allows 'unsafe-inline'. Inline style="…" attributes+ stay blocked via style-src (no fallback).+ --><metahttp-equiv="Content-Security-Policy"- content="default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self'; img-src 'self' blob: data:; media-src blob:; font-src 'self' data:; connect-src * blob: data:; base-uri 'none'; form-action 'none';"+ content="default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; style-src 'self'; style-src-elem 'self' 'unsafe-inline'; img-src 'self' blob: data:; media-src blob:; font-src 'self' data:; connect-src * blob: data:; base-uri 'none'; form-action 'none';"/><!-- This snippet is used in production (included from viewer.html) -->