feat(policy): implement project-level policy support#18682
feat(policy): implement project-level policy support#18682Abhijit-2592 merged 14 commits intomainfrom
Conversation
Summary of ChangesHello @Abhijit-2592, 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 Gemini CLI's policy engine by introducing support for project-level policies. This new tier provides a crucial layer of granular control, allowing developers to define specific tool execution rules within their project's configuration, bridging the gap between global user preferences and system-wide administrative mandates. A key security enhancement ensures these project policies are only applied in trusted workspaces, preventing potential malicious overrides. Highlights
Changelog
Activity
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. 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
|
|
Size Change: +13.3 kB (+0.05%) Total Size: 24.6 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Code Review
This pull request implements support for project-level policies, introducing a new 'Project' tier into the policy engine's hierarchy, allowing repositories to define tool execution rules. The changes are well-structured, with updates to the core policy logic, storage configuration, and CLI wiring, and the documentation has been updated. However, despite the inclusion of a workspace trust check to prevent loading policies from untrusted folders, the implementation is vulnerable to a trust bypass. If local workspace settings can disable the folder trust mechanism, a malicious repository could force itself to be trusted and load arbitrary policies, potentially leading to Remote Code Execution. This violates the rule that security-sensitive settings should not be overridden by less-trusted configuration scopes. It is recommended to perform the trust check using only verified, global settings before any local configuration is processed to mitigate this risk.
e394e26 to
ddf5a68
Compare
ddf5a68 to
93e34ed
Compare
e0504e8 to
719bb0d
Compare
jacob314
left a comment
There was a problem hiding this comment.
UI portion looks good once these comments are addressed.
a9753ed to
5d0ffb7
Compare
Documentation: Safety Checker Tier AlignmentI have updated the
I have also added unit tests to |
5d0ffb7 to
831f7bd
Compare
Introduces a new 'Project' tier (Tier 3) for policies, allowing users to define project-specific rules in `$PROJECT_ROOT/.gemini/policies`. Key Changes: - **Core**: Added `PROJECT_POLICY_TIER` (3) and bumped `ADMIN_POLICY_TIER` to 4. Updated `getPolicyDirectories`, `getPolicyTier`, and `createPolicyEngineConfig` to handle project-level policy directories. - **Storage**: Added `getProjectPoliciesDir()` to the `Storage` class. - **CLI**: Updated `loadCliConfig` to securely load project policies. Crucially, project policies are **only loaded if the workspace is trusted**. - **Tests**: Added comprehensive tests for both core policy logic and CLI integration, verifying priority hierarchy (Admin > Project > User > Default) and trust checks. This hierarchy ensures that project-specific rules override user defaults but are still subject to system-wide admin enforcement.
Adds the 'Project' tier (Base 3) to the policy engine documentation. Updates the priority hierarchy, location table, and formula examples to reflect the new Project -> User precedence.
…efault Updates the policy engine to prioritize User policies over Project-specific policies. This change is a security measure to ensure that users maintain control over their environment and are not inadvertently compromised by policies defined in a cloned repository. Key Changes: - Swapped Tier 2 (now Project) and Tier 3 (now User). - Updated documentation to reflect the new hierarchy. - Updated all built-in policy TOML files with correct tier information. - Adjusted all tests and integration test expectations to match new priority values.
…ture changes from rebase
Adds a security mechanism to detect and prompt for confirmation when project-level policies are added or modified. This prevents unauthorized policy changes from being applied silently. - PolicyIntegrityManager calculates and persists policy directory hashes. - Config integrates integrity checks during startup. - PolicyUpdateDialog prompts users in interactive mode. - --accept-changed-policies flag supports non-interactive workflows. - toml-loader refactored to expose file reading logic.
…tegration tests - Refactored `PolicyUpdateDialog` to remove side effects (`process.exit`, `relaunchApp`) and delegate logic to parent. - Updated `AppContainer` to handle relaunch logic. - Added comprehensive unit tests for `PolicyUpdateDialog`. - Fixed `project-policy-cli.test.ts` to correctly mock `PolicyIntegrityManager`. - Fixed typo in `packages/core/src/policy/config.ts`.
Updates config.test.ts to fix createPolicyEngineConfig mock expectations and expands project-policy-cli.test.ts to cover integrity check scenarios (NEW, MISMATCH) and interactive confirmation flows.
Updates the terminology and configuration for the intermediate policy tier from "Project" to "Workspace" to better align with the Gemini CLI ecosystem. Key changes: - Renamed `PROJECT_POLICY_TIER` to `WORKSPACE_POLICY_TIER`. - Renamed `getProjectPoliciesDir` to `getWorkspacePoliciesDir`. - Updated integrity scope from `project` to `workspace`. - Updated UI dialogs and documentation. - Renamed related test files.
This change eliminates the need for a CLI restart when a user accepts new or changed project-level policies. Workspace rules are now dynamically injected into the active PolicyEngine instance. Key improvements: - Added Config.loadWorkspacePolicies() to handle mid-session rule injection. - Fully encapsulated acceptance and integrity logic within PolicyUpdateDialog. - Integrated centralized keybindings (Command.ESCAPE) for dialog dismissal. - Refactored PolicyIntegrityManager tests to use a real temporary directory instead of filesystem mocks for improved reliability. - Updated copyright headers to 2026 across affected files. - Added UI snapshot tests for the policy update dialog. Addresses review feedback from PR #18682.
Simplified createPolicyEngineConfig signature by moving workspacePoliciesDir into the PolicySettings interface. Updated all core and CLI call sites and tests to align with the consolidated settings structure.
Centralized the workspace policy discovery and integrity verification logic into a new 'resolveWorkspacePolicyState' helper in the policy module. This significantly simplifies 'loadCliConfig' in config.ts, reducing its imperative bloat and removing low-level core dependencies from the main configuration flow. - Moved workspace integrity check and directory discovery to policy.ts - Refactored loadCliConfig to use the new declarative resolver - Added comprehensive unit tests for the resolver using real temp dirs - Cleaned up redundant function arguments in core and CLI calls - Verified project integrity with 'npm run preflight'
…y reliability Addressed PR review feedback by refining policy lifecycle management, improving TypeScript strictness, and streamlining CLI arguments. - Removed redundant '--accept-changed-policies' flag. - Updated non-interactive mode to automatically load changed policies with a warning. - Replaced unsafe 'as NodeJS.ErrnoException' casts with 'isNodeError(e)'. - Implemented safer TOML/JSON parsing with better validation and type guards. - Added 'removeRulesByTier' and 'removeCheckersByTier' to PolicyEngine for clean policy reloading. - Integrated a concurrency guard in PolicyUpdateDialog to prevent interleaved calls. - Updated CLI and SDK tests to align with new logic and improved type safety.
831f7bd to
ed6a20d
Compare

