Skip to content

fix(core): enforce explicit tag length and validation in file keychain - #28523

Merged
DavidAPierce merged 2 commits into
google-gemini:mainfrom
luisfelipe-alt:bugfix/WT-engineer_472960680
Jul 27, 2026
Merged

fix(core): enforce explicit tag length and validation in file keychain#28523
DavidAPierce merged 2 commits into
google-gemini:mainfrom
luisfelipe-alt:bugfix/WT-engineer_472960680

Conversation

@luisfelipe-alt

Copy link
Copy Markdown
Contributor

Summary

This Pull Request configures explicit authentication tag length and validation
for the file-based credential storage in Gemini CLI. It ensures that the
application strictly enforces standard 128-bit (16-byte) tag lengths across all
supported Node.js runtimes and handles malformed or truncated tags gracefully.

Details

The file-based credential storage in packages/core/src/services/fileKeychain.ts
previously relied on Node.js's default behavior for AES-GCM tag length
enforcement. To improve robustness and prevent potential issues on legacy
runtimes (where short tags could be accepted without explicit configuration),
this PR applies the following changes:

  • Explicitly configures { authTagLength: 16 } in both crypto.createCipheriv
    and crypto.createDecipheriv.
  • Adds an explicit validation check to ensure the parsed authTag buffer is
    exactly 16 bytes before passing it to decipher.setAuthTag().
  • Updates loadData() to catch any tag length validation errors and wrap them
    gracefully in a user-friendly "Corrupted credentials file detected" error.

Related Issues

How to Validate

To validate these changes, run the newly created regression test suite:

  1. Run the following command from the root directory:
    npx vitest run packages/core/src/services/gcmTruncationReproduction.test.ts
  2. Verify that both test cases pass successfully:
    • should use a secure 128-bit (16-byte) AES-GCM authentication tag
    • should reject decryption of a credentials file with a truncated tag

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@luisfelipe-alt
luisfelipe-alt requested a review from a team as a code owner July 23, 2026 22:54
@github-actions github-actions Bot added the size/m A medium sized PR label Jul 23, 2026
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 251
  • Additions: +233
  • Deletions: -18
  • Files changed: 2

@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 strengthens the security of the file-based credential storage by enforcing a standard 128-bit authentication tag for AES-GCM encryption. By explicitly configuring tag lengths and adding validation checks, the changes prevent potential vulnerabilities related to tag truncation and ensure that the application handles corrupted credential files more gracefully.

Highlights

  • Explicit AES-GCM Configuration: Configured an explicit 16-byte (128-bit) authentication tag length in both encryption and decryption processes to ensure consistent security across Node.js runtimes.
  • Enhanced Validation: Added a strict check to verify that the authentication tag is exactly 16 bytes before processing, preventing the use of malformed or truncated tags.
  • Improved Error Handling: Updated the error handling logic to catch tag length validation failures and provide a clear 'Corrupted credentials file detected' message.
  • Regression Testing: Introduced a new test suite to verify correct tag length enforcement and ensure that truncated tags are rejected as expected.
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.

@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 enhances security in 'FileKeychain' by explicitly setting and validating a 16-byte AES-GCM authentication tag length, and adds corresponding unit tests. The review feedback recommends two valuable improvements: defensively validating the IV length to ensure it is exactly 16 bytes, and refactoring the error handling in 'loadData' to avoid relying on fragile, implementation-specific OpenSSL error message strings.

Comment thread packages/core/src/services/fileKeychain.ts
Comment thread packages/core/src/services/fileKeychain.ts Outdated
@luisfelipe-alt

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 enhances the security of the FileKeychain service by explicitly setting the AES-GCM authentication tag length to 16 bytes and validating the lengths of the IV and authentication tag during decryption. It also refactors error handling during data loading and adds comprehensive unit tests to verify tag length enforcement. The review feedback suggests supporting both 12-byte and 16-byte IVs during decryption to align with standard AES-GCM specifications while maintaining backward compatibility, and logging detailed errors when decryption or parsing fails to facilitate debugging.

Comment thread packages/core/src/services/fileKeychain.ts Outdated
Comment thread packages/core/src/services/fileKeychain.ts Outdated
@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Jul 23, 2026
@luisfelipe-alt
luisfelipe-alt force-pushed the bugfix/WT-engineer_472960680 branch from e34f5d8 to ec94ba1 Compare July 23, 2026 23:36
@luisfelipe-alt

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 updates the FileKeychain service to use a standard 12-byte IV and a secure 16-byte (128-bit) authentication tag for AES-256-GCM encryption, while maintaining backward compatibility for legacy 16-byte IVs. It also introduces strict validation checks for both IV and authentication tag lengths during decryption, refactors error handling in loadData to isolate file reading from decryption/parsing, and adds a comprehensive test suite to verify these changes and prevent truncation vulnerabilities. I have no feedback to provide.

@DavidAPierce

Copy link
Copy Markdown
Contributor

Looks like this just needs a few linter issues fixed:

Required code changes

Apply these code modifications to resolve the static analysis failures.

1. Remove console error in file keychain

Remove the console logging statement from
packages/core/src/services/fileKeychain.ts.

Note

Deleting this log prevents stack traces from cluttering the terminal. The UI
layer handles the thrown error gracefully.

Change the try-catch block starting at line 91:

    try {
      const decrypted = this.decrypt(data);
      // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
      return JSON.parse(decrypted) as Record<string, Record<string, string>>;
    } catch (error: unknown) {
      throw new Error(
        `Corrupted credentials file detected at: ${this.tokenFilePath}\n` +
          `Please delete or rename this file to resolve the issue.`,
      );
    }

2. Remove unused variable in reproduction test

Remove encryptedHex from the variable extraction in
packages/core/src/services/gcmTruncationReproduction.test.ts on line 48:

    const ivHex = parts[0];
    const authTagHex = parts[1];

3. Replace any cast in reproduction test

Avoid using the any type in
packages/core/src/services/gcmTruncationReproduction.test.ts on line 87. Use
a cast through unknown to keep the codebase type-safe:

    const encryptionKey = (keychain as unknown as { encryptionKey: Buffer }).encryptionKey;

otherwise, LGTM.

@luisfelipe-alt

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 updates the FileKeychain service to use a standard 12-byte IV for AES-256-GCM encryption (down from 16 bytes) and explicitly configures a 16-byte authentication tag length. It also adds validation checks during decryption to reject truncated IVs or authentication tags, and refactors the file loading logic to provide clearer error messages when a corrupted credentials file is detected. A comprehensive test suite has been added to verify these security improvements and ensure backward compatibility with legacy 16-byte IVs. Additionally, helper scripts (test-check.bat and test-check.sh) have been introduced to automate local verification steps. I have no further feedback to provide as the changes are well-implemented and thoroughly tested.

@luisfelipe-alt
luisfelipe-alt force-pushed the bugfix/WT-engineer_472960680 branch from 7a45bea to 7f5d8d7 Compare July 26, 2026 21:52
@luisfelipe-alt

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 updates the FileKeychain service to use a standard 12-byte IV (down from 16 bytes) and explicitly configures a 16-byte authentication tag length for AES-256-GCM encryption. It also adds validation checks for IV and authentication tag lengths during decryption, refactors error handling in loadData to catch and report corrupted credential files more cleanly, and introduces a comprehensive test suite to verify these security and backward-compatibility behaviors. I have no additional feedback to provide as there are no review comments.

@DavidAPierce DavidAPierce 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.

Thanks for addressing my comment.
LGTM!

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

Labels

size/l A large sized PR size/m A medium sized PR status/need-issue Pull requests that need to have an associated issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants