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(notes): harden srcset strip against mismatched quotes; tighten tests
Elian Doran077e2e6563cf
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.tsindex 5366611ed9..0025493247 100644--- a/packages/trilium-core/src/services/notes.spec.ts+++ b/packages/trilium-core/src/services/notes.spec.ts@@ -353,7 +353,22 @@ describe("notes service (real DB)", () => {const { content: newContent } = getContext().init(() => saveLinks(source.note, content));- expect(newContent).toContain("srcset=");+ // Assert the full srcset value survived intact, not merely that a srcset= token is present.+ expect(newContent).toContain(`srcset="https://example.com/a.png 1290w, https://example.com/a-300.png 300w"`);+ });++ it("strips a srcset containing the opposite quote character without corrupting the tag", () => {+ const source = createNote("root", { title: "spec-srcset-quote" });++ // A single quote inside the double-quoted srcset value must not truncate the strip+ // (a naive [^"']* would stop at the apostrophe and leave a dangling fragment).+ const content =+ `<p><img src="api/attachments/aBc/image/x.png" ` ++ `srcset="https://example.com/it's-a-photo.png 1x"> after</p>`;++ const { content: newContent } = getContext().init(() => saveLinks(source.note, content));++ expect(newContent).toBe(`<p><img src="api/attachments/aBc/image/x.png"> after</p>`);});it("extracts an inline base64 attachment, deriving its title via prepareTitle", () => {diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex c5b954e281..20b7cb0142 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -980,11 +980,13 @@ function saveAttachments(note: BNote, content: string) {*/function stripStaleSrcset(content: string): string {return content.replace(/<img\b[^>]*>/gi, (imgTag) => {- if (!/\ssrc=["']api\/attachments\/[^"']+["']/i.test(imgTag)) {+ // Match quotes as balanced pairs (and allow optional whitespace around `=`) so a value+ // containing the opposite quote character doesn't truncate the match and corrupt the tag.+ if (!/\ssrc\s*=\s*(?:"api\/attachments\/[^"]+"|'api\/attachments\/[^']+')/i.test(imgTag)) {return imgTag;}- return imgTag.replace(/\s(?:srcset|sizes)=["'][^"']*["']/gi, "");+ return imgTag.replace(/\s(?:srcset|sizes)\s*=\s*(?:"[^"]*"|'[^']*')/gi, "");});}