fix(core,cli): sanitize URLs in error messages#28490
Conversation
kunalrawat425
commented
Jul 22, 2026
- Add sanitizeUrlsInMessage utility to strip trailing sentence punctuation from URLs in error messages (closes Bug: Trailing '.' in antigravity.google URL in error message causes link to not load #28052)
- Apply it to CLI auth error messages
- Add core unit tests for sanitizeUrlsInMessage
- Add sanitizeUrlsInMessage utility to strip trailing sentence punctuation from URLs in error messages (closes google-gemini#28052) - Apply it to CLI auth error messages - Add core unit tests for sanitizeUrlsInMessage
|
📊 PR Size: size/M
|
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request improves the usability of error messages by ensuring that URLs embedded within them are not rendered invalid by trailing punctuation. By stripping characters like periods or exclamation marks that often accidentally attach to links, users can more easily copy and navigate to the intended resources directly from their terminal. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a sanitizeUrlsInMessage utility to strip trailing punctuation from URLs in error messages, applying it to the sign-in error flow, and adds corresponding unit tests. The review feedback suggests making the URL sanitization regex more robust to handle quotes, parentheses, commas, and semicolons. It also recommends centralizing this sanitization directly within getErrorMessage to simplify the usage in useAuth.ts and adding comprehensive test cases to cover these edge cases.
| /** | ||
| * Strips trailing sentence punctuation (`.`, `!`, `?`) from URLs in a message | ||
| * so that links copied from error text remain valid. | ||
| */ | ||
| export function sanitizeUrlsInMessage(message: string): string { | ||
| return message.replace(/(https?:\/\/\S+?)([.!?]+)(\s|$)/g, '$1$3'); | ||
| } |
There was a problem hiding this comment.
The current regular expression /(https?:\/\/\S+?)([.!?]+)(\s|$)/g has a few limitations:
- It does not handle other common trailing sentence punctuation like commas (
,) or semicolons (;). - It fails to sanitize URLs that are enclosed in quotes (e.g.,
"https://example.com.") or parentheses (e.g.,(see https://example.com.)) because the trailing character (quote or parenthesis) prevents matching\s|$.
We can make this much more robust and clean by using a lookahead assertion (?=[\s"')]|$) and including more punctuation characters. This also avoids capturing and restoring the trailing whitespace/character.
export function sanitizeUrlsInMessage(message: string): string {
return message.replace(/(https?:\/\/\S+?)([,.;!?]+)(?=[\\s\"')]|$)/g, '$1');
}| import { | ||
| getErrorMessage, | ||
| sanitizeUrlsInMessage, | ||
| } from '@google/gemini-cli-core'; |
| onAuthError( | ||
| `Failed to sign in. Message: ${sanitizeUrlsInMessage(getErrorMessage(e))}`, | ||
| ); |
There was a problem hiding this comment.
| it('leaves messages without URLs unchanged', () => { | ||
| const msg = 'No URLs here. Just plain text.'; | ||
| expect(sanitizeUrlsInMessage(msg)).toBe(msg); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Let's add comprehensive test cases to verify the new punctuation handling (commas, semicolons), quotes, parentheses, and the centralized getErrorMessage URL sanitization behavior.
it('leaves messages without URLs unchanged', () => {
const msg = 'No URLs here. Just plain text.';
expect(sanitizeUrlsInMessage(msg)).toBe(msg);
});
it('strips trailing comma and semicolon from URL', () => {
expect(sanitizeUrlsInMessage('Go to https://example.com, or https://example.org;')).toBe(
'Go to https://example.com or https://example.org',
);
});
it('handles URLs enclosed in quotes or parentheses', () => {
expect(sanitizeUrlsInMessage('Go to "https://example.com."')).toBe(
'Go to "https://example.com"'
);
expect(sanitizeUrlsInMessage('Go to (https://example.com.)')).toBe(
'Go to (https://example.com)'
);
});
});
describe('getErrorMessage with URL sanitization', () => {
it('automatically sanitizes URLs in error messages', () => {
expect(getErrorMessage(new Error('Failed with link: https://example.com.'))).toBe(
'Failed with link: https://example.com',
);
});
});