Skip to content

fix(cli): allow restricted .env loading in untrusted sandboxed folders#17806

Merged
galz10 merged 15 commits intomainfrom
galzahavi/fix/trustfolder-untrusted-auth
Feb 4, 2026
Merged

fix(cli): allow restricted .env loading in untrusted sandboxed folders#17806
galz10 merged 15 commits intomainfrom
galzahavi/fix/trustfolder-untrusted-auth

Conversation

@galz10
Copy link
Copy Markdown
Collaborator

@galz10 galz10 commented Jan 28, 2026

Summary

Fixes a "chicken and egg" paradox where the CLI crashes due to missing authentication keys in untrusted folders, even when the user is explicitly requesting a secure sandbox environment.

Details

  • Issue: loadEnvironment would return early for untrusted folders before the sandbox was even entered, preventing .env files from loading the GEMINI_API_KEY.
  • Logic Refactor: Updated loadEnvironment in packages/cli/src/config/settings.ts to recognize when sandboxing is active (by checking both settings and raw process.argv flags).
  • Secure Passthrough: Implemented AUTH_ENV_VAR_WHITELIST to ensure that only specifically required authentication keys are loaded in this mode. All other potentially malicious variables in the untrusted .env are still filtered out.
  • Type Safety: Ensured the test suite satisfies the TypeScript compiler by using direct property assignment for the sandbox flag.

Related Issues

Fixes the reported crash: Error authenticating: Error: When using Gemini API, you must specify the GEMINI_API_KEY environment variable. when running with -s in an untrusted folder.

How to Validate

  1. Go to a new, untrusted directory.
  2. Create a .env file with GEMINI_API_KEY=your_key and MALICIOUS_VAR=oops.
  3. Run npm start -- -s "echo \$GEMINI_API_KEY && echo \$MALICIOUS_VAR".
  4. Expected Result: GEMINI_API_KEY is printed, MALICIOUS_VAR is empty/undefined, and the app starts successfully.
  5. Run tests: npm test -w @google/gemini-cli -- src/config/settings.test.ts.

Pre-Merge Checklist

  • Updated relevant documentation (N/A)
  • Added/updated tests
  • Noted breaking changes (None)
  • Validated on required platforms:
    • MacOS (Seatbelt/npm run)

Bypass folder trust check in `loadEnvironment` if sandboxing is enabled,
either via settings or CLI flags (-s, --sandbox). This prevents
authentication crashes in untrusted folders while maintaining security
through a whitelist of essential auth-related variables:
- GEMINI_API_KEY
- GOOGLE_API_KEY
- GOOGLE_CLOUD_PROJECT
- GOOGLE_CLOUD_LOCATION

All other environment variables from untrusted folders remain blocked.

Verified with new regression tests in settings.test.ts.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @galz10, 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 addresses a critical issue where the CLI would fail to load essential authentication keys from .env files when operating in an untrusted workspace, even if the user intended to run in a secure sandboxed mode. The changes implement a more robust environment loading mechanism that recognizes sandboxing requests and securely allows only whitelisted authentication variables to be loaded, thereby preventing application crashes and improving the user experience in secure contexts without compromising security.

Highlights

  • Restricted .env Loading in Untrusted Sandboxed Folders: The CLI now allows specific, whitelisted environment variables (like GEMINI_API_KEY) to be loaded from .env files in untrusted workspaces, provided a secure sandbox environment is explicitly requested. This resolves a 'chicken and egg' paradox where the CLI would crash due to missing authentication keys before the sandbox could be properly initialized.
  • Enhanced loadEnvironment Logic: The loadEnvironment function has been refactored to intelligently determine if sandboxing is active, either through explicit settings or by sniffing process.argv for relevant CLI flags (-s or --sandbox). This ensures that the environment loading logic adapts correctly to the intended execution context.
  • Secure Whitelisting of Authentication Variables: A new AUTH_ENV_VAR_WHITELIST has been introduced to explicitly define which authentication-related environment variables are safe to load in untrusted, sandboxed environments. This prevents the loading of potentially malicious or unintended variables while still allowing critical authentication information to be provided.
  • Improved Test Coverage and Type Safety: New test cases have been added to thoroughly validate the updated .env loading behavior in various trust and sandboxing scenarios. Additionally, the test suite now ensures type safety by using direct property assignment for the sandbox flag, aligning with TypeScript best practices.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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
Copy link
Copy Markdown

github-actions bot commented Jan 28, 2026

Size Change: +839 B (0%)

Total Size: 23.7 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 23.7 MB +839 B (0%)
./bundle/sandbox-macos-permissive-closed.sb 1.03 kB 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-closed.sb 3.29 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B

compressed-size-action

Copy link
Copy Markdown
Contributor

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

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 aims to resolve a critical usability issue by allowing the CLI to load whitelisted authentication-related environment variables from .env files in untrusted directories when sandboxing is active. However, the current implementation introduces significant security flaws, including a bypass of the workspace trust check due to naive argument sniffing and a potential command injection vulnerability from unsanitized environment variables loaded from untrusted sources. These vulnerabilities could enable an attacker to execute arbitrary commands if a user runs the CLI in a malicious directory. Specifically, the high-severity issue with sandbox flag detection from process.argv before full argument parsing needs to be addressed to prevent a bypass of the folder trust security feature.

Enhance security when loading essential authentication variables from
untrusted workspaces by:
- Adding `sanitizeEnvVar` to strip potential shell injection characters
  from environment variables.
- Applying sanitization in `loadEnvironment` and `setUpCloudShellEnvironment`
  when a workspace is untrusted but sandboxed.
- Refining sandbox flag detection to ignore `-s` or `--sandbox` if they
  appear as positional arguments after `--`.
- Adding comprehensive regression tests for environment sanitization,
  sandbox detection, and Cloud Shell security.

Also includes minor documentation formatting and linting fixes.
@galz10 galz10 marked this pull request as ready for review February 2, 2026 18:46
@galz10 galz10 requested a review from a team as a code owner February 2, 2026 18:46
@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Feb 2, 2026
@galz10 galz10 added 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. and removed status/need-issue Pull requests that need to have an associated issue. labels Feb 2, 2026
- Remove redundant nullish coalescing operators in loadEnvironment.
- Add documentation for early process.argv sniffing limitations.
- Replace brittle 'as Settings' casts with createMockSettings helper across multiple test files.
- Consolidate and export createMockSettings from test utilities for consistent mock generation.
- Fix unused imports and type errors in test files.
- Resolve circular dependencies in test utilities.
- Fix flakiness in FolderTrustDialog tests and increase integration test timeouts.
@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Feb 2, 2026
Copy link
Copy Markdown
Contributor

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm

@galz10 galz10 requested a review from a team as a code owner February 3, 2026 22:04
@galz10 galz10 enabled auto-merge February 3, 2026 22:38
@galz10 galz10 added this pull request to the merge queue Feb 4, 2026
Merged via the queue into main with commit aba8c5f Feb 4, 2026
26 checks passed
@galz10 galz10 deleted the galzahavi/fix/trustfolder-untrusted-auth branch February 4, 2026 01:18
@skeshive
Copy link
Copy Markdown
Contributor

skeshive commented Feb 4, 2026

/patch preview

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 4, 2026

Patch workflow(s) dispatched successfully!

📋 Details:

  • Channels: preview
  • Commit: aba8c5f662574d3e80ebd3ac70872163d0a54009
  • Workflows Created: 1

🔗 Track Progress:

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 4, 2026

🚀 Patch PR Created!

📋 Patch Details:

📝 Next Steps:

  1. Review and approve the hotfix PR: #18307
  2. Once merged, the patch release will automatically trigger
  3. You'll receive updates here when the release completes

🔗 Track Progress:

yuvrajangadsingh pushed a commit to yuvrajangadsingh/gemini-cli that referenced this pull request Feb 4, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 4, 2026

🚀 Patch Release Started!

📋 Release Details:

  • Environment: prod
  • Channel: preview → publishing to npm tag preview
  • Version: v0.28.0-preview.0
  • Hotfix PR: Merged ✅
  • Release Branch: release/v0.28.0-preview.0-pr-17806

⏳ Status: The patch release is now running. You'll receive another update when it completes.

🔗 Track Progress:

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 5, 2026

Patch Release Failed!

📋 Details:

  • Version: 0.28.0-preview.1
  • Channel: preview
  • Error: The patch release workflow encountered an error

🔍 Next Steps:

  1. Check the workflow logs for detailed error information
  2. The maintainers have been notified via automatic issue creation
  3. You may need to retry the patch once the issue is resolved

🔗 Troubleshooting:

@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 5, 2026

Patch Release Complete!

📦 Release Details:

🎉 Status: Your patch has been successfully released and published to npm!

📝 What's Available:

🔗 Links:

sidwan02 pushed a commit to sidwan02/gemini-cli-gemma that referenced this pull request Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. 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.

3 participants