Repository navigation
fix(sanitize): preserve angle brackets inside code blocks and inline code #2408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blackwell-systems
wants to merge
3
commits into
github:main
Choose a base branch
from
blackwell-systems:fix-angle-brackets-in-code-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ac2718a
fix(sanitize): preserve angle brackets inside code blocks and inline …
blackwell-systems 3c28543
fix: prevent sentinel collision XSS and document closing-fence deviation
blackwell-systems 64e6ffb
refactor: use goldmark CommonMark parser for code block detection
blackwell-systems File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,14 @@ var policy *bluemonday.Policy | |||||||||||||||||||||||||||||
| var policyOnce sync.Once | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func Sanitize(input string) string { | ||||||||||||||||||||||||||||||
| return FilterHTMLTags(FilterCodeFenceMetadata(FilterInvisibleCharacters(input))) | ||||||||||||||||||||||||||||||
| cleaned := FilterCodeFenceMetadata(FilterInvisibleCharacters(input)) | ||||||||||||||||||||||||||||||
| // Protect angle brackets inside code blocks and inline code spans | ||||||||||||||||||||||||||||||
| // from being stripped by the HTML sanitizer. bluemonday treats <int>, | ||||||||||||||||||||||||||||||
| // <T>, etc. as unknown HTML tags and removes them. | ||||||||||||||||||||||||||||||
| // See https://github.com/github/github-mcp-server/issues/2202 | ||||||||||||||||||||||||||||||
| protected := protectCodeAngleBrackets(cleaned) | ||||||||||||||||||||||||||||||
| sanitized := FilterHTMLTags(protected) | ||||||||||||||||||||||||||||||
| return restoreCodeAngleBrackets(sanitized) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // FilterInvisibleCharacters removes invisible or control characters that should not appear | ||||||||||||||||||||||||||||||
|
|
@@ -145,6 +152,162 @@ func isSafeCodeFenceToken(token string) bool { | |||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Sentinels used to protect angle brackets inside code from HTML sanitization. | ||||||||||||||||||||||||||||||
| // These are chosen to be unlikely to appear in real content. | ||||||||||||||||||||||||||||||
| const ( | ||||||||||||||||||||||||||||||
| ltSentinel = "\x00LT\x00" | ||||||||||||||||||||||||||||||
| gtSentinel = "\x00GT\x00" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
Comment on lines
+178
to
+185
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth noting in the doc comment that the NUL-stripping in
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // protectCodeAngleBrackets replaces < and > inside fenced code blocks and | ||||||||||||||||||||||||||||||
| // inline code spans with sentinels so bluemonday does not strip them as HTML. | ||||||||||||||||||||||||||||||
| func protectCodeAngleBrackets(input string) string { | ||||||||||||||||||||||||||||||
| var b strings.Builder | ||||||||||||||||||||||||||||||
| b.Grow(len(input)) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| runes := []rune(input) | ||||||||||||||||||||||||||||||
| i := 0 | ||||||||||||||||||||||||||||||
| n := len(runes) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for i < n { | ||||||||||||||||||||||||||||||
| // Fenced code block: ``` ... ``` | ||||||||||||||||||||||||||||||
| if i+2 < n && runes[i] == '`' && runes[i+1] == '`' && runes[i+2] == '`' { | ||||||||||||||||||||||||||||||
| // Find the fence length | ||||||||||||||||||||||||||||||
| fenceStart := i | ||||||||||||||||||||||||||||||
| fenceLen := 0 | ||||||||||||||||||||||||||||||
| for i < n && runes[i] == '`' { | ||||||||||||||||||||||||||||||
| fenceLen++ | ||||||||||||||||||||||||||||||
| i++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Write opening fence + rest of line (info string) | ||||||||||||||||||||||||||||||
| for range fenceLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| for i < n && runes[i] != '\n' { | ||||||||||||||||||||||||||||||
| b.WriteRune(runes[i]) | ||||||||||||||||||||||||||||||
| i++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if i < n { | ||||||||||||||||||||||||||||||
| b.WriteRune(runes[i]) // newline | ||||||||||||||||||||||||||||||
| i++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Inside fence: protect angle brackets until closing fence. | ||||||||||||||||||||||||||||||
| // NOTE: CommonMark requires the closing fence to be on its own line | ||||||||||||||||||||||||||||||
| // with no info string. This implementation is more permissive: any | ||||||||||||||||||||||||||||||
| // run of >= fenceLen backticks ends the block, even mid-line. This | ||||||||||||||||||||||||||||||
| // is a soft-fail (some angle brackets may be unprotected) rather | ||||||||||||||||||||||||||||||
| // than a security issue. | ||||||||||||||||||||||||||||||
| for i < n { | ||||||||||||||||||||||||||||||
| // Check for closing fence | ||||||||||||||||||||||||||||||
| if runes[i] == '`' { | ||||||||||||||||||||||||||||||
| closeLen := 0 | ||||||||||||||||||||||||||||||
| j := i | ||||||||||||||||||||||||||||||
| for j < n && runes[j] == '`' { | ||||||||||||||||||||||||||||||
| closeLen++ | ||||||||||||||||||||||||||||||
| j++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if closeLen >= fenceLen { | ||||||||||||||||||||||||||||||
| for range closeLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| i = j | ||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| switch runes[i] { | ||||||||||||||||||||||||||||||
| case '<': | ||||||||||||||||||||||||||||||
| b.WriteString(ltSentinel) | ||||||||||||||||||||||||||||||
| case '>': | ||||||||||||||||||||||||||||||
| b.WriteString(gtSentinel) | ||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||
| b.WriteRune(runes[i]) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| i++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| _ = fenceStart | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Inline code: `...` | ||||||||||||||||||||||||||||||
| if runes[i] == '`' { | ||||||||||||||||||||||||||||||
| // Count opening backticks | ||||||||||||||||||||||||||||||
| openLen := 0 | ||||||||||||||||||||||||||||||
| j := i | ||||||||||||||||||||||||||||||
| for j < n && runes[j] == '`' { | ||||||||||||||||||||||||||||||
| openLen++ | ||||||||||||||||||||||||||||||
| j++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Don't treat ``` as inline code (handled above for fenced blocks) | ||||||||||||||||||||||||||||||
| if openLen >= 3 { | ||||||||||||||||||||||||||||||
| for range openLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| i = j | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Find matching closing backticks | ||||||||||||||||||||||||||||||
| closeStart := -1 | ||||||||||||||||||||||||||||||
| for k := j; k <= n-openLen; k++ { | ||||||||||||||||||||||||||||||
| match := true | ||||||||||||||||||||||||||||||
| for m := range openLen { | ||||||||||||||||||||||||||||||
| if runes[k+m] != '`' { | ||||||||||||||||||||||||||||||
| match = false | ||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if match { | ||||||||||||||||||||||||||||||
| // Verify it's exactly openLen backticks (not more) | ||||||||||||||||||||||||||||||
| if k+openLen < n && runes[k+openLen] == '`' { | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| closeStart = k | ||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if closeStart == -1 { | ||||||||||||||||||||||||||||||
| // No closing backticks found; treat as literal | ||||||||||||||||||||||||||||||
| for range openLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| i = j | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Write opening backticks | ||||||||||||||||||||||||||||||
| for range openLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Protect content | ||||||||||||||||||||||||||||||
| for i = j; i < closeStart; i++ { | ||||||||||||||||||||||||||||||
| switch runes[i] { | ||||||||||||||||||||||||||||||
| case '<': | ||||||||||||||||||||||||||||||
| b.WriteString(ltSentinel) | ||||||||||||||||||||||||||||||
| case '>': | ||||||||||||||||||||||||||||||
| b.WriteString(gtSentinel) | ||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||
| b.WriteRune(runes[i]) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| // Write closing backticks | ||||||||||||||||||||||||||||||
| for range openLen { | ||||||||||||||||||||||||||||||
| b.WriteRune('`') | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| i = closeStart + openLen | ||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| b.WriteRune(runes[i]) | ||||||||||||||||||||||||||||||
| i++ | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return b.String() | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // restoreCodeAngleBrackets converts sentinels back to angle brackets. | ||||||||||||||||||||||||||||||
| func restoreCodeAngleBrackets(input string) string { | ||||||||||||||||||||||||||||||
| s := strings.ReplaceAll(input, ltSentinel, "<") | ||||||||||||||||||||||||||||||
| return strings.ReplaceAll(s, gtSentinel, ">") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func getPolicy() *bluemonday.Policy { | ||||||||||||||||||||||||||||||
| policyOnce.Do(func() { | ||||||||||||||||||||||||||||||
| p := bluemonday.StrictPolicy() | ||||||||||||||||||||||||||||||
|
|
@@ -175,7 +338,8 @@ func getPolicy() *bluemonday.Policy { | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func shouldRemoveRune(r rune) bool { | ||||||||||||||||||||||||||||||
| switch r { | ||||||||||||||||||||||||||||||
| case 0x200B, // ZERO WIDTH SPACE | ||||||||||||||||||||||||||||||
| case 0x0000, // NUL — stripped to prevent sentinel collision in protectCodeAngleBrackets | ||||||||||||||||||||||||||||||
| 0x200B, // ZERO WIDTH SPACE | ||||||||||||||||||||||||||||||
| 0x200C, // ZERO WIDTH NON-JOINER | ||||||||||||||||||||||||||||||
| 0x200E, // LEFT-TO-RIGHT MARK | ||||||||||||||||||||||||||||||
| 0x200F, // RIGHT-TO-LEFT MARK | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: pull out the ordering as a named invariant so future readers don't accidentally reorder these calls. The security argument relies on
FilterInvisibleCharactersrunning first (so NUL is stripped before sentinels are introduced) and onrestoreCodeAngleBracketsrunning last (afterFilterHTMLTags).