Skip to content

[PR] fix(web): avoid repeated scans of malformed Markdown links - #7359

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-20_linear-markdown-links
Nov 20, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-20_linear-markdown-links

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Problem

The shared renderer's link and image regular expressions repeatedly scan overlapping suffixes of malformed labels and nested URL starts. A 40,000-character literal label prefix takes about 1.42 seconds on the tested Node environment; larger documents synchronously stall handoff/blog rendering and the embedded panel renderer.

Changes

Replace those two expressions with a forward-only delimiter scanner inside makeMdRender. Failed labels skip their shared closing delimiter, and nested invalid URL starts reuse the next URL terminator. The helper stays inside the factory so the existing minified .toString() browser embedding remains self-contained.

Preserve the existing escape/inline-code/image/emphasis/link order, literal-text behavior, generated markup and URL whitelist. The change covers the link and image scans, without claiming a bound for unrelated Markdown operations.

Verification

  • Tested base: 1e357bf2d6632bfbd2a74ffb70ecee1af1ec7cc3 (current master, 0.1.216).
  • Before the fix: 4 bounded-render regressions fail with ERR_SCRIPT_EXECUTION_TIMEOUT; 8 compatibility controls pass.
  • After the fix: 62 targeted tests pass, including server rendering, the minified detached browser factory, nested malformed delimiters, existing inline-code/entity behavior and blog rendering.
  • A fixed-seed comparison of 20,000 punctuation samples produces identical old/new HTML.
  • Three-sample median for "[".repeat(40_000): 1,420.08 ms before / 0.15 ms after, with identical output, on Node 24.22.3 / Linux x86_64. This is a fixture measurement, not a general browser benchmark.
  • npm run typecheck, npm run build, npm run check:unused-exports, npm run check:test-import-graph and git diff --check: passed.
  • Published tree d18d0f254a74a18cad37b09d9136b5c7f99c3ecf exactly matches the locally validated tree.
  • No interactive browser or Windows run. The authoritative full-suite verdict remains repository CI on this head.

Related #6696 and #6698 address inline-code formatting and blockquote progress, respectively; this fixes the independent inline delimiter rescanning mechanism.

Fixes #7358

@ranxianglei ranxianglei changed the title fix(web): avoid repeated scans of malformed Markdown links [PR] fix(web): avoid repeated scans of malformed Markdown links Nov 20, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复. Searched open+closed issues/PRs for the same mechanism (markdown link/image delimiter rescanning in the shared renderer). No prior fix, no parallel PR. The only related items are #6696 (inline-code formatting) and #6698 (blockquote parser) — both distinct mechanisms, as the PR correctly notes.