Summary
This PR implements support for project-level policies in the Gemini CLI. It allows users to define fine-grained tool execution rules within a project's configuration directory (
$PROJECT_ROOT/.gemini/policies), filling the gap between user-wide preferences and system-wide admin enforcement.Details
The policy engine now supports a new Project Tier (Base 2), creating the following hierarchy:
/etc/gemini-cli/policies). Always wins.~/.gemini/policies). Overrides project settings.$PROJECT_ROOT/.gemini/policies). Overrides default settings.Key Changes:
Policy Integrity (New):
policy_integrity.jsonin the global config directory.Core:
PolicyEngineConfigto accept aprojectPoliciesDir.PROJECT_POLICY_TIERconstant and updated priority calculation logic.PolicyIntegrityManagerfor hash calculation and verification.Storage: Added
getProjectPoliciesDir()andgetPolicyIntegrityStoragePath()to theStorageclass.CLI:
loadCliConfigto integrate integrity checks.PolicyUpdateDialogfor user confirmation.Documentation: Updated
docs/core/policy-engine.mdto reflect the new hierarchy and configuration options.Related Issues
Closes #18679
How to Validate
Setup:
npm run build:all.Verify Hierarchy (User > Project):
.gemini/policies/project.toml) that allows a command.~/.gemini/policies/user.toml) that denies the same command.Verify Integrity:
--prompt "hi"(non-interactive). You should see a warning and policies should NOT load.--prompt "hi" --accept-changed-policies. You should see a warning (auto-accept) and policies SHOULD load.Verify Security (Untrusted Workspace):
Pre-Merge Checklist