Repository navigation
fix: apply configured permission rules when an agent starts - #4757
Conversation
config.toml rules were parsed and then never copied into the rule list the permission policies read, so deny, ask, and allow never ran.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b8a4c67421
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules; | ||
| if (rules === undefined || rules.length === 0) return; | ||
| handle.accessor.get(IAgentPermissionRulesService).addRules(rules); |
There was a problem hiding this comment.
Honor session-runtime rules loaded from config
When a configured rule uses the documented scope = "session-runtime", this call adds it to the rule service, but evaluateUserConfiguredRule filters rules to turn-override, project, and user (permissionPolicy/policies/user-configured-rule.ts:14-18), while the session-history policy reads only separately recorded approval patterns. Consequently, a configured deny or ask rule with this accepted scope is silently ignored; either reject this scope in the config schema or include it when evaluating configured rules.
AGENTS.md reference: AGENTS.md:L129-L131
Useful? React with 👍 / 👎.
| const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules; | ||
| if (rules === undefined || rules.length === 0) return; | ||
| handle.accessor.get(IAgentPermissionRulesService).addRules(rules); |
There was a problem hiding this comment.
Preserve first-match ordering for configured rules
Loading the whole ordered config array into the existing policy chain does not provide the documented “first matching rule takes effect” behavior: permissionPolicyService.ts:39-49 evaluates every matching deny before every ask and allow, regardless of array position. For overlapping rules such as an early allow Bash followed by ask Bash, users get a prompt instead of the promised allow, so the rules need a unified ordered evaluator or the configuration contract must explicitly describe and validate decision precedence.
AGENTS.md reference: AGENTS.md:L129-L131
Useful? React with 👍 / 👎.
| const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules; | ||
| if (rules === undefined || rules.length === 0) return; | ||
| handle.accessor.get(IAgentPermissionRulesService).addRules(rules); |
There was a problem hiding this comment.
Let ask rules override auto-mode approval
When default_permission_mode = "auto" and the loaded config contains a matching ask rule, the rule still never gates the call: permissionPolicyService.ts:39-48 evaluates the unconditional AutoModeApprovePermissionPolicyService before UserConfiguredAskPermissionPolicyService, and the auto policy approves every tool. TUI and print-mode users therefore silently approve calls they explicitly configured to require confirmation, so configured ask rules must run before auto-mode approval while retaining deny precedence.
AGENTS.md reference: AGENTS.md:L129-L131
Useful? React with 👍 / 👎.
| const rules = this.config.get<PermissionConfig | undefined>(PERMISSION_SECTION)?.rules; | ||
| if (rules === undefined || rules.length === 0) return; | ||
| handle.accessor.get(IAgentPermissionRulesService).addRules(rules); |
There was a problem hiding this comment.
Reapply configured rules after undo restores state
Configured rules are added through the live-only PermissionRulesAdd event, and the existing replay test confirms that restoring state rebuilds rules as empty; conversation undo then calls dispatcher.restore() in undoService.ts:128-136, whose rerun path resets every replayable state key before replaying durable records (eventDispatcherService.ts:783-795,861-865). After the first /undo, all configured deny, ask, and allow rules therefore silently stop applying until the agent is recreated, so these rules must be reapplied after every replay or stored outside replay-reset state.
AGENTS.md reference: AGENTS.md:L129-L131
Useful? React with 👍 / 👎.
🦋 Changeset detectedLatest commit: b8a4c67 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Thanks for fixing this. We reported this behaviour to Moonshot on 9 October and again as GHSA-cmmx-h9w3-462x on 8 November. Our retest confirms that deny, ask and write rules are now enforced (fixed in 2.6.0, tested on 2.6.2). Could you set 2.6.0 as the patched version, request a CVE through GitHub and publish the advisory, so people still on 2.5.2 or earlier know they need to upgrade? Leo Ruocco, Agentic Thinking (agenticbench.org) |
Requirement or Bug
Users who wrote
[[permission.rules]]in~/.kimi-code/config.tomlexpected those rules to gate tool calls. The file loaded, but deny, ask, and allow rules never ran, in print mode and in the interactive TUI, including subagents. This PR makes a newly created or restored agent apply the rules already parsed from config.Bug Reproduction Steps
~/.kimi-code/config.toml:kimi -p, and ask the agent to read that file.Root Cause
[[permission.rules]]is parsed into the config section, and the permission policy chain already consultsIAgentPermissionRulesService.rules. Nothing copied the parsed rules into that list.addRules()had no production caller, so the list stayed empty and every user-configured policy missed. This is a fundamental fix: agent creation now copies the parsed rules into the list the policies already read.Code Changes
Agent creation already copies
default_permission_modeinto the agent after wire restore. The same step now copiespermission.rules.bindBootstrap bind profile apply default permission mode unless the session already chose one + copy permission.rules from config into the agent rule listSubagents, forks, and
/reloadall go throughAgentLifecycleService.create, so they pick up the same copy. Rules are not written to the wire log, so a restored session reads the current config instead of replaying a stale copy. An already-open session does not see a config edit until it is created again (/reload, or a newkimi -prun).Start at
packages/agent-core-v2/src/session/agentLifecycle/agentLifecycleService.ts. The policy chain and the pattern matcher are unchanged.Behavior Changes and Affected Users
[[permission.rules]]deny / ask / allow on tool calls/reload, or start a new session.Affected modules: agent creation in
agent-core-v2, consumed by the CLI (TUI andkimi -p), desktop, web, and ACP through the same server. Tests cover a new agent, a subagent, and a restored agent loading the current rules. The existing policy-chain tests already deny, ask, and allow once the rule list is populated.Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.