Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions pkgs/id/web/src/header-subtitle.test.ts
Original file line number Diff line number Diff line change
@@ -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 = '<img src=x onerror="alert(1)">.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</a><svg onload=alert(2)>";
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();
});
});
35 changes: 35 additions & 0 deletions pkgs/id/web/src/header-subtitle.ts
Original file line number Diff line number Diff line change
@@ -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";
}
}
17 changes: 1 addition & 16 deletions pkgs/id/web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = `// <a href="${lastFilePath}" data-nav>${lastFilename}</a>`;
} 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.
Expand Down
Loading