SAFE_SRC in ui/src/chat/markdown.ts now keeps only same-origin root-relative paths and data:image/ URLs, rejecting http(s), protocol-relative and backslash-after-slash forms that the browser would read as a remote host. The shared parser also covers task approvals, plans and run transcripts. modules/dashboard sets Content-Security-Policy: img-src 'self' data: on every non-API response (page, assets, errors, no-build fallback) as the browser backstop; the /api/ JSON fallback is unchanged. No proxy, no migration: stored replies re-render from their text.
47 lines
2.1 KiB
TypeScript
47 lines
2.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import type { Element, Root } from "hast";
|
|
|
|
import { parseMarkdown } from "../src/chat/markdown.ts";
|
|
|
|
function elements(tree: Root | Element, tag: string): Element[] {
|
|
const found: Element[] = [];
|
|
for (const child of tree.children) {
|
|
if (child.type !== "element") continue;
|
|
if (child.tagName === tag) found.push(child);
|
|
found.push(...elements(child, tag));
|
|
}
|
|
return found;
|
|
}
|
|
|
|
// Guards the chat renderer against script URLs and against #N task links
|
|
// appearing inside code or existing links.
|
|
test("parseMarkdown keeps only safe URLs and links task references in prose", () => {
|
|
const cases = [
|
|
{ md: "[x](javascript:alert(1))", tag: "a", count: 0 },
|
|
{ md: "[x](https://example.com)", tag: "a", count: 1 },
|
|
{ md: "[x](//evil.example)", tag: "a", count: 0 },
|
|
{ md: ")", tag: "img", count: 0 },
|
|
{ md: "", tag: "img", count: 1 },
|
|
// Remote and remote-interpretable image URLs must not survive: the
|
|
// browser would fetch them, leaking data in the query string (#2073).
|
|
{ md: "", tag: "img", count: 0 },
|
|
{ md: "", tag: "img", count: 0 },
|
|
{ md: "", tag: "img", count: 0 },
|
|
// remark percent-encodes a backslash in a destination, so /\evil stays a
|
|
// same-origin path (/%5C...); SAFE_SRC still rejects a literal /\ form.
|
|
{ md: "", tag: "img", count: 1 },
|
|
// Allowed controls: same-origin root-relative path and inline data image.
|
|
{ md: "", tag: "img", count: 1 },
|
|
{ md: "<script>alert(1)</script>", tag: "script", count: 0 },
|
|
{ md: "See #12 and #13.", tag: "task-ref", count: 2 },
|
|
{ md: "`#12` and [#13](https://example.com)", tag: "task-ref", count: 0 },
|
|
{ md: "```\n#12\n```", tag: "task-ref", count: 0 },
|
|
{ md: "issue#12 and a/#12", tag: "task-ref", count: 0 },
|
|
];
|
|
for (const { md, tag, count } of cases) {
|
|
assert.equal(elements(parseMarkdown(md), tag).length, count, md);
|
|
}
|
|
});
|