Skip to content

fix(sanitize): preserve angle brackets inside code blocks and inline code#2408

Open
blackwell-systems wants to merge 1 commit intogithub:mainfrom
blackwell-systems:fix-angle-brackets-in-code-blocks
Open

fix(sanitize): preserve angle brackets inside code blocks and inline code#2408
blackwell-systems wants to merge 1 commit intogithub:mainfrom
blackwell-systems:fix-angle-brackets-in-code-blocks

Conversation

@blackwell-systems
Copy link
Copy Markdown

Description

bluemonday.StrictPolicy() treats angle brackets inside markdown code blocks and inline code spans as HTML tags and strips them. This causes content like mut_raw_ptr<int> in issue/PR bodies to become mut_raw_ptr when read through MCP endpoints.

Before

let ptr: mut_raw_ptr<int> = raw_new int;

Agent sees: let ptr: mut_raw_ptr = raw_new int;

After

Agent sees: let ptr: mut_raw_ptr<int> = raw_new int;

Root cause

FilterHTMLTags calls bluemonday.Sanitize() on the entire markdown body without distinguishing code from prose. Bluemonday treats <int>, <T>, <String>, etc. as unrecognized HTML tags and removes them.

Fix

Before HTML sanitization, replace < and > inside fenced code blocks (```) and inline code spans (`) with null-byte sentinels that bluemonday will not interpret as HTML. After sanitization, restore the sentinels to angle brackets.

This preserves XSS protection for angle brackets in prose (e.g. <script> is still stripped) while keeping angle brackets inside code intact.

Testing

Added 6 test cases covering:

  • Fenced code blocks with angle brackets (the reported bug)
  • Inline code with generic types (Vec<String>)
  • Angle brackets outside code still sanitized (XSS protection preserved)
  • Multiple inline code spans
  • Fenced code with language info string

Fixes #2202

@blackwell-systems blackwell-systems requested a review from a team as a code owner April 30, 2026 02:39
…code

bluemonday's StrictPolicy treats angle brackets inside markdown code
blocks and inline code spans as HTML tags and strips them. This causes
content like `mut_raw_ptr<int>` to become `mut_raw_ptr` when read
through MCP issue/PR endpoints.

The fix protects angle brackets inside fenced code blocks (```) and
inline code spans (`) with sentinels before HTML sanitization, then
restores them after. Angle brackets outside code are still sanitized
normally, preserving XSS protection.

Fixes github#2202

Signed-off-by: Dayna Blackwell <dayna@blackwell-systems.com>
@blackwell-systems blackwell-systems force-pushed the fix-angle-brackets-in-code-blocks branch from 3722fe4 to 680c63b Compare April 30, 2026 02:41
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.

GitHub MCP issue read appears to drop code block text in angle brackets

1 participant