Skip to content

Bash PermissionRequest hooks omit the selected execution directory and environment #48064

Open

Description

@BridgeAR

What version of Codex CLI is running?

codex-cli 0.155.0-alpha.16.3, bundled with VS Code extension openai.chatgpt-26.917.62051-darwin-arm64. The source trace below is pinned to 4b1c0c30dabd08fed7d6523844f9156d982eb297.

What subscription do you have?

ChatGPT Enterprise

Which model were you using?

gpt-6-sol

What platform is your computer?

Darwin 25.6.0 arm64 arm (uname -mprs).

What terminal emulator and version are you using (if applicable)?

VS Code extension. The terminal emulator version was not recorded.

Codex doctor report

Not captured for the extension-bundled binary.

What issue are you seeing?

A Bash PermissionRequest hook can approve or deny an exec_command escalation, but its input omits the selected execution directory and environment. For commands with relative paths, the hook cannot determine which directory an approval would affect.

At the pinned revision, exec_command resolves workdir against the selected environment. The resulting ApprovalAction::ExecCommand retains cwd and environment_id. Its PermissionRequest projection passes only the command and justification to a serializer that emits command and optional description.

The top-level hook cwd cannot supply the missing context. Codex sets it from the host-local hook directory, and the Hooks documentation defines it as the session working directory. This is a missing permission-payload capability, not a claim that the documented cwd value regressed.

A policy that denies unknown directories rejects a valid approval for another worktree. A policy that treats top-level cwd as the command directory can approve an operation in the wrong location. This report does not claim that hooks replace Codex's sandbox.

What steps can reproduce the bug?

  1. Create /tmp/codex-hook-cwd/a and /tmp/codex-hook-cwd/b. Start Codex with the session directory set to a.
  2. In a disposable Codex home, configure and trust a PermissionRequest command hook with matcher ^Bash$. Have it append its stdin JSON to a file and return no decision, so the normal approval prompt remains in control.
  3. Use on-request approval and a read-only permission profile. Invoke exec_command with arguments equivalent to:
{
  "cmd": "pwd",
  "workdir": "/tmp/codex-hook-cwd/b",
  "sandbox_permissions": "require_escalated",
  "justification": "check permission hook context"
}
  1. Repeat with the same command and justification, but set workdir to /tmp/codex-hook-cwd/a.
  2. Compare each hook event's cwd and tool_input. If the requests are approved, compare the two pwd outputs.

The pinned repository has an integration test that invokes a real Bash PermissionRequest hook. Giving that test two workdirs and checking the recorded hook input would make this a deterministic regression test. The PermissionRequest omission reported here follows from the pinned producer code; I have not captured this specific event from the installed binary.

What is the expected behavior?

PermissionRequest should expose the pending command's resolved execution directory and selected environment identity in explicit fields. A relative workdir must resolve using the selected environment's path semantics.

Keep top-level cwd as the host-local session or hook directory. Keep the existing allow, deny, and normal approval behavior.

The same approval projection also drops retained context for Execve and managed-network Bash requests. Those sibling routes should expose the context available for their own pending operation.

Additional information

A separate live PreToolUse probe on the installed binary supplied an explicit workdir. The command ran there, but the captured hook input contained only tool_input.command and the session cwd. This supports the related reports below. The PermissionRequest finding above is based on the pinned source and its existing integration path.

I have a candidate branch, based on 4b1c0c30, with the implementation in commit 9b31c45e. It could serve as a base for a maintainer fix. The patch carries the selected execution directory and environment identity through:

  • PreToolUse for unified exec_command;
  • applicable Bash PermissionRequest paths;
  • immediate PostToolUse completion; and
  • terminal PostToolUse completion after write_stdin.

It may help resolve the unified-exec portions of these existing reports:

  • #33986: exec_command PreToolUse loses per-call workdir.
  • #32360: PreToolUse reports the session directory instead of the execution directory.
  • #37251: PreToolUse omits the effective directory for shell-like tools.
  • #40348: PreToolUse and PostToolUse need the effective directory, including delayed completion.

The branch does not change classic shell_command, which #32360 and #37251 also cover. Code-mode execution could not be verified on this macOS ARM64 host because its rusty_v8 build artifact was unavailable. Windows and remote execution remain unverified. On the preceding main revision, 105 focused host tests passed; the patch stayed byte-identical when moved to 4b1c0c30. just clippy -p codex-core and just fmt-check passed on 4b1c0c30. The broader codex-core suite was not all green, so this branch is a candidate base rather than a completed upstream validation.

