Skip to content

fix: DCP_PAIRED_TAG_REGEX regex bug causes tag fragment leakage into chat#124

Closed
mengfanbo123 wants to merge 1 commit into
ranxianglei:masterfrom
mengfanbo123:fix/paired-tag-regex-leak
Closed

fix: DCP_PAIRED_TAG_REGEX regex bug causes tag fragment leakage into chat#124
mengfanbo123 wants to merge 1 commit into
ranxianglei:masterfrom
mengfanbo123:fix/paired-tag-regex-leak

Conversation

@mengfanbo123

Copy link
Copy Markdown

Bug

After multiple compression rounds, malformed XML tag fragments like </d-message-id> and stale message IDs (e.g. m00097, m00099) leak into the user-visible chat dialog.

Root Cause

lib/messages/utils.ts:14DCP_PAIRED_TAG_REGEX:

- const DCP_PAIRED_TAG_REGEX = /]*>[\s\S]*?<\/(?:dcp|acp)[^>]*/gi
+ const DCP_PAIRED_TAG_REGEX = /<(?:dcp|acp)[^>]*>[\s\S]*?<\/(?:dcp|acp)[^>]*/gi

The buggy regex starts with ]*> which matches a literal ] character — missing the < and tag name. As a result, stripHallucinationsFromString only partially deletes paired tags: it removes the closing tag + content but leaves behind opening-tag fragments that then leak into the chat.

Call Path

stripHallucinations (lib/messages/utils.ts:265) → called globally at lib/hooks.ts:246. stripHallucinationsFromString (utils.ts:261) runs the PAIRED regex first (which misses due to the bug), then the UNPAIRED regex /<\/?(?:dcp|acp)[^>]*/gi to clean up residual tag fragments.

Fix

Add <(?:dcp|acp)[^>]*> to correctly match the opening tag. Now the PAIRED regex matches the full open+content+close span and removes it in one pass.

Verification

  • node --import tsx --test tests/strip-stale-compress.test.ts tests/inject.test.ts tests/message-utils.test.ts32/32 pass
  • Reproduced the leak locally before fix; confirmed clean after fix.

Related

line 14 正则 /]*>/ 开头匹配字面量 ']',缺少 '<' 和标签名。
导致 stripHallucinationsFromString 部分删除配对标签——
删了闭标签+内容,残留开标签碎片(如 </d-message-id>)泄漏到用户对话框。

修复:正则改为 /<(?:dcp|acp)[^>]*>/ 正确匹配开标签。

Closes ranxianglei#123
@ranxianglei

Copy link
Copy Markdown
Owner

缺少devlog和tests因此ci无法通过

@ranxianglei

Copy link
Copy Markdown
Owner

Thanks for the fix! Your root cause analysis for DCP_PAIRED_TAG_REGEX (line 14) was spot-on.

While reviewing, I found that lines 11 (DCP_BLOCK_ID_TAG_REGEX) and 13 (DCP_MESSAGE_REF_TAG_REGEX) have the same class of bug. I've created PR #130 which includes your fix plus the two additional regex fixes, with 23 dedicated tests.

Closing this in favor of #130.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: stripHallucinations 正则错误导致 dcp-message-id 标签碎片泄漏到对话框

2 participants