Skip to content

fix(core): execpolicy matching for redirected shell scripts - #11868

Closed
almuqrin wants to merge 3 commits into
openai:mainfrom
almuqrin:codex/fix-gh-run-view-approval-prefix
Closed

almuqrin wants to merge 3 commits into
openai:mainfrom
almuqrin:codex/fix-gh-run-view-approval-prefix

Conversation

@almuqrin

@almuqrin almuqrin commented Feb 15, 2026 •

Copy link
Copy Markdown

Summary

Fix execpolicy matching when a wrapped shell script contains redirection (for example > /tmp/log or 2>&1).

Fixes #10321.

Before this change, commands like gh run view ... > /tmp/log or xcodebuild ... 2>&1 could repeatedly prompt for approval even after selecting "Yes, and don't ask again".

Problem

Approval persistence relies on extracting inner command prefixes from wrapper invocations such as zsh -lc "...".

For redirected scripts, core/src/exec_policy.rs called parse_shell_lc_plain_commands, which rejects redirections by design. That forced fallback to the outer wrapper argv (zsh -lc ...), so saved prefix rules such as ["gh", "run", "view"] did not match future runs.

Root Cause

  • commands_for_exec_policy used a parser that only accepts "plain" word-only commands.
  • Redirected shell scripts were treated as unparseable and matched against wrapper argv instead of the actual inner command.
  • Result: approval rules appeared to be "not remembered" for many real commands that include redirection.

Fix

  1. Add execpolicy-focused shell parsing path in shell-command/src/bash.rs:
    • parse_shell_lc_commands_for_exec_policy accepts redirected statements and command attachments needed for matching (file_redirect, variable_assignment, comments).
    • It remains fail-closed for unsupported/unsafe constructs (substitutions/control-flow still reject).
  2. Switch core/src/exec_policy.rs to use parse_shell_lc_commands_for_exec_policy in commands_for_exec_policy.
  3. Add focused regression coverage in core + integration tests.

Why This Is Safe

  • The change only affects command extraction for execpolicy matching.
  • If script structure is unsupported/ambiguous, behavior still falls back conservatively to existing prompt path.
  • No broadening of execution permission outside existing prefix-rule semantics.

Reproduction (Before vs After)

Representative command

/bin/zsh -lc "gh run view --help > /tmp/codex-gh-run-view-help.log && echo saved"

Before

  • Select "Yes, and don't ask again for commands that start with gh run view".
  • Same command shape triggers approval again because parser falls back to wrapper argv.

After

  • Same saved rule matches inner command extraction.
  • No repeated approval prompt for the same command family.

Tests Added

  • core/src/exec_policy.rs
    • evaluates_redirected_shell_script_against_prefix_rules
    • commands_for_exec_policy_parses_redirected_sequences
  • shell-command/src/bash.rs
    • parse_shell_lc_commands_for_exec_policy_supports_redirects
    • parse_shell_lc_commands_for_exec_policy_rejects_substitutions
  • core/tests/suite/approvals.rs
    • gh_run_view_prefix_rule_allows_redirected_command_without_prompt

Verification Performed

Local bug reproduction

  • Reproduced in this environment using redirected wrapped command + saved prefix rule.
  • Verified fail-before/pass-after by temporarily restoring old parser call in core/src/exec_policy.rs:
    • without fix: integration test requests approval unexpectedly,
    • with fix restored: integration test passes.

Local checks run

  • cargo test -p codex-core redirected -- --nocapture
  • cargo test -p codex-core gh_run_view_prefix_rule_allows_redirected_command_without_prompt -- --nocapture
  • just fix -p codex-core

All passed.

Related Reports

Existing PRs Checked

This PR complements those by covering non-heredoc redirection command extraction.

@github-actions

github-actions Bot commented Feb 15, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@almuqrin

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Feb 15, 2026
@etraut-openai

Copy link
Copy Markdown
Contributor

We've updated our contribution guidelines to indicate that we're no longer accepting unsolicited code contributions. All code contributions are by invitation only. To read more about why we've taken this step, please refer to this announcement.

@almuqrin

Copy link
Copy Markdown
Author

@etraut-openai understandable, please check the issue I linked, and, keep in mind that:

  1. I've reproduced the issue locally.
  2. I've verified fail-before/pass-after:
    1. Before my changes: integration test requests approval unexpectedly
    2. After my changes: integration test passes

@erikeckhardt

Copy link
Copy Markdown

This change, broadening execpolicy parsing to accept redirected shell scripts, also appears to allow variable_assignment nodes and then ignore them when reconstructing the command prefix. That means a rule like prefix_rule(pattern=["gh", "run", "view"], decision="allow") will match environment-prefixed invocations such as:

LD_PRELOAD=/tmp/x gh run view

This is not fully safe, because LD_PRELOAD tells a Unix dynamic linker to load a shared library before the program’s normal libraries. This can be useful, but is capable of completely changing a given command's behavior, to potentially allow unsafe/unexpected execution behavior. LD_PRELOAD can be useful for debugging, profiling, or instrumentation, but makes LD_PRELOAD=/tmp/x gh run view meaningfully different from plain gh run view, which is likely outside a user’s expectation when approving ordinary use of that command.

LD_PRELOAD is only one example here. Mitigating that specific variable would not address the underlying issue: environment pre-assignment in general can materially change execution semantics while still matching the same reconstructed command prefix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asks for permission to run command xcodebuild, even after permitting all xcodebuild commands

3 participants