Skip to content

fix(core): stop matching 401 as a substring in isAuthenticationError - #29242

Open
winklemad wants to merge 1 commit into
google-gemini:mainfrom
winklemad:fix/auth-error-401-substring
Open

winklemad wants to merge 1 commit into
google-gemini:mainfrom
winklemad:fix/auth-error-401-substring

Conversation

@winklemad

Copy link
Copy Markdown

Summary

isAuthenticationError fell back to message.includes('401'), which matches any error message containing 401 as a substring — a port number like 4012, an id, or a line number — and misreports those as authentication errors. That can trigger a spurious re-auth / logout flow on errors that have nothing to do with auth.

Details

The other branches of isAuthenticationError (numeric code === 401, UnauthorizedError) are precise; only the string fallback was too loose. This changes it to match 401 as a standalone number token (\b401\b) instead of a raw substring, so a larger number that merely contains 401 no longer matches, while real messages like HTTP 401, 401 Unauthorized, Status code: 401, and the MCP SDK's Error POSTing to endpoint (HTTP 401): ... still resolve.

Related Issues

Fixes #28203

How to Validate

cd packages/core
npx vitest run src/utils/errors.test.ts

The isAuthenticationError suite adds cases that the old substring check failed:

  • true (unchanged): 401 Unauthorized, HTTP 401, Status code: 401, Error POSTing to endpoint (HTTP 401): denied
  • false (previously true): listening on port 4012, connection refused at 127.0.0.1:4015, processed 24013 records, error at line 1401

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any) — none; behavior only narrows to genuine 401s
  • Validated on required platforms/methods:
    • MacOS
      • npm run (vitest, all 30 tests in errors.test.ts pass; RED confirmed on the prior code)

isAuthenticationError fell back to `message.includes('401')`, which
matched any message containing "401" as a substring — a port like 4012,
an id, or a line number — and misreported them as authentication errors.

Match 401 as a standalone number token (`\b401\b`) instead, so real
"HTTP 401" / "401 Unauthorized" messages still resolve while numeric
substrings do not.

Fixes google-gemini#28203
@winklemad
winklemad requested a review from a team as a code owner September 8, 2026 00:55
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 refines the logic used to identify authentication errors within the core package. By tightening the string matching criteria for 401 status codes, the change eliminates false positives that previously triggered unnecessary re-authentication flows when unrelated numbers appeared in error messages.

Highlights

  • Improved Error Detection: Updated the isAuthenticationError utility to use a regex word boundary check (\b401\b) instead of a simple substring match.
  • Reduced False Positives: Prevented spurious authentication errors caused by numbers containing '401' (e.g., port 4012 or line numbers) being incorrectly identified as HTTP 401 errors.
  • Expanded Test Coverage: Added new test cases to verify that legitimate 401 messages are still captured while unrelated numeric strings are correctly ignored.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added the size/s A small PR label Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

📊 PR Size: size/S

  • Lines changed: 22
  • Additions: +21
  • Deletions: -1
  • Files changed: 2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the authentication error detection in packages/core/src/utils/errors.ts by replacing message.includes('401') with a word-boundary regex /\b401\b/ to prevent false positives on larger numbers (e.g., ports), and adds corresponding test cases. The review feedback suggests further refining the regex to target specific HTTP/authentication contexts (like HTTP 401 or 401 Unauthorized) to avoid false positives on standalone, non-auth '401' values (such as user IDs or counts), and adding tests for those scenarios.

// number (e.g. a port like 4012 or an id), which produced false positives.
const message = getErrorMessage(error);
if (message.includes('401')) {
if (/\b401\b/.test(message)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

While matching \b401\b prevents matching larger numbers like 4012, it still matches any standalone 401 in the error message (e.g., 'User 401 not found', 'processed 401 records', or 'error at line 401'). Since triggering a 401 authentication error can lead to a highly disruptive spurious re-auth or logout flow, we should make this fallback check more precise by matching only known HTTP/authentication contexts (like HTTP 401, status code: 401, 401 Unauthorized, or exactly 401).

Suggested change
if (/\b401\b/.test(message)) {
if (/\bHTTP\s+401\b|\bstatus\s+code:\s*401\b|\b401\b[\s:-]*unauthorized\b|^401$/i.test(message)) {

Comment on lines +113 to 124
it('should not match 401 as a substring of a larger number', () => {
expect(isAuthenticationError(new Error('listening on port 4012'))).toBe(
false,
);
expect(
isAuthenticationError(new Error('connection refused at 127.0.0.1:4015')),
).toBe(false);
expect(isAuthenticationError(new Error('processed 24013 records'))).toBe(
false,
);
expect(isAuthenticationError(new Error('error at line 1401'))).toBe(false);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

To prevent regressions and ensure that standalone numbers like 401 in non-authentication contexts (e.g., user IDs, record counts) are not falsely matched as authentication errors, we should add explicit test cases for these scenarios.

  it('should not match 401 as a substring of a larger number or in non-auth contexts', () => {
    expect(isAuthenticationError(new Error('listening on port 4012'))).toBe(
      false,
    );
    expect(
      isAuthenticationError(new Error('connection refused at 127.0.0.1:4015')),
    ).toBe(false);
    expect(isAuthenticationError(new Error('processed 24013 records'))).toBe(
      false,
    );
    expect(isAuthenticationError(new Error('error at line 1401'))).toBe(false);
    expect(isAuthenticationError(new Error('User 401 not found'))).toBe(false);
    expect(isAuthenticationError(new Error('processed 401 records'))).toBe(false);
  });

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality labels Sep 8, 2026
@gemini-cli

gemini-cli Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Hi there! Thank you for your interest in contributing to Gemini CLI.

To ensure we maintain high code quality and focus on our prioritized roadmap, we only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@winklemad

Copy link
Copy Markdown
Author

This one's already triaged priority/p2 + area/core, so I don't think it's a drive-by: it's a genuine correctness bug. isAuthenticationError fell back to message.includes('401'), so any error whose text merely contains 401 — a port like 4012, an id, a line number — is misclassified as an authentication failure and can trigger an unwanted re-auth. The fix scopes it to a \b401\b word boundary (+21/-1, with a regression test). Could a maintainer tag it help wanted or give it a quick look before the auto-close window, given the p2 triage? Happy to adjust anything to fit the roadmap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p2 Important but can be addressed in a future release. size/s A small PR status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: isAuthenticationError falsely matches port numbers or non-auth messages containing '401'

1 participant