gemini-cli - ✅(Solved) Fix bug: API key validation guard is effectively dead code in handleApiKeySubmit [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#25456Fetched 2026-04-16 07:06:15
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×2cross-referenced ×1

The validation condition in handleApiKeySubmit (packages/cli/src/ui/AppContainer.tsx) is logically contradictory and never triggers, allowing empty or whitespace-only strings to be saved as valid API keys.

Error Message

Any API key that is empty or contains only whitespace should be rejected with an error message before calling saveApiKey().

Root Cause

The validation condition in handleApiKeySubmit (packages/cli/src/ui/AppContainer.tsx) is logically contradictory and never triggers, allowing empty or whitespace-only strings to be saved as valid API keys.

Fix Action

Fixed

PR fix notes

PR #25453: fix: correct API key validation logic in handleApiKeySubmit

Description (problem / solution / changelog)

Summary

Fix a contradictory condition in handleApiKeySubmit that effectively disabled the API key validation guard.

Problem

The previous condition:

was logically contradictory:

  • apiKey.length > 1 requires 2+ characters

Combined with &&, this only matched strings of 2 or more whitespace characters. This means:

  • An empty string "" (length 0) → bypasses the guard → saved as API key
  • A single space " " (length 1) → bypasses the guard → saved as API key

The error message itself ("API key cannot be empty string with length greater than 1") hints at the confusion.

Fix

Replace with the straightforward check:

This correctly rejects any key that is empty or contains only whitespace characters, regardless of length.

Testing

  • No existing tests exercise this specific validation path with whitespace-only input.
  • The fix converts a dead-code guard into a functional one, so it is strictly an improvement with no regression risk for valid API keys.

Fixes #25456

Changed files

  • packages/cli/src/ui/AppContainer.tsx (modified, +2/-4)

Code Example

onAuthError('API key cannot be empty string with length greater than 1.');
  return;
}

---

onAuthError('API key cannot be empty or whitespace only.');
  return;
}
RAW_BUFFERClick to expand / collapse

Description

The validation condition in handleApiKeySubmit (packages/cli/src/ui/AppContainer.tsx) is logically contradictory and never triggers, allowing empty or whitespace-only strings to be saved as valid API keys.

Current Behavior

The current condition:

  onAuthError('API key cannot be empty string with length greater than 1.');
  return;
}
  • apiKey.length > 1 requires 2 or more characters

Combined with &&, this only matches strings of 2 or more whitespace characters. The following invalid inputs all bypass the guard:

  • Empty string "" (length 0) → saved as API key ✅ (should be rejected)
  • Single space " " (length 1) → saved as API key ✅ (should be rejected)

Expected Behavior

Any API key that is empty or contains only whitespace should be rejected with an error message before calling saveApiKey().

Steps to Reproduce

  1. Launch gemini-cli and reach the API key input prompt
  2. Enter a single space or leave the field empty
  3. Submit — the empty/whitespace value is accepted and saved

Proposed Fix

  onAuthError('API key cannot be empty or whitespace only.');
  return;
}

File: packages/cli/src/ui/AppContainer.tsx line 877

extent analysis

TL;DR

The validation condition in handleApiKeySubmit should be updated to correctly reject empty or whitespace-only strings as API keys.

Guidance

  • The current condition apiKey.length > 1 is insufficient and should be replaced with a check for whitespace-only strings, such as using a regular expression or the trim() method.
  • To verify the fix, test the API key input with various invalid inputs, including empty strings, single spaces, and strings containing only whitespace characters.
  • The proposed fix suggests using onAuthError with a message indicating that the API key cannot be empty or whitespace only, but the actual condition to trigger this error is missing.
  • Consider adding a condition like if (apiKey.trim() === '') to check for empty or whitespace-only strings.

Example

if (apiKey.trim() === '') {
  onAuthError('API key cannot be empty or whitespace only.');
  return;
}

Notes

The provided proposed fix only includes the error message, but not the actual condition to trigger the error. The example code snippet above demonstrates a possible solution.

Recommendation

Apply workaround: update the validation condition in handleApiKeySubmit to correctly reject empty or whitespace-only strings, such as using the trim() method as shown in the example. This will ensure that only valid API keys are saved.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING