Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

web: sanitizer strips MathML <annotation>/<semantics> from rendered KaTeX — LaTeX source missing from DOM (copy-to-LaTeX tools broken)

Aperta Adatta ai principianti
#54,769 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Dante-dan ci sta già lavorando.

Dal 12/10/2026.

  • #54939 di @Dante-dan — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
frontend

Direzione di ricerca

Leggi packages/session-ui/src/components/markdown-cache.tsx ed esamina l’elenco ADD_TAGS nella configurazione del sanitizer. Conferma che l’output MathML venga sanificato lì, poi verifica che annotation e la relativa sorgente TeX rimangano nel DOM dopo la modifica; il lavoro è completato quando gli strumenti di copia basati su annotation possono leggere la sorgente e la matematica renderizzata continua a essere visualizzata correttamente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

reproduced
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-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
  1. 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}
    $$
    
  2. 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
    
  3. 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 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):

-  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 sanitizeMarkdown config: annotation count = 0
  • After adding the three tags: annotation count = 1, <annotation encoding="application/x-tex">…</annotation> intact
Lingua principale
TypeScript
Stelle
213k
Fork
28.4k
Merge medio
7h 22m
PR unite (30g)
276

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di anomalyco/opencode

Tutte le issue di anomalyco/opencode

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.