Activity

  1. added
    bug
    on Sep 25, 2026
  2. added
    CLI
    hooks
    sandbox
    on Sep 25, 2026
  3. argszero commented on Sep 25, 2026

    @argszero

    Your producer trace holds up on current main, and there is a stronger piece of evidence than the one cited: the same ApprovalAction already exposes cwd and environment_id to the Guardian reviewer — with a dedicated rendering — and environment_id even has an accessor. The hook is the one consumer of that action which never got the attribution fields. Details below, all line numbers verified on main (58670eeac4); approvals.rs, sandboxing.rs and exec_command.rs are byte-identical to the revision your trace pins, so your analysis carries over unchanged.

    1. Where the context is dropped. ApprovalAction::ExecCommand retains environment_id, cwd: PathUri, sandbox_permissions, additional_permissions, tty (core/src/tools/approvals.rs:68-80). Its PermissionRequest projection destructures with .. and forwards only the command and justification (:169-173) into PermissionRequestPayload::bash, whose serializer has no field for anything else (core/src/tools/sandboxing.rs:134-148). Execve (:199-202) and NetworkAccess (:217-224) take the same bash() route with the same result.

    2. The tell is in the same match. The WriteStdin arm two branches below hand-builds tool_input and does emit environment_id, cwd, tty, sandbox_permissions, additional_permissions (:174-197). So the payload type is not designed to be context-free — I could not read this as deliberate minimalism, and I'd be interested if a maintainer says otherwise. This was not an accident of one commit either: the WriteStdin keys were added on purpose for the escalated-terminal approval path (#40978, a57b398351), so the hook projection has already been extended for one tool and simply never extended for exec_command.

    3. The pipeline grew the fields; the hook projection did not follow. Same file, ~400 lines down, the Guardian path consumes exactly those two fields to decide how a command's path must be interpreted: it resolves the selected environment and maps its OS to PathConvention::{Windows,Posix} (:589-612), falling back to spelling-based inference only for legacy executors that report no OS. The Guardian's own action type then carries environment_id, cwd: PathUri and guardian_cwd, documented as "Executor-native rendering sent to Guardian; cwd remains typed for attribution" (core/src/guardian/approval_request.rs:19-30), plus an environment_id() accessor over every variant (:95-104). Two consumers of one action, in the same approval, and only one can answer "which directory would this affect?" — using the word attribution for it. Since then the pipeline gained target-native paths (#42577, 7a7c188682) and executor paths for Guardian reviews (#42838, 773f0b081d) without the hook projection moving, which is the shape of lag rather than of a decision.

    4. Your PreToolUse siblings are the same root cause, reached differently. There the payload is built before any handler runs: pre_tool_use_payload returns function_hook_tool_input(arguments) — the model's raw argument JSON, Value::String-wrapped on parse failure (core/src/tools/registry.rs:133-142, :849-855), while the effective directory only exists later, after workdir is joined onto the environment cwd (core/src/tools/handlers/unified_exec/exec_command.rs:196-205). Note the escape hatch right above it: post_tool_use_payload prefers result.post_tool_use_input(...) and only falls back to the raw arguments (registry.rs:99-131). So the codebase already has a "let the tool supply richer hook-facing input" seam — post-execution only. Pre-execution events have no equivalent, which is why #33986, #32360, #40348 and #37251 all keep landing on the same wall. I'd treat that as a separate work item from this one (different event, different plumbing), but the family is real and worth tracking together.

    5. Minimal shape, and it needs no wire change. Because tool_input is an unconstrained Value in both the struct and the published schema, the fields can be added nested inside tool_input: no new top-level field, no PermissionRequestCommandInput change (codex-rs/hooks/src/schema.rs:301-318), no schema regeneration. Suggested keys, mirroring WriteStdin so the two arms agree: cwd (resolved, expressed under the selected environment's path semantics), environment_id, and sandbox_permissions / additional_permissions / tty where the variant has them. Top-level cwd stays the host-local session/hook directory that tool_hook_cwd computes (core/src/hook_runtime.rs:248-253, used at :269). Two things worth knowing before anyone assumes the tests forbid this: assert_permission_request_hook_input only asserts absence at the top level of the hook input (core/tests/suite/hooks.rs:1213-1232), and Codex extensions inside this payload are established practice — turn_id is documented as one.

    6. A small independent fix in the same area. Top-level cwd has no description in the generated schema at all — it is only "type": "string", in both the PermissionRequest and PreToolUse input schemas, because descriptions in these files come from Rust doc comments (schema.rs:287 and :310 carry none while turn_id does). Documenting it there would put the session-directory semantics you had to look up in external docs into the artifact a hook author actually reads, and it is landable on its own.

    7. Test hook you already identified. permission_request_hook_sees_raw_exec_command_input (core/tests/suite/hooks.rs:3286-3355) runs a real Bash PermissionRequest hook through the mock server and asserts the logged tool_input; giving it two workdirs and asserting the resolved directory plus environment_id turns your report into a deterministic regression test using existing fixtures.

    I'm not claiming a cwd regression or that hooks should replace the sandbox — your framing is right and the failure mode is exactly the one you describe: a policy keyed on the top-level cwd neither rejects the valid approval nor catches the wrong directory. The open question is whether the team wants these fields in the hook contract at all; if the answer is yes, tool_input is the cheap place for them.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIbughookssandbox

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions