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(pdf): abort signature save on malformed payload; harden message guard
Elian Dorana77ed74dc58a
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsxindex 4630c060a4..7d25ecd7e3 100644--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx@@ -71,7 +71,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {}}- if (event.data.type === "pdfjs-viewer-save-signatures" && event.data?.data) {+ if (event.data?.type === "pdfjs-viewer-save-signatures" && event.data?.data) {// The signature library is global (not per-note), but scope the write to this// viewer instance so multiple open PDFs don't each re-save the same payload.if (event.data.noteId === note.noteId && event.data.ntxId === noteContext.ntxId) {diff --git a/packages/pdfjs-viewer/src/persistence.spec.ts b/packages/pdfjs-viewer/src/persistence.spec.tsindex 37d781f384..8dcdf95366 100644--- a/packages/pdfjs-viewer/src/persistence.spec.ts+++ b/packages/pdfjs-viewer/src/persistence.spec.ts@@ -80,6 +80,18 @@ describe("signature-library interception", () => {expect(readRealStore(SIGNATURE_KEY)).toBeNull();});+ it("aborts on a malformed payload instead of forwarding broken JSON", () => {+ window.TRILIUM_SIGNATURES = { abc: ENTRY };+ interceptPersistence();++ // A non-JSON value must not be forwarded — persisting it would make the parent's getJson+ // fall back to {} and wipe every saved signature. The prior library stays untouched.+ localStorage.setItem(SIGNATURE_KEY, "not json{");++ expect(postMessageSpy).not.toHaveBeenCalled();+ expect(window.TRILIUM_SIGNATURES).toEqual({ abc: ENTRY });+ });+it("leaves unrelated keys and the view-history channel working", () => {interceptPersistence();diff --git a/packages/pdfjs-viewer/src/persistence.ts b/packages/pdfjs-viewer/src/persistence.tsindex ee44c36606..7168b2cd88 100644--- a/packages/pdfjs-viewer/src/persistence.ts+++ b/packages/pdfjs-viewer/src/persistence.ts@@ -62,7 +62,7 @@ function saveHistory(value: string) {}/**- * Persists the reusable signature library. pdf.js writes the full library to `localStorage` on every+ * Persists the reusable signature library. pdf.js writes the library to `localStorage` on every* add/remove; we mirror it into the injected `TRILIUM_SIGNATURES` global (so in-session reads stay* consistent) and forward it to the parent, which stores it in the synced `pdfSignatures` option.* These are discrete user actions, so — unlike view history — no debounce is needed.@@ -71,7 +71,10 @@ function saveSignatures(value: string) {try {window.TRILIUM_SIGNATURES = JSON.parse(value);} catch {- // Malformed payload should never reach here; keep the previous in-memory copy if it does.+ // Malformed payload should never reach here (pdf.js always serializes valid JSON). If it+ // somehow does, abort rather than forward it — persisting broken JSON into the synced+ // option would make `getJson` fall back to `{}` and silently wipe every saved signature.+ return;}window.parent.postMessage({