-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: restrict @-mention parsing to line-start or whitespace boundaries #7876
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
Conversation
- Updated mentionRegex to require @ symbol at start of line or after whitespace - Prevents accidental mention loading from pasted logs containing @https:// or @/path mid-line - Added comprehensive test cases for boundary restrictions - Maintains backward compatibility for valid mentions while fixing the issue Fixes #7875
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.
Reviewing my own code feels like debugging in a mirror maze - everything looks backward but somehow still broken.
| */ | ||
| export const mentionRegex = | ||
| /(?<!\\)@((?:\/|\w+:\/\/)(?:[^\s\\]|\\ )+?|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/ | ||
| /(?:^|(?<=\s))(?<!\\)@((?:\/|\w+:\/\/)(?:[^\s\\]|\\ )+?|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/ |
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.
The regex pattern has become quite complex with the addition of (?:^|(?<=\s)). Have you considered adding a helper function that validates mention boundaries separately? This could improve readability and make the logic easier to maintain.
| export const mentionRegex = | ||
| /(?<!\\)@((?:\/|\w+:\/\/)(?:[^\s\\]|\\ )+?|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/ | ||
| /(?:^|(?<=\s))(?<!\\)@((?:\/|\w+:\/\/)(?:[^\s\\]|\\ )+?|[a-f0-9]{7,40}\b|problems\b|git-changes\b|terminal\b)(?=[.,;:!?]?(?=[\s\r\n]|$))/ | ||
| export const mentionRegexGlobal = new RegExp(mentionRegex.source, "g") |
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.
Is it intentional that the regex doesn't use the multiline flag? Without it, ^ only matches the absolute start of the string. Mentions at the beginning of lines after newlines currently work because \n counts as whitespace in (?<=\s), but this might be worth clarifying in the documentation.
| { input: "URL: @https://example.com.", expected: ["@https://example.com"] }, // After colon and space | ||
| { input: "Commit @a1b2c3d, then check @/file.txt", expected: ["@a1b2c3d", "@/file.txt"] }, | ||
|
|
||
| // NEW: Test cases for mentions mid-line without whitespace (should NOT match) |
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.
| - The exact word 'git-changes'. | ||
| - The exact word 'terminal'. | ||
| - It ensures that any trailing punctuation marks (such as ',', '.', '!', etc.) are not included in the matched mention, allowing the punctuation to follow the mention naturally in the text. | ||
| - **NEW**: The @ symbol must be at the start of a line or preceded by whitespace to prevent accidental matches in pasted logs. |
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.
Consider adding examples of valid vs invalid patterns in the comments to make it immediately clear for future developers. For instance, showing that '@/path/to/file.txt' at line start is valid, 'Check @problems here' after space is valid, but 'error@https://api.com' mid-word is invalid.
daniel-lxs
left a comment
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.
LGTM
Summary
This PR fixes issue #7875 by restricting @-mention parsing to only work at line-start or after whitespace, preventing accidental loading of URLs/files from pasted logs.
Problem
When users paste logs containing patterns like
@https://...or@/pathmid-line (e.g.,Error loading resource@https://api.example.com), the chat incorrectly treats them as mentions and attempts to load the content.Solution
mentionRegexpattern to require the @ symbol to be either:^)(?<=\s))Changes
src/shared/context-mentions.tsto update the regex patternsrc/shared/__tests__/context-mentions.spec.tsto verify:Screenshots:
Before:

After:

Testing
Backward Compatibility
This change maintains full backward compatibility:
\@) is preservedFixes #7875
Important
Restricts @-mention parsing to line-start or after whitespace to prevent incorrect parsing in pasted logs.
mentionRegexincontext-mentions.ts.context-mentions.spec.tsfor valid mentions (line-start, after whitespace) and invalid mentions (mid-word, after punctuation without space).This description was created by
for 2f64e13. You can customize this summary. It will automatically update as commits are pushed.