fix(security): external content marker sanitization bypass via invisible characters#2
Open
cubic-dev-ai[bot] wants to merge 1 commit into
Open
Conversation
…rkers The replaceMarkers function computed regex match indices on the folded (invisible-chars-stripped) string but applied them to the original string via slice(). An attacker could prepend invisible characters (e.g. U+200B) to shift indices and leave spoofed boundary markers intact. Fix: perform replacements directly on the folded string instead of mapping indices back to the original, eliminating the index mismatch entirely.
Author
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/security/external-content.ts">
<violation number="1" location="src/security/external-content.ts:208">
P2: Restore the check to return the original `content` when no markers are replaced. Without this, the function irreversibly strips invisible characters and normalizes homoglyphs across the entire message just because it contains the phrase 'external untrusted content'.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| } | ||
| output += content.slice(cursor); | ||
| return output; | ||
| return sanitized; |
Author
There was a problem hiding this comment.
P2: Restore the check to return the original content when no markers are replaced. Without this, the function irreversibly strips invisible characters and normalizes homoglyphs across the entire message just because it contains the phrase 'external untrusted content'.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/security/external-content.ts, line 208:
<comment>Restore the check to return the original `content` when no markers are replaced. Without this, the function irreversibly strips invisible characters and normalizes homoglyphs across the entire message just because it contains the phrase 'external untrusted content'.</comment>
<file context>
@@ -191,53 +191,21 @@ function foldMarkerText(input: string): string {
- }
- output += content.slice(cursor);
- return output;
+ return sanitized;
}
</file context>
[internal] Confidence score: 10/10
[internal] Posted by: General AI Review Agent
Suggested change
| return sanitized; | |
| return sanitized === folded ? content : sanitized; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
replaceMarkerswhere regex match indices from the folded (invisible-chars-stripped) string were applied to the original string, allowing attackers to bypass marker sanitization by prepending invisible Unicode characters (e.g.\u200B)Root Cause
replaceMarkerscalledfoldMarkerText(content)to strip invisible characters and normalize homoglyphs, then ran regex matching on the resulting shorterfoldedstring. However, the match indices were used toslice()the original (longer)contentstring. An attacker injecting N invisible characters before a spoofed boundary marker would shift the replacement window by N characters, leaving the actual marker intact.Fix
Perform regex replacements directly on the folded string instead of mapping indices back to the original. When a marker is detected, the folded string (with replacements applied) is returned, not the original.
Test plan
pnpm check(lint + format + type-check) passespnpm tsgopasses🤖 Generated with Claude Code