fix(desktop): bound raw HTML nesting before it reaches rehype-raw

Streamdown parses assistant markdown with allowDangerousHtml, so every
`<tag>` run in a message goes to parse5 and then through
hast-util-from-parse5, which recurses once per level of unclosed
nesting. Past roughly 1,750 consecutive unclosed tags that overflows the
call stack and throws RangeError out of the middle of a React render.

Nemotron-3-ultra degenerates into exactly that: thousands of `<unk>`
tokens emitted as reasoning, every one of them an element parse5 opens
and never closes. The payload is persisted to the session, so the throw
comes back on every reload.

Clamp the depth of unclosed elements in the prose path and escape the
opening `<` past the cap, leaving the text visible as the literal
`<unk>` it always was. The bound is on depth, not size: 20,000 balanced
`<b>x</b>` pairs and 20,000 void `<br>` tags parse fine because neither
drives the tree deeper, so only unclosed elements are counted and normal
markup is returned by identity.
This commit is contained in:
Brooklyn Nicholson
2026-08-13 13:25:15 -05:00
committed by brooklyn!
parent 7afac122ef
commit 1641512c94
3 changed files with 186 additions and 2 deletions
@@ -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('&lt;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('&lt;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('&lt;'))
// 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)
})
})
+112
View File
@@ -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 (`<` → `&lt;`) 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('<', '&lt;')
}
+5 -2
View File
@@ -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
})