Skip to content

refactor(core): centralize path validation and allow temp dir access for tools#17185

Merged
joshualitt merged 11 commits intomainfrom
ntm/gh.17164
Jan 27, 2026
Merged

refactor(core): centralize path validation and allow temp dir access for tools#17185
joshualitt merged 11 commits intomainfrom
ntm/gh.17164

Conversation

@NTaylorMullen
Copy link
Collaborator

Summary

This PR centralizes path validation logic within the Config class and refactors 9 file system tools to use these new helpers. This change consistently allows tools to access the project's temporary directory (~/.gemini/tmp/<project_hash>) while maintaining strict workspace boundary enforcement.

Details

  • Centralized Logic: Implemented isPathAllowed and getValidationErrorForPath in Config to encapsulate workspace and project temp directory boundary checks.
  • Tool Refactoring: Updated ls, glob, grep, ripGrep, read-file, write-file, read-many-files, edit, and shell to utilize centralized validation, reducing code duplication and ensuring consistent security.
  • Security & Integrity: Maintained explicit existence and directory/file type checks for tools where appropriate (e.g., grep, ripGrep).
  • Test Coverage: Updated all associated tool tests (292 tests total) with updated mocks and assertions to reflect the new validation patterns.

Related Issues

Fixes #17164

How to Validate

Run the full tool test suite in the core package:

npm test -w @google/gemini-cli-core -- src/tools/ls.test.ts src/tools/glob.test.ts src/tools/write-file.test.ts src/tools/grep.test.ts src/tools/ripGrep.test.ts src/tools/read-file.test.ts src/tools/read-many-files.test.ts src/tools/edit.test.ts src/tools/shell.test.ts --run

All 292 tests should pass.

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

@NTaylorMullen NTaylorMullen requested a review from a team as a code owner January 21, 2026 05:57
@gemini-cli gemini-cli bot added the status/need-issue Pull requests that need to have an associated issue. label Jan 21, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @NTaylorMullen, 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 significantly enhances the security and maintainability of file system operations by centralizing path validation logic. By moving these critical checks into a dedicated Config class, the system ensures that all tools adhere to a unified security policy, preventing unintended access to files outside the project's workspace or its designated temporary directory. This refactoring streamlines the codebase, making it easier to manage and audit file access permissions.

Highlights

  • Centralized Path Validation: Introduced isPathAllowed and getValidationErrorForPath methods within the Config class to centralize logic for determining if a given absolute path is permissible for file system operations. This includes checks against both the defined workspace directories and the project's temporary directory.
  • Refactored File System Tools: Updated nine core file system tools (ls, glob, grep, ripGrep, read-file, write-file, read-many-files, edit, and shell) to leverage the new centralized path validation helpers. This significantly reduces code duplication and ensures consistent security enforcement across all tools.
  • Enhanced Security and Consistency: The refactoring ensures that all affected tools consistently allow access to the project's temporary directory (~/.gemini/tmp/<project_hash>) while strictly maintaining workspace boundary enforcement, preventing unauthorized file access outside designated areas. Specific tools like grep and ripGrep retain explicit existence and directory/file type checks.
  • Updated Test Coverage: The pull request includes comprehensive updates to the associated test suite, with 292 tests modified to reflect the new validation patterns and expected error messages, ensuring the robustness of the changes.
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.

Copy link
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 introduces a centralized path validation mechanism by adding isPathAllowed and getValidationErrorForPath methods to the Config class. These methods check if a given absolute path is within the workspace context or the project's temporary directory. The change refactors path validation logic across several tools (EditTool, GlobTool, GrepTool, LSTool, ReadFileTool, ReadManyFilesTool, RipGrepTool, ShellTool, WriteFileTool), replacing individual, often duplicated, checks with calls to the new Config methods. Corresponding test files have been updated to reflect these changes, including adjustments to mock configurations and expected error messages. A review comment highlights a potential path traversal vulnerability in the isPathAllowed method related to symbolic links in the project's temporary directory, suggesting the use of fs.realpathSync for full path resolution before validation. Other review comments recommend using the new getValidationErrorForPath helper consistently and ensuring concise, user-facing error messages.

@gemini-cli gemini-cli bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality and removed status/need-issue Pull requests that need to have an associated issue. labels Jan 21, 2026
@github-actions
Copy link

github-actions bot commented Jan 21, 2026

Size Change: +3.87 kB (+0.02%)

Total Size: 23.5 MB

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

compressed-size-action

@NTaylorMullen NTaylorMullen force-pushed the ntm/gh.17164 branch 2 times, most recently from 9bbbc80 to e62bcba Compare January 21, 2026 07:07
@NTaylorMullen
Copy link
Collaborator Author

/gemini review

Copy link
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 effectively centralizes path validation logic into the Config class, which is a great refactoring for consistency and maintainability. The changes across the various tools to adopt these new helpers are well-executed.

My review focuses on a few critical areas related to file system operations. I've identified instances where synchronous file system calls are used, which can block the event loop and impact performance. This is against the repository's guidelines. Specifically:

  • The new isPathAllowed method in Config uses fs.realpathSync.
  • The grep tool's execute method uses fs.statSync within an async function.
  • The ripGrep tool uses fs.statSync for validation.

While I understand that synchronous validation methods might constrain some of these choices, using asynchronous operations is crucial for performance. I've provided specific comments with suggestions where applicable. Addressing these points will improve the performance and robustness of the application.

@jacob314 jacob314 added the 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. label Jan 22, 2026
@joshualitt joshualitt requested review from a team as code owners January 27, 2026 19:18
NTaylorMullen and others added 9 commits January 27, 2026 19:30
…ystem tools

- Implement centralized path validation logic in the Config class (isPathAllowed, getValidationErrorForPath).
- Update all 9 file system tools (ls, glob, grep, ripGrep, read-file, write-file, read-many-files, edit, shell) to use the new Config validation helpers.
- Ensure consistent boundary and existence checks across all file system tools.
- Update test suites with necessary mocks and expectations for centralized validation.

Fixes #17164
- Refactor Config.isPathAllowed to use fs.realpathSync and isSubpath for robustness against path traversal and Windows drive letter casing.
- Update 10 tool tests to use the new validation pattern in their mocks.
- Refactor RipGrepTool, GrepTool, and ReadManyFilesTool to provide concise human-facing error messages while maintaining detailed agent feedback.
- Add consistent path validation to EditTool and WriteFileTool execute methods.

Fixes #17164
…messages, and use centralized validation

- Refactored execute methods in Grep, RipGrep, Shell, Edit, and WriteFile tools to use asynchronous file system operations.
- Updated path validation error messages to 'Path not in workspace' for better clarity.
- Consolidated path validation in RipGrep and ReadManyFiles tools using Config.getValidationErrorForPath.
- Updated test mocks and expectations to match the new error messages.

Part of #17164
Renamed the path security validation helper to more accurately reflect its purpose of enforcing access boundaries.

Part of #17164
… methods

- Added getWorkingDir and getContentGenerator to mockConfig in CLI hook tests.
- Added getCurrentSequenceModel to MockedGeminiClientClass in useGeminiStream.test.tsx.
- Fixed a Vitest hoisting issue with MockValidationRequiredError.

These fixes resolve TypeErrors observed in Windows CI logs.

Part of #17164
- Used toLowerCase() for path comparison on Windows in isPathAllowed.
- Added a realpath helper to handle non-existent paths gracefully.
- Imported node:os for platform detection.

This should resolve silent test failures on Windows caused by drive letter casing mismatches.

Part of #17164
@joshualitt joshualitt added this pull request to the merge queue Jan 27, 2026
Merged via the queue into main with commit 5f569fa Jan 27, 2026
25 checks passed
@joshualitt joshualitt deleted the ntm/gh.17164 branch January 27, 2026 21:27
sidwan02 pushed a commit to sidwan02/gemini-cli-gemma that referenced this pull request Feb 6, 2026
…for tools (google-gemini#17185)

Co-authored-by: Your Name <joshualitt@google.com>
kuishou68 pushed a commit to iOfficeAI/aioncli that referenced this pull request Feb 27, 2026
…for tools (google-gemini#17185)

Co-authored-by: Your Name <joshualitt@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Shell Redirection #4] ReadFolder and other FS tools cannot access project temp directory

4 participants