You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
web: sanitizer strips MathML <annotation>/<semantics> from rendered KaTeX — LaTeX source missing from DOM (copy-to-LaTeX tools broken) #54769
In the web UI, rendered math looks correct, but the LaTeX source is absent from the DOM. KaTeX produces its default output (htmlAndMathml, i.e. HTML + MathML with <semantics><annotation encoding="application/x-tex">), but the HTML is passed through sanitizeMarkdown() before insertion, and the sanitizer's mathMl profile strips <semantics> / <annotation>. Consequences:
Browser extensions / userscripts that copy the original TeX (the KaTeX contrib/copy-tex ecosystem: "Copy LaTeX", "Formula Copy", etc.) cannot work on the web UI, while they work on katex.org / other chat UIs.
MathML semantics are lost for assistive technology and other DOM consumers.
Tested on OpenCode v2.0.25 (web UI, Chrome; served bundle _assets/markdown.worker-*.js contains marked + KaTeX 0.16.47). The same config is still present in dev.
Steps to reproduce
Open the web UI, ask the model for a display formula, e.g.:
With the formulas rendered, run in DevTools console:
document.querySelectorAll(".katex").length// > 0 — math is rendereddocument.querySelectorAll('annotation[encoding="application/x-tex"]').length// 0 — but the LaTeX source is not in the DOM
Or install any annotation-based copy extension (e.g. "Copy LaTeX (KaTeX & MathJax)"): it does nothing on OpenCode, while working on katex.org.
Expected
The DOM should retain KaTeX's MathML output, including <annotation encoding="application/x-tex"> with the original TeX (KaTeX's default behavior; what other AI chat UIs expose).
Actual
<semantics> and <annotation> are removed by sanitization. Visual rendering is unaffected (the HTML part is kept), so the loss is invisible until you try to copy a formula's source.
DOMPurify's built-in mathMl profile allowlist does not include semantics / annotation / annotation-xml, so the MathML annotation (and its encoding attribute) is dropped.
Suggested fix
Extend the allowlist (verified locally with the exact config above on doMPurify 3.4.16 — the annotation then survives sanitization and is queryable in the DOM):
No ADD_ATTR change is needed (encoding is already in DOMPurify's MathML attribute allowlist). The elements are plain text containers; allowing them carries no scripting risk.
Local verification (before/after)
Worker output: contains application/x-tex ✓
After sanitizeMarkdown config: annotation count = 0
After adding the three tags: annotation count = 1, <annotation encoding="application/x-tex">…</annotation> intact
Thanks for the detailed report. I can confirm this.
I rendered a display formula with KaTeX 0.16.47, using the same renderToString options the web UI uses. I then sanitized it with the exact DOMPurify config from packages/session-ui/src/components/markdown-cache.tsx, on DOMPurify 3.4.14 (the version pinned in the package).
I’m working on this. The sanitizer currently retains KaTeX’s SVG output but drops its MathML semantics and TeX annotation. My patch preserves those two MathML elements without enabling annotation-xml or changing the attribute allowlist; I’m checking it against the existing markdown and sanitization tests.
Activity
opencode-agent commentedon Oct 12, 2026
Thanks for the detailed report. I can confirm this.
I rendered a display formula with KaTeX 0.16.47, using the same
renderToStringoptions the web UI uses. I then sanitized it with the exact DOMPurify config frompackages/session-ui/src/components/markdown-cache.tsx, on DOMPurify 3.4.14 (the version pinned in the package).annotation[encoding="application/x-tex"].katexHTML is kept.semantics,annotationandannotation-xmladded toADD_TAGS: 1I got the same result with the v2.0.25 config and with the latest
dev(a349618), where the config hasn't changed. The script below reproduces it.Related: #25866, an earlier change that added SVG tags to the same sanitizer config so KaTeX would render.
Reproduction script (repro.mjs)
Run it:
bun repro.mjsDante-dan commentedon Oct 12, 2026
I’m working on this. The sanitizer currently retains KaTeX’s SVG output but drops its MathML semantics and TeX annotation. My patch preserves those two MathML elements without enabling annotation-xml or changing the attribute allowlist; I’m checking it against the existing markdown and sanitization tests.