Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): only frame an absolute http(s) web view source

Elian Dorancbae79bfa2c9
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.ts
index 074f29c5c3..8318de331b 100644
--- a/apps/server/src/share/content_renderer.spec.ts
+++ b/apps/server/src/share/content_renderer.spec.ts
@@ -426,42 +426,50 @@ describe("content_renderer", () => {
expect(root.childNodes.length).toBe(0);
});
- it("passes through the source URLs a frame is allowed to load", () => {
- for (const src of [
- "https://example.com/page",
- "http://example.com/a?b=1&c=2",
- "/relative/path",
- "//example.com/protocol-relative"
+ it("loads an absolute http(s) source URL, normalising only its scheme and host", () => {
+ for (const [src, expected] of [
+ ["https://example.com/page", "https://example.com/page"],
+ ["http://example.com/a?b=1&c=2", "http://example.com/a?b=1&c=2"],
+ ["HTTPS://Example.com/Page", "https://example.com/Page"]
]) {
- expect(renderWebViewNote(src).frame?.getAttribute("src")).toBe(src);
+ expect(renderWebViewNote(src).frame?.getAttribute("src")).toBe(expected);
}
});
- it("substitutes a blank page for a source URL that would run script", () => {
- for (const src of ["javascript:alert(1)", "JaVaScRiPt:alert(1)", "data:text/html,<script>alert(1)</script>", "vbscript:msgbox"]) {
- expect(renderWebViewNote(src).frame?.attributes).toStrictEqual({
- class: "webview",
- src: "about:blank",
- sandbox: SANDBOX
- });
+ it("renders nothing for a source URL a frame has no business loading", () => {
+ // A web view frames a website, and the setup form only ever writes an absolute URL.
+ // Anything else reaches the label by another route — a hand-edited attribute, an
+ // import, ETAPI, a sync — and either cannot be framed at all or would frame this very
+ // server, which the sandbox's allow-same-origin would then not isolate from the page
+ // doing the framing.
+ for (const src of [
+ "/relative/path",
+ "//example.com/protocol-relative",
+ "./a",
+ "mailto:[email protected]",
+ "ftp://example.com/file",
+ "javascript:alert(1)",
+ "JaVaScRiPt:alert(1)",
+ "data:text/html,<script>alert(1)</script>",
+ "vbscript:msgbox",
+ "not a url at all"
+ ]) {
+ const { root, frame } = renderWebViewNote(src);
+ expect(frame).toBeNull();
+ expect(root.childNodes.length).toBe(0);
}
});
- it("keeps a quote-carrying source URL inside the src attribute instead of letting it open a new one", () => {
- // Sanitising the URL only normalises its scheme: a relative URL, or one whose scheme is
- // neither http nor https, comes back verbatim with its quotes intact. Placing such a
- // value in an attribute is what closes it, so it has to be escaped on the way in.
+ it("never lets a source URL add attributes of its own to the frame", () => {
+ // Whatever the URL carries, it can only ever be read back as the frame's source: an
+ // attribute value cannot end early and leave the rest of itself to be read as markup.
for (const src of [
- `./a" onload="alert(1)" data-x="`,
- `mailto:x@y" onload="alert(1)" data-x="`
+ `https://example.com/?a=" onload="alert(1)" data-x="`,
+ `https://example.com/#" onload="alert(1)" data-x="`
]) {
- // The full attribute set, so that nothing the value carries can be read as a
- // further attribute on the element and the whole of it stays the source.
- expect(renderWebViewNote(src).frame?.attributes).toStrictEqual({
- class: "webview",
- src,
- sandbox: SANDBOX
- });
+ expect(Object.keys(renderWebViewNote(src).frame?.attributes ?? {})).toStrictEqual([
+ "class", "src", "sandbox"
+ ]);
}
});
});
diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts
index 0f70ebbf5f..c231d91f0e 100644
--- a/apps/server/src/share/content_renderer.ts
+++ b/apps/server/src/share/content_renderer.ts
@@ -1,4 +1,4 @@
-import { extractYouTubeVideoId, safeLinkPreviewHref, safeLinkPreviewImageSrc } from "@triliumnext/commons";
+import { extractYouTubeVideoId, isHttpUrl, safeLinkPreviewHref, safeLinkPreviewImageSrc } from "@triliumnext/commons";
import { renderToHtml as renderMarkdownToHtml } from "@triliumnext/commons/src/lib/markdown_renderer.js";
import { renderSpreadsheetToHtml } from "@triliumnext/commons/src/lib/spreadsheet/render_to_html.js";
import { type BAttachment, type BBranch, becca, BNote, getLog, icon_packs as iconPackService, options, sanitize, task_states, utils } from "@triliumnext/core";
@@ -690,15 +690,23 @@ function renderSpreadsheet(result: Result) {
/**
* Renders a web view note as the frame that embeds its source.
*
+ * Only an absolute http(s) URL is framed, which is the source a web view is documented to take and
+ * the only one its setup form will write. Any other value reaches the label by another route — a
+ * hand-edited attribute, an import, ETAPI, a sync — and is either not framable at all or points at
+ * this very server, which `allow-same-origin` would then not isolate from the page framing it.
+ *
* The frame is built as an element rather than assembled as a string: `setAttribute()` escapes the
- * value it is handed, so a source URL is placed as a value and can only ever be read back as one.
- * Sanitising the URL alone would not be enough — it normalises the scheme, but returns a relative
- * URL, or one whose scheme is neither http nor https, verbatim with any quotes it carries intact.
+ * value it is handed, so the source is placed as a value and can only ever be read back as one.
*/
function renderWebView(note: SNote | BNote, result: Result) {
const url = note.getLabelValue("webViewSrc");
if (!url) return;
+ if (!isHttpUrl(url)) {
+ getLog().error(`Web view of shared note '${note.noteId}' not rendered: '${url}' is not an absolute http(s) URL.`);
+ return;
+ }
+
const frame = new HTMLElement("iframe", { class: "webview" });
frame.setAttribute("src", sanitize.sanitizeUrl(url));
// The embedded page keeps its own origin, may run scripts and may open windows. Keeping the