diff --git a/pkgs/id/web/src/header-subtitle.test.ts b/pkgs/id/web/src/header-subtitle.test.ts new file mode 100644 index 0000000000..f3a67f0566 --- /dev/null +++ b/pkgs/id/web/src/header-subtitle.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, it } from "vitest"; +import { updateHeaderSubtitle } from "./header-subtitle"; + +function renderSubtitle(filename: string, path: string): HTMLElement { + document.body.innerHTML = ""; + const subtitle = document.createElement("div"); + subtitle.id = "header-subtitle"; + document.body.appendChild(subtitle); + updateHeaderSubtitle(filename, path, true); + return subtitle; +} + +describe("header subtitle", () => { + it("renders a markup-carrying filename as literal text, not elements", () => { + const label = '.md'; + const subtitle = renderSubtitle(label, "/edit/" + label); + + expect(subtitle.querySelector("img")).toBeNull(); + const link = subtitle.querySelector("a"); + expect(link).not.toBeNull(); + expect(link?.textContent).toBe(label); + expect(link?.getAttribute("data-nav")).toBe(""); + expect(subtitle.querySelectorAll("a")).toHaveLength(1); + }); + + it("cannot be broken out of by an anchor-closing filename", () => { + const label = "x"; + const subtitle = renderSubtitle(label, "/edit/x"); + + expect(subtitle.querySelector("svg")).toBeNull(); + expect(subtitle.querySelectorAll("a")).toHaveLength(1); + expect(subtitle.querySelector("a")?.textContent).toBe(label); + }); + + it("renders the benign control as one anchor with the file's text and href", () => { + const subtitle = renderSubtitle("notes.md", "/edit/notes.md"); + + const link = subtitle.querySelector("a"); + expect(link).not.toBeNull(); + expect(link?.textContent).toBe("notes.md"); + expect(link?.getAttribute("href")).toBe("/edit/notes.md"); + expect(subtitle.textContent).toBe("// notes.md"); + }); + + it("keeps quotes in the path inside the href attribute", () => { + const path = '/edit/a" onmouseover="alert(3)'; + const subtitle = renderSubtitle("notes.md", path); + + expect(subtitle.querySelectorAll("a")).toHaveLength(1); + const link = subtitle.querySelector("a"); + expect(link?.getAttribute("href")).toBe(path); + expect(link?.hasAttribute("onmouseover")).toBe(false); + }); + + it("falls back to the plain subtitle without history", () => { + document.body.innerHTML = ""; + const subtitle = document.createElement("div"); + subtitle.id = "header-subtitle"; + document.body.appendChild(subtitle); + + updateHeaderSubtitle("notes.md", "/edit/notes.md", false); + + expect(subtitle.textContent).toBe("// p2p file sharing"); + expect(subtitle.querySelector("a")).toBeNull(); + }); +}); diff --git a/pkgs/id/web/src/header-subtitle.ts b/pkgs/id/web/src/header-subtitle.ts new file mode 100644 index 0000000000..649b4db5ed --- /dev/null +++ b/pkgs/id/web/src/header-subtitle.ts @@ -0,0 +1,35 @@ +/** + * Header subtitle rendering. + * + * Shows "p2p file sharing" on initial load, or the last-opened document's + * filename as a link after navigation. + * + * The filename is untrusted document metadata (it arrives from the + * server's URL-encoded data-filename attribute, decoded back to the raw + * name in main.ts), so the link is built with DOM APIs: the name goes in + * via textContent and the href via setAttribute, never string-interpolated + * into innerHTML. A crafted name carrying markup (for example an img tag + * with an onerror handler) therefore renders as literal text instead of + * live markup (#1457). The href is set as an attribute (not the href + * property) so the SPA router's getAttribute("href") read keeps the exact + * path string it has always navigated to. + */ +export function updateHeaderSubtitle( + lastFilename: string | null, + lastFilePath: string | null, + hasHistory: boolean, +): void { + const subtitle = document.getElementById("header-subtitle"); + if (!subtitle) return; + + if (lastFilename && lastFilePath && hasHistory) { + // Create a link to the last file + const link = document.createElement("a"); + link.setAttribute("data-nav", ""); + link.setAttribute("href", lastFilePath); + link.textContent = lastFilename; + subtitle.replaceChildren(document.createTextNode("// "), link); + } else { + subtitle.textContent = "// p2p file sharing"; + } +} diff --git a/pkgs/id/web/src/main.ts b/pkgs/id/web/src/main.ts index 78c6965999..a994f606ff 100644 --- a/pkgs/id/web/src/main.ts +++ b/pkgs/id/web/src/main.ts @@ -10,6 +10,7 @@ import "./world"; import { type CollabConnection, initCollab } from "./collab"; import { type EditorInstance, getEditorState } from "./editor"; import { identityNeedsRefresh } from "./identity-refresh"; +import { updateHeaderSubtitle } from "./header-subtitle"; import { cycleTheme, initTheme, setTheme, type Theme } from "./theme"; /** Result from saveFile() for AutoSaveManager to process */ @@ -537,22 +538,6 @@ function initScrollShowHeader( }; } -/** - * Update header subtitle based on navigation state. - * Shows "p2p file sharing" on initial load, or last filename as link after navigation. - */ -function updateHeaderSubtitle(lastFilename: string | null, lastFilePath: string | null, hasHistory: boolean): void { - const subtitle = document.getElementById("header-subtitle"); - if (!subtitle) return; - - if (lastFilename && lastFilePath && hasHistory) { - // Create a link to the last file - subtitle.innerHTML = `// ${lastFilename}`; - } else { - subtitle.textContent = "// p2p file sharing"; - } -} - /** * Update back link based on app navigation history. * If there's history, use SPA navigation. Otherwise, grey out but still allow browser back.