fix(core): improve windows sandbox reliability and fix integration tests#24480
fix(core): improve windows sandbox reliability and fix integration tests#24480
Conversation
Summary of ChangesHello, 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 enhances the reliability and security of the Windows sandbox implementation. It addresses critical issues related to process isolation, filesystem permission handling, and test environment stability, ensuring that the sandbox behaves correctly in restricted environments and that integration tests are more robust across platforms. Highlights
Using Gemini Code AssistThe 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
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 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. Footnotes
|
|
Hi @ehedlund, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request enhances the Windows sandbox by improving error reporting in the C# component, refining process creation flags, and updating path validation logic to support writing to new files within allowed roots. However, several critical issues were identified: resource leaks in the C# code where kernel handles are not closed during error paths, potential path traversal vulnerabilities caused by a regression in path resolution logic, and a violation of the principle of least privilege by granting 'Full Control' instead of 'Modify' permissions to the low-integrity SID.
|
Size Change: +492 B (0%) Total Size: 34 MB
ℹ️ View Unchanged
|
8df91de to
3b187c1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
6c4aab7 to
64df5c1
Compare
e4e2a25 to
eb9aad7
Compare
eb9aad7 to
c2ffff9
Compare
galz10
left a comment
There was a problem hiding this comment.
Code Review Findings
Scope: Pull Request #24480
The PR significantly improves the reliability and security of the Windows sandbox by addressing native resource management, standardized path handling, and strengthened filesystem permissions. It also re-enables and fixes several unit and integration tests for Windows that were previously skipped or failing.
While the overall direction is excellent and fixes many fundamental issues, there are several concerns regarding resource leaks in the TypeScript code and a race condition in the native C# helper.
Metadata Review
- Title:
fix(core): improve windows sandbox reliability and fix integration tests- Adheres to Conventional Commits: Yes (
fix(core)). - Clear and descriptive: Yes.
- Adheres to Conventional Commits: Yes (
- Body:
- Provides a comprehensive summary of improvements.
- Links to related work for CI coverage.
- Inconsistency noted: The body claims to have implemented "atomic (suspended) process creation," but the code in
GeminiSandbox.csdoes not appear to use theCREATE_SUSPENDEDflag or the necessaryResumeThreadcall.
Concerns (Action Required)
1. GeminiSandbox.cs: Race Condition in Process Creation
The PR summary mentions "atomic (suspended) process creation," but the diff for GeminiSandbox.cs shows that the process is created without the CREATE_SUSPENDED flag.
- File:
packages/core/src/sandbox/windows/GeminiSandbox.cs - Issue: The process is spawned with
CREATE_BREAKAWAY_FROM_JOB(0x01000000) but notCREATE_SUSPENDED(0x4). This means there is a race condition where the process can start executing and potentially finish or escape certain job limits beforeAssignProcessToJobObjectis successfully called on line 340. - Suggested Improvement: Follow the standard pattern for Job Object assignment:
- Add
CREATE_SUSPENDEDtocreationFlags. - Call
AssignProcessToJobObject. - Call
ResumeThread(pi.hThread).
This ensures the process is within the sandbox boundaries from its first instruction.
- Add
2. Resource Leak: Missing cleanup() Calls in TypeScript
The PR adds a cleanup function to the SandboxedCommand interface, but several key callers of prepareCommand have not been updated to execute it.
- Files:
packages/core/src/services/sandboxedFileSystemService.tspackages/core/src/utils/shell-utils.ts(spawnAsync)packages/core/src/tools/grep.tspackages/core/src/tools/tool-registry.ts
- Issue: These services/tools call
sandboxManager.prepareCommandbut do not execute the returnedcleanup()function. On Windows,WindowsSandboxManagercreates a temporary directory and manifest file inos.tmpdir()for every command. Without callingcleanup(), these temporary files will leak until the OS or a manual cleanup occurs. - Suggested Improvement: Ensure all callers of
prepareCommandexecutecleanup()in afinallyblock after the spawned process terminates.
3. WindowsSandboxManager.ts: Case-Sensitivity Issue with isWithinRoot
- File:
packages/core/src/sandbox/windows/WindowsSandboxManager.ts - Context: Line 323 uses
isWithinRoot(resolved, root)to check if a new file path resides within an allowed root. - Issue:
isWithinRoot(fromfileUtils.ts) usespath.resolveandstartsWith, which is case-sensitive. On Windows, paths likeC:\Workspaceandc:\workspacerepresent the same location. If the casing of the drive letter or folder names differs between the request and the workspace path,isWithinRootwill returnfalse, resulting in an unexpected "Access Denied" error for valid paths. - Suggested Improvement: Use
isSubpathfrompackages/core/src/utils/paths.tsinstead, as it correctly handles case-insensitivity on Windows by usingpath.win32.relative.
Nits (Suggestions)
-
GeminiSandbox.cs:
- Diagnostic Output: The error message for
AssignProcessToJobObjectfailure (Line 341) could include the command line to make debugging easier in complex sandboxed environments. - Magic Numbers: The JobObject information class indices (e.g.,
9forJobObjectExtendedLimitInformation,32forJobObjectNetRateControlInformation) could be moved to named constants at the top of the class for better readability.
- Diagnostic Output: The error message for
-
WindowsSandboxManager.ts:
- Constant Placement:
LOW_INTEGRITY_SIDis defined twice (Lines 43 and 482). It should be defined once as a static constant of theWindowsSandboxManagerclass.
- Constant Placement:
-
WindowsSandboxManager.test.ts:
- Mock Brittleness: The mock for
fs.existsSyncto skipcsc.execompilation (Line 42) is effective but relies on the exact internal constantHELPER_EXE. This is acceptable for unit tests but good to keep in mind if the internal structure changes.
- Mock Brittleness: The mock for
Will address this in a separate PR since this is not specific to Windows. All 3 OSes implement All other comments have been addressed! |
Summary
This PR re-enables sandbox tests for Windows and fixes critical bugs in the Windows sandboxing implementation. It also stabilizes the test suite for CI by utilizing more reliable Windows-native tools and accounting for OS-specific networking and PTY limitations.
Details
1. Sandbox Helper Fixes (
GeminiSandbox.cs)259(STILL_ACTIVE) becauseWaitForSingleObjectwas missing.CreateProcessAsUser,AssignProcessToJobObject, andSetInformationJobObjectto simplify CI failure diagnosis.CREATE_SUSPENDEDandResumeThreadto guarantee the child process is correctly assigned to the Job Object before it starts.TerminateProcessand exhaustive handle closing in afinallyblock to prevent process orphans or leaks.2. Sandbox Manager Logic (
WindowsSandboxManager.ts)icaclslogic to explicitly grantModifypermissions to the Low Integrity SID. This ensures that files created within allowed directories inherit the correct labels for sandbox access.resolveToRealPathandisSubpathutilities to handle symlinks and junction points consistently.cleanupcallback, ensuring files are removed only after the sandboxed command has fully completed.3. Integration Test Stabilization (
sandboxManager.integration.test.ts)curl: Switched to nativecurl.exeto avoid PowerShell'sInvoke-WebRequestwrapper and alias issues.touch: Switched topowershell.exe New-Itemto avoidcmd.exeredirection operators (>) which are unreliable in sandboxed contexts.it.skipIf(Platform.isWindows)for loopback blocking tests, as Windows Job Object rate limits exempt127.0.0.1.it.skipIf(Platform.isWindows)fornode-ptytests. The Windows sandbox uses pipe interception, which is incompatible with ConPTY handle inheritance.Related Issues
Related to https://github.com/google-gemini/maintainers-gemini-cli/issues/1533.
How to Validate
npm test -w @google/gemini-cli-core -- src/services/sandboxManager.integration.test.ts.Pre-Merge Checklist