Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown export): render hostile link-preview URL schemes inert

Elian Doran28e031554799
diff --git a/packages/trilium-core/src/services/export/markdown.spec.ts b/packages/trilium-core/src/services/export/markdown.spec.ts
index 2ad224b8ce..4a37943c75 100644
--- a/packages/trilium-core/src/services/export/markdown.spec.ts
+++ b/packages/trilium-core/src/services/export/markdown.spec.ts
@@ -126,6 +126,21 @@ describe("Markdown export", () => {
expect(exported).toContain('<section class="link-embed" data-url="https://e.com/?a=1&amp;b=2" data-embed-type="opengraph"');
});
+ it("renders a hostile-scheme link preview URL inert in the fallback anchor", () => {
+ // `data-url` reaches export unsanitized (the sanitizers pass `data-*` through untouched), so a
+ // stored `javascript:` scheme must not become a live anchor in the exported Markdown.
+ // `safeLinkPreviewHref` maps it to `about:blank`; the escaping asserted in the test above still
+ // guards a valid http(s) URL that happens to contain a quote.
+ const exported = markdownExportService.toMarkdown(
+ '<section class="link-embed" data-url="javascript:alert(1)" data-embed-type="opengraph" data-title="Evil"></section>'
+ );
+
+ // The live fallback href is neutralised. The original scheme survives only in the inert
+ // `data-url` attribute (which round-trips losslessly on reimport), never as a linkable href.
+ expect(exported).toContain('<a href="about:blank">Evil</a>');
+ expect(exported).not.toContain('href="javascript:');
+ });
+
it("exports strikethrough text correctly", () => {
const html = "<s>hello</s>Hello <s>world</s>";
const expected = "~~hello~~Hello ~~world~~";
diff --git a/packages/trilium-core/src/services/export/markdown.ts b/packages/trilium-core/src/services/export/markdown.ts
index 2e54a877a4..0b28427794 100644
--- a/packages/trilium-core/src/services/export/markdown.ts
+++ b/packages/trilium-core/src/services/export/markdown.ts
@@ -1,4 +1,4 @@
-import { type TaskStateDef } from "@triliumnext/commons";
+import { safeLinkPreviewHref, type TaskStateDef } from "@triliumnext/commons";
import { ADMONITION_TYPE_MAPPINGS } from "@triliumnext/commons/src/lib/markdown_renderer.js";
import { gfm, serializeStructuralHtml } from "@triliumnext/turndown-plugin-gfm";
import escapeHtml from "escape-html";
@@ -255,7 +255,10 @@ function injectLinkPreviewFallbacks(content: string): string {
const url = element.getAttribute("data-url");
if (url) {
const title = element.getAttribute("data-title") || url;
- element.innerHTML = `<a href="${escapeHtml(url)}">${escapeHtml(title)}</a>`;
+ // `safeLinkPreviewHref` renders a hostile scheme (`javascript:`, `data:`) inert —
+ // stored `data-url` values reach here unsanitized (see its JSDoc); `escapeHtml` on top
+ // stops an otherwise-valid http(s) URL containing a quote from breaking out of the attribute.
+ element.innerHTML = `<a href="${escapeHtml(safeLinkPreviewHref(url))}">${escapeHtml(title)}</a>`;
}
}