fix(claudecode): align acceptEdits Bash auto-approval with execution#16183
Open
Jiangrong-W wants to merge 1 commit into
Open
Conversation
In acceptEdits permission mode, Bash invocations were auto-approved when
the first whitespace-delimited token was an allowed edit verb
(mkdir/touch/mv/cp). The approval decision inspected only that lexical
prefix, but canUseTool executes the full, unchanged command string.
A compound command such as `cp ./a ./b; <second action>` therefore
skipped the approval prompt on the `cp` prefix while still running the
appended action — an approval/execution mismatch (approval_lexical_bypass).
Realign the approval decision point with the real execution point:
matchesAcceptEditsBashInvocation now refuses to auto-approve any command
containing shell chaining/redirection/substitution operators
(`;` `&&` `||` `|` `&` `$()` backticks `(){}` `<>` newlines). Only a
single simple command whose verb is allowlisted may skip the prompt;
anything else falls through to the normal approval prompt (the safe
default). Plain `mkdir tmp` / `cp a b` continue to auto-approve.
Adds regression tests covering the compound-command bypass vectors.
Signed-off-by: christop <825583681@qq.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Before this PR:
In
acceptEditspermission mode, the Claude Code Bash auto-approval gate decided whether to skip the command-approval prompt by inspecting only the first whitespace-delimited token of the command, while the runtime then executed the full, unmodified command string. Because the approval decision and the execution point disagreed on what "the command" is, a Bash invocation that merely starts with an allowlisted edit verb (mkdir/touch/mv/cp) could carry a second shell action after a separator (;,&&,||,|,&, redirection, command substitution) and have the whole string run without ever prompting the user. This affects only the non-defaultacceptEditsmode; thedefaultmode already prompts for Bash.Affected sites (decision point vs. real execution, relative to current
main):src/shared/ai/claudecode/toolRules.ts:104—matchesAcceptEditsBashInvocationnormalizes the command to its first whitespace-delimited token before checking the allowlist:src/main/ai/runtime/claudeCode/settingsBuilder.ts:623—canUseToolallows the call with the original input, so the entire payload runs unchanged:src/main/ai/tools/adapters/claudeCode/agentTools.ts:174— the live tool-policy snapshot resolves the runtime tool name together with the model-supplied tool input, feeding that input into the gate above.After this PR:
Auto-approval only fires for a single simple command whose verb is allowlisted. Before checking the verb,
matchesAcceptEditsBashInvocationrejects any command containing shell chaining / redirection / command-substitution / backgrounding operators and instead falls through to the normal approval prompt:This covers
;,&&,||,|,&,$(), backticks, grouping(){}, redirections</>, and embedded newlines — every way a second action can ride behind the allowlisted verb. The approval decision point and the real execution point now agree on what will run.Behavior:
mkdir tmp,cp a b,mv old newkeep auto-approving — no change to the intendedacceptEditsworkflow.acceptEditsmode is affected;defaultmode already prompts for Bash.Fixes #
Why we need it and why it was done in this way
The auto-approval is a lexical decision made on the first token, but the executor keeps the entire payload, so the approval can describe strictly less than what actually runs. Realigning the gate with execution removes that mismatch and prevents an unintended second shell action from running unprompted in
acceptEditsmode.The following tradeoffs were made:
The following alternatives were considered:
Links to places where the discussion took place:
Breaking changes
None. The function keeps the same signature, adds no dependencies, and preserves auto-approval for plain single edit commands. The only observable change is that a command mixing an allowlisted verb with an additional shell action now prompts in
acceptEditsmode instead of auto-approving.Special notes for your reviewer
src/shared/ai/claudecode/toolRules.ts; the new module-local literalSHELL_COMPOUND_METACHARACTERSis the only added symbol.src/shared/ai/claudecode/__tests__/toolRules.test.ts. The new casedoes not auto-approve acceptEdits Bash compound commands hidden behind an allowed first tokenfails on the pre-change code (expected 'auto' to be 'prompt') and passes with this change; the existingapplies invocation-level acceptEdits Bash defaultscase still passes, confirming single-command auto-approval is preserved.oxlintandeslintreport no issues on the changed files./ok-to-test(or equivalent approval) before CI runs in full.Checklist
This checklist is not enforcing, but it's a reminder of items that could be relevant to every PR.
Approvers are expected to review this list.
mainfor active development,v1for v1 maintenance fixes/gh-pr-review,gh pr diff, or GitHub UI) before requesting review from othersRelease note