web: sanitizer strips MathML <annotation>/<semantics> from rendered KaTeX — LaTeX source missing from DOM (copy-to-LaTeX tools broken)
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 77/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- frontend
Research direction
Read packages/session-ui/src/components/markdown-cache.tsx and inspect the sanitizer config’s ADD_TAGS list. Confirm the MathML output is sanitized there, then check that annotation and its TeX source remain in the DOM after the change; done when annotation based copy tools can read the source while rendered math still displays correctly.
Written by the indexing model from the issue text.
Description
Summary
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-texecosystem: "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.:
$$ \begin{aligned} (a+b)^3 &= (a+b)(a+b)^2 \\ &= a^3 + 3a^2 b + 3ab^2 + b^3 . \end{aligned} $$ -
With the formulas rendered, run in DevTools console:
document.querySelectorAll(".katex").length // > 0 — math is rendered document.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.
Root cause
-
packages/session-ui/src/components/markdown-cache.tsx:const config = { USE_PROFILES: { html: true, mathMl: true }, SANITIZE_NAMED_PROPS: true, FORBID_TAGS: ["style"], FORBID_CONTENTS: ["style", "script"], ADD_TAGS: ["svg", "path"], ADD_ATTR: ["d", "viewBox", "preserveAspectRatio", "xmlns", "target"], } -
DOMPurify's built-in
mathMlprofile allowlist does not includesemantics/annotation/annotation-xml, so the MathML annotation (and itsencodingattribute) 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):
- ADD_TAGS: ["svg", "path"],
+ ADD_TAGS: ["svg", "path", "semantics", "annotation", "annotation-xml"],
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
sanitizeMarkdownconfig:annotationcount = 0 - After adding the three tags:
annotationcount = 1,<annotation encoding="application/x-tex">…</annotation>intact
- Dominant language
- TypeScript
- Stars
- 213k
- Forks
- 28.4k
- Avg merge
- 8h 32m
- Merged PRs (30d)
- 253
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from anomalyco/opencode
-
app: keybinds match the produced character, so Ctrl+F and other letter shortcuts fail on non-Latin layoutsPossibly taken A pull request linked to this issue is open or already merged. Openreproduced
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
anomalyco/opencode#55676 · 1 comment ·
Maintainers usually reply within 1 day
-
tui: NumPad End/Home ignored under kitty keyboard protocol (Windows Terminal reports KP_END 57424)Possibly taken @argszero claimed this 1 day ago. Openreproduced
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
anomalyco/opencode#55533 · 2 comments ·
Maintainers usually reply within 1 day
-
pending close triaging
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
anomalyco/opencode#55392 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
anomalyco/opencode#55383 · 3 comments ·
Maintainers usually reply within 1 day
-
web: second Firefox tab never connects to /api/event - Cache-Control: no-cache lets Firefox's HTTP cache block concurrent SSE requestsPossibly taken @zonemeen claimed this 1 day ago. Openreproduced
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
anomalyco/opencode#55369 · 2 comments ·
Maintainers usually reply within 1 day
All issues in anomalyco/opencode
Similar issues
-
area:ui effort:S enhancement priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
QuantumNous/new-api#7839 ·
Maintainers usually reply within 1 day
-
good first issue maintenance
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
dark-princezz/stellar-hooks#1010 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
interledger/rhyza#352 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day