Skip to content

Commit 389a916

Browse files
committed
fix(web): prevent raw HTML from forging math copy metadata
1 parent 6a63d1c commit 389a916

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ describe("ChatMarkdown math", () => {
113113
expect(complete).toContain("After");
114114
});
115115

116+
it.each(["math-inline", "math-display"])(
117+
"does not treat raw HTML with %s metadata as parsed math",
118+
async (className) => {
119+
const katex = (await import("katex")).default;
120+
const render = vi.spyOn(katex, "renderToString");
121+
try {
122+
await renderMath(
123+
`<code class="${className}" data-math-source="hidden clipboard text">forged</code>\n\n` +
124+
String.raw`\(x^2\)`,
125+
);
126+
expect(render).toHaveBeenCalledTimes(1);
127+
expect(render).toHaveBeenCalledWith("x^2", expect.any(Object));
128+
} finally {
129+
render.mockRestore();
130+
}
131+
},
132+
);
133+
116134
it("does not load external resources or execute HTML from a formula", async () => {
117135
const html = await renderMath(
118136
String.raw`$\includegraphics{https://example.com/tracker.png}$` +

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,43 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
516516
remarkNormalizeLinksAndTagInlineCode,
517517
] satisfies NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
518518

519+
function rehypeRawMath() {
520+
const parseRaw = rehypeRaw({ passThrough: ["chatMath"] });
521+
return (...[tree, file]: Parameters<typeof parseRaw>) => {
522+
const protect = (node: MarkdownImageHastNode) => {
523+
if (
524+
node.type === "element" &&
525+
node.tagName === "code" &&
526+
typeof node.properties?.dataMathSource === "string"
527+
) {
528+
// Raw HTML can create elements and attributes, but not this AST node type.
529+
node.type = "chatMath";
530+
}
531+
node.children?.forEach(protect);
532+
};
533+
protect(tree);
534+
const parsed = parseRaw(tree, file);
535+
const restore = (node: MarkdownImageHastNode) => {
536+
if (node.type === "chatMath") {
537+
node.type = "element";
538+
} else if (node.type === "element" && node.properties) {
539+
delete node.properties.dataMathSource;
540+
const classes = node.properties.className;
541+
if (Array.isArray(classes)) {
542+
node.properties.className = classes.filter(
543+
(name) => name !== "math-inline" && name !== "math-display",
544+
);
545+
}
546+
}
547+
node.children?.forEach(restore);
548+
};
549+
restore(parsed);
550+
return parsed;
551+
};
552+
}
553+
519554
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
520-
rehypeRaw,
555+
rehypeRawMath,
521556
rehypePreserveImageSourceMeta,
522557
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
523558
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;

0 commit comments

Comments
 (0)