An open source design system that's fully customizable and agent ready
TypeScript
fix(Markdown): cap list and blockquote nesting, so deep input cannot crash rendering (#7075)
Cindy Zhangf524d72975c7
diff --git a/.changeset/markdown-block-nesting-cap.md b/.changeset/markdown-block-nesting-cap.mdnew file mode 100644index 0000000000..430baa6ab0--- /dev/null+++ b/.changeset/markdown-block-nesting-cap.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] Markdown: cap list and blockquote nesting, so deep input cannot crash rendering+@cixzhang++`Markdown` now nests lists and blockquotes at most 100 levels deep, as it already caps emphasis; content nested deeper reads as text. A list or blockquote nested thousands of levels deep, as a crafted message can be, used to overflow the stack and throw while parsing.diff --git a/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts b/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.tsnew file mode 100644index 0000000000..124362ef99--- /dev/null+++ b/packages/core/src/Markdown/__tests__/MarkdownNesting.a11y.chromium.spec.ts@@ -0,0 +1,101 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file MarkdownNesting.a11y.chromium.spec.ts+ * @input Uses the Core/Markdown Default story, given deeply nested input+ * through its `children` arg+ * @output Real-Chromium evidence that lists and blockquotes nested thousands+ * of levels deep render without an error, nested at most 100 deep+ * @position Browser evidence for Markdown's block nesting cap+ */++import {expect, test, type Page} from '@playwright/test';+import {+ DEFAULT_STORYBOOK_DIR,+ serveStorybook,+ type StaticServer,+} from '@astryxdesign/a11y-spec/storybook';++const STORYBOOK_DIR = process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR;+const STORY = 'core-markdown--default';++let storybook: StaticServer;++test.beforeAll(async () => {+ storybook = await serveStorybook(STORYBOOK_DIR);+});++test.afterAll(async () => {+ await storybook?.close();+});++/** Opens the story and renders `markdown` in it; returns the page errors. */+async function renderMarkdown(page: Page, markdown: string): Promise<string[]> {+ const errors: string[] = [];+ page.on('pageerror', error => errors.push(String(error)));+ page.on('console', message => {+ if (message.type() === 'error' && !message.text().includes('404')) {+ errors.push(message.text());+ }+ });+ await page.goto(+ `${storybook.origin}/iframe.html?id=${STORY}&viewMode=story&globals=astryxTheme:neutral;colorMode:light;direction:ltr`,+ {waitUntil: 'load'},+ );+ await expect(page.locator('#storybook-root')).not.toBeEmpty();+ await page.evaluate(+ ({storyId, children}) => {+ const channel = (+ window as Window & {+ __STORYBOOK_ADDONS_CHANNEL__?: {+ emit(event: string, payload: unknown): void;+ };+ }+ ).__STORYBOOK_ADDONS_CHANNEL__;+ channel?.emit('updateStoryArgs', {storyId, updatedArgs: {children}});+ },+ {storyId: STORY, children: markdown},+ );+ return errors;+}++/** How deep lists and blockquotes nest in the rendered story. */+async function renderedDepth(page: Page): Promise<number> {+ return page.locator('#storybook-root').evaluate(root => {+ let deepest = 0;+ const stack: {element: Element; depth: number}[] = [+ {element: root, depth: 0},+ ];+ while (stack.length > 0) {+ const {element, depth} = stack.pop() as {element: Element; depth: number};+ const next = ['UL', 'OL', 'BLOCKQUOTE'].includes(element.tagName)+ ? depth + 1+ : depth;+ deepest = Math.max(deepest, next);+ for (const child of element.children) {+ stack.push({element: child, depth: next});+ }+ }+ return deepest;+ });+}++test.describe('Markdown renders deeply nested input', () => {+ for (const [name, markdown] of [+ ['a list 10,000 deep', `${'- '.repeat(10_000)}deepest`],+ ['a blockquote 10,000 deep', `${'> '.repeat(10_000)}deepest`],+ [+ 'lists and blockquotes 10,000 deep',+ `${Array.from({length: 10_000}, (_, level) => (level % 2 === 0 ? '> ' : '- ')).join('')}deepest`,+ ],+ ] as const) {+ test(`${name}, nested at most 100 deep, with no error`, async ({page}) => {+ const errors = await renderMarkdown(page, markdown);+ await expect(page.locator('#storybook-root')).toContainText('deepest');+ const depth = await renderedDepth(page);+ expect(depth).toBeGreaterThan(50);+ expect(depth).toBeLessThanOrEqual(101);+ expect(errors).toEqual([]);+ });+ }+});diff --git a/packages/core/src/Markdown/blockNesting.test.tsx b/packages/core/src/Markdown/blockNesting.test.tsxnew file mode 100644index 0000000000..37d6870b5a--- /dev/null+++ b/packages/core/src/Markdown/blockNesting.test.tsx@@ -0,0 +1,95 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, expect, it} from 'vitest';+import {render} from '@testing-library/react';+import {Markdown} from './Markdown';+import {parseMarkdownAst} from './parser';++type Json = {type: string; value?: string; children?: Json[]};++/** A list nested `levels` deep by indentation, one item per level. */+const indentedList = (levels: number): string =>+ Array.from({length: levels}, (_, level) => `${' '.repeat(level)}- a`).join(+ '\n',+ );++/** A list nested `levels` deep on one line: each item opens the next list. */+const nestedList = (levels: number, marker = '- '): string =>+ `${marker.repeat(levels)}a`;++/** A blockquote nested `levels` deep. */+const nestedQuote = (levels: number): string => `${'> '.repeat(levels)}a`;++/** Lists and blockquotes alternating `levels` deep, on one line. */+const mixed = (levels: number): string =>+ `${Array.from({length: levels}, (_, level) => (level % 2 === 0 ? '> ' : '- ')).join('')}a`;++/** The deepest run of lists and blockquotes, iteratively. */+function blockDepth(nodes: ReadonlyArray<Json>): number {+ let deepest = 0;+ const stack: {node: Json; depth: number}[] = nodes.map(node => ({+ node,+ depth: 0,+ }));+ while (stack.length > 0) {+ const {node, depth} = stack.pop() as {node: Json; depth: number};+ const next =+ node.type === 'list' || node.type === 'blockquote' ? depth + 1 : depth;+ deepest = Math.max(deepest, next);+ for (const child of node.children ?? []) {+ stack.push({node: child, depth: next});+ }+ }+ return deepest;+}++/** The fastest of three parses of `markdown`, in milliseconds. */+function parseTime(markdown: string): number {+ parseMarkdownAst(markdown);+ let fastest = Number.POSITIVE_INFINITY;+ for (let round = 0; round < 3; round++) {+ const started = performance.now();+ parseMarkdownAst(markdown);+ fastest = Math.min(fastest, performance.now() - started);+ }+ return fastest;+}++describe('lists and blockquotes nest at most 100 deep', () => {+ it('nests a list 100 deep exactly, and reads deeper items as text', () => {+ const at = (levels: number) =>+ blockDepth(parseMarkdownAst(indentedList(levels)).children as never);+ expect(at(50)).toBe(50);+ expect(at(101)).toBe(101);+ expect(at(150)).toBe(101);+ });++ it.each([+ ['a list 2,000 deep', nestedList(2_000)],+ ['a list 10,000 deep', nestedList(10_000)],+ ['an ordered list 10,000 deep', nestedList(10_000, '1. ')],+ ['a list 500 deep by indentation', indentedList(500)],+ ['a blockquote 2,000 deep', nestedQuote(2_000)],+ ['a blockquote 10,000 deep', nestedQuote(10_000)],+ ['lists and blockquotes 10,000 deep', mixed(10_000)],+ ])('parses %s without throwing, within a time budget', (_, markdown) => {+ expect(() => parseMarkdownAst(markdown)).not.toThrow();+ const depth = blockDepth(parseMarkdownAst(markdown).children as never);+ expect(depth).toBeGreaterThan(50);+ expect(depth).toBeLessThanOrEqual(101);+ // The budget leaves room for a loaded test machine.+ expect(parseTime(markdown)).toBeLessThan(5000);+ });++ it.each([+ ['a list', nestedList(10_000)],+ ['a blockquote', nestedQuote(10_000)],+ ['lists and blockquotes', mixed(10_000)],+ ])(+ 'renders %s nested past the cap without exhausting the stack',+ (_, markdown) => {+ const {container} = render(<Markdown>{markdown}</Markdown>);+ expect(container.textContent).toContain('a');+ },+ );+});diff --git a/packages/core/src/Markdown/parser.ts b/packages/core/src/Markdown/parser.tsindex ed4e88e6d7..49fb510779 100644--- a/packages/core/src/Markdown/parser.ts+++ b/packages/core/src/Markdown/parser.ts@@ -561,8 +561,21 @@ type ResolvedOptions = {* to resolve full/collapsed/shortcut reference links and images.*/readonly linkDefs?: ReadonlyMap<string, string>;+ /**+ * How many lists and blockquotes enclose this parse. Internal only: past+ * MAX_BLOCK_NESTING, content is read as text, so no input nests blocks+ * deep enough to exhaust the stack of the parser, a projection, or a+ * render.+ */+ readonly blockDepth: number;};+/**+ * The deepest lists and blockquotes may nest, as emphasis is capped+ * (MAX_EMPHASIS_DEPTH). Content nested deeper is one paragraph of text.+ */+const MAX_BLOCK_NESTING = 100;+/*** Every resolved options object is built here, so all of them share one key* set in one order — and therefore one hidden class. The parser reads these@@ -585,6 +598,7 @@ function makeResolvedOptions(allowBlockSyntax: fields.allowBlockSyntax ?? true,baseOffset: fields.baseOffset,linkDefs: fields.linkDefs,+ blockDepth: fields.blockDepth ?? 0,};}@@ -2623,11 +2637,13 @@ function isTableSeparator(line: string): boolean {* offset into it would not address the document.*/function nested(opts: ResolvedOptions): ResolvedOptions {- const nestedOptions =- opts.allowBlockSyntax === false ? opts : {...opts, allowBlockSyntax: false};- return nestedOptions.sourceRanges || nestedOptions.astPositions- ? {...nestedOptions, sourceRanges: false, astPositions: false}- : nestedOptions;+ return {+ ...opts,+ allowBlockSyntax: false,+ sourceRanges: false,+ astPositions: false,+ blockDepth: opts.blockDepth + 1,+ };}function blockExtensionColumn(line: string): number | null {@@ -3055,6 +3071,13 @@ function parseMarkdownImpl(input: string,baseOpts: ResolvedOptions,): MarkdownAstBlockContent<RuntimeExtensionNode>[] {+ if (baseOpts.blockDepth > MAX_BLOCK_NESTING) {+ // Nested deeper than the cap: the content is one paragraph of text.+ const text = input.trim();+ return text === ''+ ? []+ : [{type: 'paragraph', children: parseInlineEntry(text, baseOpts)}];+ }// Collect this input's link reference definitions and strip their lines,// then merge them with any definitions inherited from an enclosing parse// (the incremental parser passes the whole document's definitions in; a