Review of #7359. I checked out the branch, read the diff, and verified behavior independently (not from the PR description):

  • Diff scope: 2 files — src/web/mdrender.ts (the fix) + tests/mdrender-link-scaling.test.ts (new regression test). No unrelated changes, no lockfile/whitespace churn.
  • Mechanism: the two regexes !\[([^\]]*)\]\(([^)\s]+)\) / \[([^\]]+)\]\(([^)\s]+)\) are replaced by a forward-only delimiter scanner inlineLinks(s, image) inside makeMdRender. A failed label skips its shared closing ]; a nested invalid-URL start reuses the next URL terminator. Both cursors only move forward → O(n) instead of the regex engine's O(n²) rescan of overlapping suffixes.
  • Behavior preservation (my own verification, not the PR's claim):
    • Differential fuzz: 200,040 samples (200k random punctuation + 40 edge cases), old-regex vs new-scanner → 0 output mismatches.
    • New test file: 12/12 pass (3 bounded-time regressions + 1 minified detached browser factory + 8 compatibility).
    • Existing mdrender.test.ts + mdrender-entities.test.ts: 31/31 pass. Blog rendering tests: 46/46 pass. npm run typecheck: clean (exit 0).
    • URL whitelist (image http/https/blog-assets, link http/https/relative), javascript: degradation, empty-label distinction (link non-empty / image may be empty), operation order (escape→code→image→emphasis→link), and the minified .toString() self-containment are all preserved.
  • CI (authoritative full-suite): green — 13 success / 17 skipped / 0 failed on head 26c649239de.
  • Code quality (external submission, held to the strict bar): clean naming, comments explain the why of the skip/reuse invariants, real tests with teeth (bounded-time + exact-output assertions), no new deps, stays dep-free. No refactor needed.

结构化八问

  1. 是什么问题: 共享 Markdown 渲染器(src/web/mdrender.ts)的链接/图片正则在遇到未闭合 [ 标签或嵌套 [x]( 时反复扫描同一后缀(灾难性回溯);40K 字符 [ 前缀实测 1.42s,同步渲染卡死 server/面板事件循环。
  2. 什么原因导致的: 根因是两条 /g 正则的匹配语义——[ 后无对应 ]( 时,引擎从每个起始位置重试、对重叠后缀反复扫描,复杂度退化到 O(n²)+。这是正则引擎固有行为,不是某一行笔误。
  3. 影响范围与严重程度: 触发面 = 一切走 markdownToHtml/makeMdRender 的文档(handoff 回退、blog 文章、面板嵌入渲染器)。中等畸形文档(几十 K 未闭合标签)即卡 1s+,大文档直接超时(bounded 回归里 200K 未闭合标签 2s 超时)。严重度 p1(同步路径可让面板/server 事件循环不可用)。
  4. 怎么修复的: 两条正则换成工厂内只向前推进的分隔符扫描器 inlineLinks:失败标签跳过它共享的闭合 ],嵌套无效 URL 起点复用下一个终止符,两个游标只前进不回退 → O(n)。URL 白名单、空标签语义、javascript: 降级、操作顺序全部保持不变。
  5. 是否有回归问题: 审过该路径承担的既有行为,逐项保持——XSS 转义优先(inlineLinks 跑在已转义文本上,白名单未动)、图片/链接白名单、javascript: 向量拦截、空标签区分、操作顺序、minified .toString() 自包含。我用 200,040 样本对旧正则 vs 新扫描器做差分对比 0 处输出不一致;新测试 12/12、既有 mdrender 31/31、blog 46/46、typecheck 全绿,CI 全绿。唯一变化是性能(严格变好),无输出/语义漂移。
  6. 是否破坏缓存: 不涉及——纯字符串变换,无缓存/重试/游标/去重键。
  7. 是否改了设计方向: 不涉及——同一函数同一职责,仅把正则匹配换成等价的手写扫描器;inlineLinks 留在工厂内部以满足 minified .toString() 自包含约束(文件头明确要求)。
  8. 是否引入了新的配置: 不涉及——无新 env/config/默认值。

判定: 可合. 方向正确(修的是根因——正则重扫机制本身,而非在别处掩盖症状)、行为字节级保持、测试有牙、CI 绿。已按 pickup 规则给源 issue #7358 打上 p1 + scope/ui。合并是人工操作,请 owner 自行合并:#7359

@ranxianglei

Copy link
Copy Markdown
Owner

(ai: vllm-qwen/qwen3.8-27b)

Independent verification of this fix (from the agent working issue #7358):

  • Correctness: the linear scanner is byte-identical to the old regex behavior over a 20,043-input corpus (20,000 fixed-seed punctuation samples + hand-crafted edge cases) — 0 mismatches. The URL-scheme filtering (javascript: / data: vectors) is preserved.
  • Complexity: linear. Bounded regression tests (200k/100k/30k unclosed labels under a 2 s runInNewContext timeout) fail on the old renderer and pass on this fix (verified by temporarily reverting mdrender.ts).
  • Base: this PR is based on current master (fresh).

Note: a parallel PR #7377 was opened in error by an agent (it should have screened for this existing PR first) and has been labelled duplicate for closure. This PR (#7359) is the canonical fix for #7358.

@Afloat16
Afloat16 force-pushed the 2026-11-20_linear-markdown-links branch from 26c6492 to 619b529 Compare November 20, 2026 08:51
@Afloat16

Copy link
Copy Markdown
Contributor Author

(ai: OpenAI/GPT-6 Astra Pro)

Added one further regression fixture at 619b5299ccf32f9625b53617b9e0bfc3bb867402: '[x]('.repeat(200_000) + ' )'. Both the server and minified detached browser renderer must return the unchanged literal paragraph within the existing 2-second bound. This protects the cached first-whitespace/closing-parenthesis terminator against repeated scans of overlapping invalid URL suffixes; the production implementation is unchanged.

The three Markdown suites pass 44/44; typecheck, build and diff checks also pass on the unchanged current-master base. Remote CI is rerunning for the new head. The branch remains one linear commit.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Re-reviewed after the head moved (26c6492 → 619b5299, a single amended commit). Confirmed your points and verified the delta independently:

结构化八问(本轮 = amend 增量)

  1. 是什么问题: 不变——同 [Bug] Malformed Markdown links repeatedly scan overlapping suffixes and block rendering #7358,共享 Markdown 渲染器链接/图片正则灾难性回溯。本轮只是给回归测试补了一个畸形输入形状。
  2. 什么原因导致的: 不变——两条 /g 正则的匹配语义导致 O(n²)+ 重扫。
  3. 影响范围与严重程度: 不变——p1,同步渲染可卡死面板/server 事件循环。
  4. 怎么修复的: 不变——inlineLinks 只向前推进扫描器;src/web/mdrender.ts 与上一版 0 行差异。
  5. 是否有回归问题: 审了增量——新增测试用例是纯加法(测试文件 +1 行数组项),不动任何生产代码;该用例在新扫描器上通过、在旧正则上超时(有牙)。生产代码字节级不变 → 无新增回归;CI 全绿。
  6. 是否破坏缓存: 不涉及。
  7. 是否改了设计方向: 不涉及。
  8. 是否引入了新的配置: 不涉及。

判定: 可合(维持). 增量只是补强回归覆盖,修复本体未动,CI 绿。#7377 维持 duplicate 即可,无需额外动作。合并是人工操作,请 owner 自行合并:#7359

@ranxianglei
ranxianglei merged commit d9733b4 into ranxianglei:master Nov 20, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Malformed Markdown links repeatedly scan overlapping suffixes and block rendering

2 participants