Skip to content

Fix @mention sanitization bypass with underscore prefix#15076

Merged
pelikhan merged 2 commits intomainfrom
copilot/fix-mention-sanitization
Feb 12, 2026
Merged

Fix @mention sanitization bypass with underscore prefix#15076
pelikhan merged 2 commits intomainfrom
copilot/fix-mention-sanitization

Conversation

Copy link
Contributor

Copilot AI commented Feb 12, 2026

The neutralizeAllMentions() regex used [^\w]as a negative lookahead, but\wincludes underscore. This allowed patterns liketest_@userto bypass sanitization since the underscore preceding@` matched the word character class.

Changes

  • Regex fix: Changed [^\w]to[^A-Za-z0-9] in sanitize_content_core.cjs:300

    • Explicitly excludes only alphanumeric characters and backticks
    • Underscore now triggers mention neutralization
  • Test coverage: Added 10 security tests for bypass patterns

    • test_@user, production_@maintainer, _@user, etc.
    • All 3,726 JavaScript tests pass

Example

// Before (vulnerable)
/(^|[^\w`])@([A-Za-z0-9]...)/g
"test_@user"  "test_@user"  // bypassed

// After (fixed)
/(^|[^A-Za-z0-9`])@([A-Za-z0-9]...)/g
"test_@user" → "test_`@user`"  // neutralized

Backward compatibility preserved - normal mentions, emails, and existing patterns unaffected.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Changed regex in neutralizeAllMentions() from [^\w`] to [^A-Za-z0-9`]
- This includes underscore as a valid preceding character, preventing bypass patterns like "test_@user"
- Added 10 comprehensive tests for underscore-prefixed @mention bypass patterns
- All 243 tests pass including new security tests
- Verified all 9 bypass patterns from security report are now properly sanitized

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix sanitization bypass for mention handling Fix @mention sanitization bypass with underscore prefix Feb 12, 2026
Copilot AI requested a review from pelikhan February 12, 2026 03:10
@pelikhan pelikhan marked this pull request as ready for review February 12, 2026 03:16
Copilot AI review requested due to automatic review settings February 12, 2026 03:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a security bypass in JavaScript @mention neutralization by tightening the regex “mention boundary” so underscores immediately before @ no longer prevent sanitization.

Changes:

  • Update neutralizeAllMentions() to use [^A-Za-z0-9]instead of[^\w], closing the underscore-prefix bypass.
  • Add a new test suite covering underscore-prefixed bypass patterns.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
actions/setup/js/sanitize_content_core.cjs Adjusts the mention-boundary regex in the core mention neutralizer and adds explanatory comments.
actions/setup/js/sanitize_content.test.cjs Adds tests for underscore-prefixed mention bypass patterns.
Comments suppressed due to low confidence (1)

actions/setup/js/sanitize_content_core.cjs:306

  • In the core sanitization path there is no allowed-list logic, but the info log says “not in allowed list”. This is misleading when debugging action logs; consider changing the message for the core path (or removing it) so it accurately reflects that all mentions are being neutralized.
    // Log when a mention is escaped to help debug issues
    if (typeof core !== "undefined" && core.info) {
      core.info(`Escaped mention: @${p2} (not in allowed list)`);
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +300 to 305
// Changed [^\w`] to [^A-Za-z0-9`] to include underscore as a valid preceding character
// This prevents bypass patterns like "test_@user" from escaping sanitization
return s.replace(/(^|[^A-Za-z0-9`])@([A-Za-z0-9](?:[A-Za-z0-9_-]{0,37}[A-Za-z0-9])?(?:\/[A-Za-z0-9._-]+)?)/g, (m, p1, p2) => {
// Log when a mention is escaped to help debug issues
if (typeof core !== "undefined" && core.info) {
core.info(`Escaped mention: @${p2} (not in allowed list)`);
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new comment is misleading: the change doesn’t make underscore a “valid preceding character” for mentions, it makes underscore count as a separator so the regex matches mentions after underscores (fixing the bypass). Please reword to avoid confusion about what the character class is doing.

This issue also appears on line 303 of the same file.

Suggested change
// Changed [^\w`] to [^A-Za-z0-9`] to include underscore as a valid preceding character
// This prevents bypass patterns like "test_@user" from escaping sanitization
return s.replace(/(^|[^A-Za-z0-9`])@([A-Za-z0-9](?:[A-Za-z0-9_-]{0,37}[A-Za-z0-9])?(?:\/[A-Za-z0-9._-]+)?)/g, (m, p1, p2) => {
// Log when a mention is escaped to help debug issues
if (typeof core !== "undefined" && core.info) {
core.info(`Escaped mention: @${p2} (not in allowed list)`);
// Changed [^\w`] to [^A-Za-z0-9`] to treat underscore as a separator before mentions
// This ensures patterns like "test_@user" are matched and neutralized instead of bypassing
return s.replace(/(^|[^A-Za-z0-9`])@([A-Za-z0-9](?:[A-Za-z0-9_-]{0,37}[A-Za-z0-9])?(?:\/[A-Za-z0-9._-]+)?)/g, (m, p1, p2) => {
// Log when a mention is neutralized to help debug issues
if (typeof core !== "undefined" && core.info) {
core.info(`Escaped mention: @${p2}`);

Copilot uses AI. Check for mistakes.
Comment on lines +134 to +141
describe("@mention bypass prevention (underscore-prefixed)", () => {
// Security tests for CVE-like vulnerability where underscore before @ could bypass sanitization
// These test cases are from the security report documenting the bypass patterns

it("should neutralize @mentions preceded by underscore in function names", () => {
const result = sanitizeContent("test_@user");
expect(result).toBe("test_`@user`");
});
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new bypass tests only exercise the default path (no allowedAliases), which uses sanitizeContentCore. The underscore-prefix bypass still exists in other code paths that use the old boundary regex (e.g. sanitizeContent() when allowedAliases is non-empty, and other mention regex usages in this package). Add at least one test that calls sanitizeContent with a non-empty allowedAliases list and asserts that an underscore-prefixed disallowed mention (e.g. "ok @Allowed and test_@admin" with allowedAliases ["allowed"]) is still neutralized; then update the corresponding implementation(s) to use the same boundary fix.

Copilot uses AI. Check for mistakes.
@pelikhan pelikhan merged commit 88472d6 into main Feb 12, 2026
159 checks passed
@pelikhan pelikhan deleted the copilot/fix-mention-sanitization branch February 12, 2026 03:20
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.

2 participants