The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound raw HTML nesting before it reaches rehype-raw
Brooklyn Nicholson1641512c94c4
diff --git a/apps/desktop/src/lib/markdown-html-depth.test.ts b/apps/desktop/src/lib/markdown-html-depth.test.tsnew file mode 100644index 00000000000..d58dbcbbe2f--- /dev/null+++ b/apps/desktop/src/lib/markdown-html-depth.test.ts@@ -0,0 +1,69 @@+import { describe, expect, it } from 'vitest'++import { clampHtmlNestingDepth } from './markdown-html-depth'++// The clamp exists to keep parse5 / hast-util-from-parse5 recursion off the+// call stack. Its contract is about DEPTH of unclosed elements, so the tests+// assert that relationship rather than any particular output string.+describe('clampHtmlNestingDepth', () => {+ it('returns text without tags unchanged, by identity', () => {+ const text = 'C:\\Windows\\System32 is 12 GB, and 5 < 7 is true'++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })++ it('leaves ordinary markup untouched, by identity', () => {+ const text = '<div class="a"><p>A <b>bold</b> claim</p></div>'++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })++ it('leaves a wide but shallow tag run untouched — count is not the risk', () => {+ const text = '<b>x</b>'.repeat(20_000)++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })++ it('leaves void elements untouched — they never add depth', () => {+ const text = '<br>'.repeat(20_000)++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })++ it('leaves self-closing tags untouched — they never add depth', () => {+ const text = '<custom-el />'.repeat(20_000)++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })++ it('escapes an unbounded run of unclosed unknown tags', () => {+ const clamped = clampHtmlNestingDepth('Let' + '<unk>'.repeat(16_383))++ // Nothing is dropped: the text is still all there, just literal. Only the+ // opening `<` needs escaping — that alone stops parse5 opening an element.+ expect(clamped).toContain('<unk>')+ expect(clamped.startsWith('Let<unk>')).toBe(true)+ })++ it('escapes an unbounded run of unclosed known tags', () => {+ const clamped = clampHtmlNestingDepth('<div>'.repeat(5_000))++ expect(clamped).toContain('<div>')+ })++ it('keeps the prefix below the cap parseable and only escapes past it', () => {+ const clamped = clampHtmlNestingDepth('<unk>'.repeat(5_000))+ const parseable = clamped.slice(0, clamped.indexOf('<'))++ // Everything before the overflow point is untouched real markup...+ expect(parseable).toBe('<unk>'.repeat(parseable.length / '<unk>'.length))+ // ...and it is bounded, which is the entire point.+ expect(parseable.length).toBeLessThan('<unk>'.repeat(5_000).length)+ })++ it('does not accumulate depth when tags close, however many there are', () => {+ const text = '<div>x</div>'.repeat(10_000)++ expect(clampHtmlNestingDepth(text)).toBe(text)+ })+})diff --git a/apps/desktop/src/lib/markdown-html-depth.ts b/apps/desktop/src/lib/markdown-html-depth.tsnew file mode 100644index 00000000000..3cb8fcf6fe8--- /dev/null+++ b/apps/desktop/src/lib/markdown-html-depth.ts@@ -0,0 +1,112 @@+/**+ * Bound the nesting depth of raw inline HTML before it reaches `rehype-raw`.+ *+ * Streamdown parses assistant markdown with `allowDangerousHtml` + `rehype-raw`,+ * so any `<tag>` run in the text is handed to parse5. `hast-util-from-parse5`+ * then walks the resulting tree with mutual recursion (`element → one → all`),+ * one JS frame per level of nesting — so a deeply nested run of UNCLOSED tags+ * overflows the call stack and throws `RangeError: Maximum call stack size+ * exceeded` out of the middle of a React render.+ *+ * That is not a hypothetical: a degenerating model emits thousands of `<unk>`+ * tokens as its reasoning, every one of which parse5 opens as another element+ * that never closes. Measured against our pinned deps, the throw starts at+ * ~1,750 consecutive unclosed tags and the payload persists to the session, so+ * every reload re-renders it — the crash repeats until the message is deleted.+ *+ * The bound is on DEPTH, not size: 20,000 balanced `<b>x</b>` pairs (160KB) and+ * 20,000 void `<br>` tags parse fine, because neither drives the tree deeper.+ * Only unclosed elements accumulate depth, so only they are counted.+ *+ * Past the cap the remaining opening tags are escaped (`<` → `<`) rather than+ * dropped: the text stays visible and readable as literal `<unk>`, which is+ * exactly what it is. Content under the cap is returned untouched — the common+ * case pays one scan and no rewrite.+ */++// parse5 nests only known HTML elements meaningfully, but an unknown tag+// (`<unk>`) still becomes an element node, so the depth cost is identical.+// Well under the ~1,750 measured failure point, and far above any nesting real+// markup uses — a deeply structured HTML document is a few dozen levels.+const MAX_HTML_DEPTH = 300++// Elements that never take children, so an opening tag adds no depth.+const VOID_ELEMENTS = new Set([+ 'area',+ 'base',+ 'br',+ 'col',+ 'embed',+ 'hr',+ 'img',+ 'input',+ 'link',+ 'meta',+ 'param',+ 'source',+ 'track',+ 'wbr'+])++// `<tag`, `</tag`, or `<tag/`. Deliberately loose on attributes: we only need+// the name and whether it opens, closes, or self-closes.+const TAG_RE = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|'[^']*'|[^>"'])*)>/g++/**+ * Escape raw HTML tags past `MAX_HTML_DEPTH` levels of unclosed nesting.+ *+ * Returns `text` unchanged when the depth never exceeds the cap, so the+ * identity is preserved for the overwhelming majority of messages (which+ * matters: the markdown pipeline caches on string identity).+ */+export function clampHtmlNestingDepth(text: string): string {+ // Fast path: no tags at all, nothing to bound.+ if (!text.includes('<')) {+ return text+ }++ const open: string[] = []+ let depth = 0+ let firstOverflow = -1+ let match: RegExpExecArray | null++ TAG_RE.lastIndex = 0++ while ((match = TAG_RE.exec(text)) !== null) {+ const closing = match[1] === '/'+ const name = match[2].toLowerCase()+ const selfClosing = match[3].endsWith('/')++ if (closing) {+ // Close the nearest matching open element, mirroring parse5's recovery:+ // an unmatched close tag is ignored rather than unwinding the stack.+ const index = open.lastIndexOf(name)++ if (index !== -1) {+ depth = index+ open.length = index+ }++ continue+ }++ if (selfClosing || VOID_ELEMENTS.has(name)) {+ continue+ }++ open.push(name)+ depth += 1++ if (depth > MAX_HTML_DEPTH && firstOverflow === -1) {+ firstOverflow = match.index+ }+ }++ if (firstOverflow === -1) {+ return text+ }++ // Everything before the overflow parses at a safe depth and is left exactly+ // as-is; from there on, opening tags become visible literal text.+ return text.slice(0, firstOverflow) + text.slice(firstOverflow).replaceAll('<', '<')+}diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.tsindex c4a8731f478..00d54403539 100644--- a/apps/desktop/src/lib/markdown-preprocess.ts+++ b/apps/desktop/src/lib/markdown-preprocess.ts@@ -1,6 +1,7 @@import { normalizeMathDelimiters } from '@assistant-ui/react-streamdown'import { isLikelyProseFence, sanitizeLanguageTag } from '@/lib/markdown-code'+import { clampHtmlNestingDepth } from '@/lib/markdown-html-depth'import { stripPreviewTargets } from '@/lib/preview-targets'import { linkifySessionRefs } from '@/lib/session-refs'@@ -510,8 +511,10 @@ export function preprocessMarkdown(text: string): string {const trailing = part.match(/\s*$/)?.[0] ?? ''// Run only on prose segments so `$5` literals and `\(` inside code- // blocks stay intact.- const transformed = normalizeVisibleProse(stripPreviewTargets(normalizeProseMath(part)))+ // blocks stay intact. The HTML-depth clamp belongs here for the same+ // reason: a fenced block renders as code and never reaches rehype-raw,+ // so escaping tags inside one would corrupt the listing for nothing.+ const transformed = clampHtmlNestingDepth(normalizeVisibleProse(stripPreviewTargets(normalizeProseMath(part))))return leading + transformed + trailing})