Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 500
Decode HTML entities before @mention detection to prevent bypass#14846
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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 |
|---|---|---|
| @@ -485,6 +485,54 @@ function applyTruncation(content, maxLength) { | ||
| return content; | ||
| } | ||
| /** | ||
| * Decodes HTML entities to prevent bypass of @mention detection. | ||
| * Handles named entities (e.g., @), decimal entities (e.g., @), | ||
| * and hex entities (e.g., @), including double-encoded variants (e.g., @). | ||
| * | ||
| * @param {string} text - Input text that may contain HTML entities | ||
| * @returns {string} Text with HTML entities decoded | ||
| */ | ||
| function decodeHtmlEntities(text) { | ||
| if (!text || typeof text !== "string") { | ||
| return ""; | ||
| } | ||
| let result = text; | ||
| // Decode named entity for @ symbol (including double-encoded variants) | ||
| // @ and @ → @ | ||
| result = result.replace(/&(?:amp;)?commat;/gi, "@"); | ||
| // Decode decimal entities (including double-encoded variants) | ||
| // @ and @ → @ | ||
| // &#NNN; and &#NNN; → corresponding character | ||
| result = result.replace(/&(?:amp;)?#(\d+);/g, (match, code) => { | ||
| const codePoint = parseInt(code, 10); | ||
| // Validate code point is in valid Unicode range | ||
| if (codePoint >= 0 && codePoint <= 0x10ffff) { | ||
| return String.fromCodePoint(codePoint); | ||
| } | ||
pelikhan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Return original match if invalid | ||
| return match; | ||
| }); | ||
| // Decode hex entities (including double-encoded variants) | ||
| // @, @, &#x40;, &#X40; → @ | ||
| // &#xHHH;, &#XHHH;, &#xHHH;, &#XHHH; → corresponding character | ||
| result = result.replace(/&(?:amp;)?#[xX]([0-9a-fA-F]+);/g, (match, code) => { | ||
| const codePoint = parseInt(code, 16); | ||
| // Validate code point is in valid Unicode range | ||
pelikhan marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (codePoint >= 0 && codePoint <= 0x10ffff) { | ||
| return String.fromCodePoint(codePoint); | ||
| } | ||
| // Return original match if invalid | ||
| return match; | ||
| }); | ||
| return result; | ||
| } | ||
| /** | ||
| * Performs text hardening to protect against Unicode-based attacks. | ||
| * This applies multiple layers of character normalization and filtering | ||
| @@ -504,16 +552,21 @@ function hardenUnicodeText(text) { | ||
| // This ensures consistent character representation across different encodings | ||
| result = result.normalize("NFC"); | ||
| // Step 2: Strip invisible zero-width characters that can hide content | ||
| // Step 2: Decode HTML entities to prevent @mention bypass | ||
| // This MUST happen early, before any other processing, to ensure entities | ||
| // are converted to their actual characters for proper sanitization | ||
| result = decodeHtmlEntities(result); | ||
| // Step 3: Strip invisible zero-width characters that can hide content | ||
| // These include: zero-width space, zero-width non-joiner, zero-width joiner, | ||
| // word joiner, and byte order mark | ||
| result = result.replace(/[\u200B\u200C\u200D\u2060\uFEFF]/g, ""); | ||
| // Step 3: Remove bidirectional text override controls | ||
| // Step 4: Remove bidirectional text override controls | ||
| // These can be used to reverse text direction and create visual spoofs | ||
| result = result.replace(/[\u202A\u202B\u202C\u202D\u202E\u2066\u2067\u2068\u2069]/g, ""); | ||
| // Step 4: Convert full-width ASCII characters to standard ASCII | ||
| // Step 5: Convert full-width ASCII characters to standard ASCII | ||
| // Full-width characters (U+FF01-FF5E) can be used to bypass filters | ||
| result = result.replace(/[\uFF01-\uFF5E]/g, char => { | ||
| const code = char.charCodeAt(0); | ||
| @@ -611,4 +664,5 @@ module.exports = { | ||
| neutralizeBotTriggers, | ||
| applyTruncation, | ||
| hardenUnicodeText, | ||
| decodeHtmlEntities, | ||
| }; | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.