diff --git a/apps/desktop/src/lib/markdown-html-depth.test.ts b/apps/desktop/src/lib/markdown-html-depth.test.ts new file mode 100644 index 0000000000..d58dbcbbe2 --- /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 = '

A bold claim

' + + expect(clampHtmlNestingDepth(text)).toBe(text) + }) + + it('leaves a wide but shallow tag run untouched — count is not the risk', () => { + const text = 'x'.repeat(20_000) + + expect(clampHtmlNestingDepth(text)).toBe(text) + }) + + it('leaves void elements untouched — they never add depth', () => { + const text = '
'.repeat(20_000) + + expect(clampHtmlNestingDepth(text)).toBe(text) + }) + + it('leaves self-closing tags untouched — they never add depth', () => { + const text = ''.repeat(20_000) + + expect(clampHtmlNestingDepth(text)).toBe(text) + }) + + it('escapes an unbounded run of unclosed unknown tags', () => { + const clamped = clampHtmlNestingDepth('Let' + ''.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')).toBe(true) + }) + + it('escapes an unbounded run of unclosed known tags', () => { + const clamped = clampHtmlNestingDepth('
'.repeat(5_000)) + + expect(clamped).toContain('<div>') + }) + + it('keeps the prefix below the cap parseable and only escapes past it', () => { + const clamped = clampHtmlNestingDepth(''.repeat(5_000)) + const parseable = clamped.slice(0, clamped.indexOf('<')) + + // Everything before the overflow point is untouched real markup... + expect(parseable).toBe(''.repeat(parseable.length / ''.length)) + // ...and it is bounded, which is the entire point. + expect(parseable.length).toBeLessThan(''.repeat(5_000).length) + }) + + it('does not accumulate depth when tags close, however many there are', () => { + const text = '
x
'.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.ts new file mode 100644 index 0000000000..3cb8fcf6fe --- /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 `` 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 `` + * 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 `x` pairs (160KB) and + * 20,000 void `
` 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 ``, 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 +// (``) 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' +]) + +// `"'])*)>/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.ts index c4a8731f47..00d5440353 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 })