Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): keep a web view source URL inside the src attribute

Elian Doranb139677e37b7
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.ts
index 7fbbf42c40..074f29c5c3 100644
--- a/apps/server/src/share/content_renderer.spec.ts
+++ b/apps/server/src/share/content_renderer.spec.ts
@@ -1,4 +1,5 @@
import { trimIndentation } from "@triliumnext/commons";
+import { parse } from "node-html-parser";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { buildShareNote, buildShareNotes } from "../test/shaca_mocking.js";
@@ -390,6 +391,81 @@ describe("content_renderer", () => {
});
});
+ describe("Web view note", () => {
+ const SANDBOX = "allow-same-origin allow-scripts allow-popups";
+
+ /**
+ * Renders a web view note and reads back the element the share page ends up with, so a test
+ * can assert the whole of it — every attribute the browser sees, and nothing besides.
+ */
+ function renderWebViewNote(src?: string) {
+ const note = buildShareNote({
+ type: "webView",
+ content: "",
+ ...(src !== undefined ? { "#webViewSrc": src } : {})
+ });
+ const root = parse(String(getContent(note).content));
+ return { root, frame: root.querySelector("iframe") };
+ }
+
+ it("renders a frame carrying the source URL, and nothing else", () => {
+ const { root, frame } = renderWebViewNote("https://example.com/page");
+ expect(root.childNodes.length).toBe(1);
+ expect(frame?.rawTagName).toBe("iframe");
+ expect(frame?.attributes).toStrictEqual({
+ class: "webview",
+ src: "https://example.com/page",
+ sandbox: SANDBOX
+ });
+ expect(frame?.innerHTML).toBe("");
+ });
+
+ it("renders nothing at all when the note carries no source", () => {
+ const { root, frame } = renderWebViewNote();
+ expect(frame).toBeNull();
+ 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"
+ ]) {
+ expect(renderWebViewNote(src).frame?.getAttribute("src")).toBe(src);
+ }
+ });
+
+ 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("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.
+ for (const src of [
+ `./a" onload="alert(1)" data-x="`,
+ `mailto:x@y" 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
+ });
+ }
+ });
+ });
+
describe("renderCode", () => {
it("identifies empty content", () => {
const emptyResult: Result = {
diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts
index 369c192902..0f70ebbf5f 100644
--- a/apps/server/src/share/content_renderer.ts
+++ b/apps/server/src/share/content_renderer.ts
@@ -687,15 +687,29 @@ function renderSpreadsheet(result: Result) {
}
}
+/**
+ * Renders a web view note as the frame that embeds its source.
+ *
+ * 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.
+ */
function renderWebView(note: SNote | BNote, result: Result) {
const url = note.getLabelValue("webViewSrc");
if (!url) return;
- result.content = `<iframe class="webview" src="${sanitize.sanitizeUrl(url)}" sandbox="allow-same-origin allow-scripts allow-popups"></iframe>`;
+ 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
+ // origin is what lets the pages a web view is normally pointed at use their cookies and
+ // storage, but it also means a page served from this very origin is not isolated from the page
+ // embedding it; only dropping allow-same-origin would isolate it.
+ frame.setAttribute("sandbox", "allow-same-origin allow-scripts allow-popups");
+ result.content = frame.toString();
}
-
function safeHostnameForShare(url: string): string {
try { return new URL(url).hostname; } catch { return url; }
}
diff --git a/apps/server/src/test/shaca_mocking.ts b/apps/server/src/test/shaca_mocking.ts
index d8d4b49ddd..08c77e4a0a 100644
--- a/apps/server/src/test/shaca_mocking.ts
+++ b/apps/server/src/test/shaca_mocking.ts
@@ -63,8 +63,9 @@ export function buildShareNote(noteDef: NoteDefinition) {
]);
shaca.notes[note.noteId] = note;
- // Handle content
- if (noteDef.content) {
+ // Handle content: an explicitly empty content is still a content, as note types that keep
+ // everything in their attributes (web view) have nothing else to return.
+ if (noteDef.content !== undefined) {
note.getContent = () => {
if (noteDef.isProtected) return undefined;
return noteDef.content;