Skip to content

fix: harden remote Codex hook callbacks against planted links, replays and slow hook starts - #200835

Merged
freeqaz merged 6 commits into
mainfrom
fix/codex-native-hook-relay-hardening
Nov 18, 2026
Merged

freeqaz merged 6 commits into
mainfrom
fix/codex-native-hook-relay-hardening

Conversation

@freeqaz

@freeqaz freeqaz commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

When OpenClaw runs Codex in a dedicated app-server, Codex's native hooks call back to the Gateway over HTTPS with a per-turn relay credential. Three gaps weakened that path:

  • The credential file keeps the same name for the whole chat session and is absent between turns. The Codex app-server's fs/writeFile follows links, so a link planted at that name between turns made the next turn's credential (URL and token) land in a file of the planter's choosing.
  • A holder of a live relay credential who also knew the current Codex thread and turn ids could post pre_tool_use, post_tool_use and before_agent_finalize callbacks that the Gateway accepted, could replay identical bodies, and left no log line.
  • Codex starts a hook's timeout before it spawns the hook process and treats a hook it kills as non-blocking, so the tool call runs. The hook CLI started its own (shorter) deadline only after its modules loaded, so a slow process start could still run into Codex's timeout instead of returning the relay's refusal.

User Impact

Operators of dedicated Codex app-servers get a credential delivery that cannot be redirected by a link left in the credential directory, remote tool hook callbacks that are accepted once per tool use, a Gateway log record for every remote callback (refusals as warnings), and a hook that refuses the call when the Gateway does not answer in time even if the hook process was slow to start. Local (loopback) relays are unchanged apart from the deadline.

A hook process that cannot start and reach OpenClaw's code before the Codex timeout still lets the call run; the docs now say so.

Why This Change Was Made

  • Credential delivery: the app-server filesystem API offers neither no-follow nor exclusive create, and the name must stay stable because warm threads keep their hook commands. Delivery now removes whatever is at that name (fs/remove removes a link, never its target) and then writes. A process running as the same user while the turn starts can still race the two requests, but such a process can read the credential directly anyway; the docs already say the directory is not a boundary against same-user code.
  • Remote callbacks: Codex runs each tool hook once per tool use and the remote hook client never retries, so the remote bridge now requires tool_use_id on pre_tool_use and post_tool_use and accepts each event once per tool use id for the life of the relay registration; a repeat gets 409, which the hook CLI turns into its normal unavailable answer (deny for PreToolUse, no-op for PostToolUse). permission_request and before_agent_finalize can legitimately recur and have no per-call id, so they keep the existing checks. Every remote callback writes one log record with relay id, event, tool use id and status: accepted ones to the file log only, refusals as warnings.
  • Hook deadline: the relay CLI now counts its deadline from process start (performance.now() origin). The deadline was already a little under the Codex hook timeout it is installed with, and a timed-out PreToolUse already returned a deny.

Evidence

Each new test was run against main sources (red) and against this branch (green) on a Linux test VM with Node 24, --maxWorkers=1:

  • extensions/codex/src/app-server/native-hook-relay-remote.test.ts, "replaces a link planted at the stable name instead of writing through it": a fake app-server that writes through links like Codex does. On main the credential lands in the linked workspace file; with the fix the workspace file is untouched and the credential is a regular file.
  • src/agents/harness/native-hook-relay.remote-http.test.ts, "accepts each remote tool callback once and logs every outcome": on main a replayed pre_tool_use gets 200 and nothing is logged; with the fix the repeat gets 409 and each callback, including the unknown-relay 403, writes one record.
  • src/cli/native-hook-relay-cli.test.ts, "counts the deadline from process start and refuses before Codex's hook timeout": on main the transport is still open 5 ms after the deadline measured from process start; with the fix it is aborted then and the hook answers deny.

With the fix, the relay suites that consume the changed code also pass (pnpm test over 18 files: the core relay, bridge, remote HTTP callback, remote client, hook CLI and CLI process tests, and the Codex extension's relay, remote credential and remote-hook thread lifecycle tests). pnpm check:changed typecheck and guards, the formatter on touched files and the docs format check pass on the same VM; the SQLite worker ratchet (--staged, no T1 call growth) and type-aware oxlint on the touched files (0 warnings, 0 errors) pass too. A full pnpm lint of the branch fails on existing max-lines errors in files this PR does not touch.

Two existing cold-process tests now pass a longer --timeout, because the relay deadline includes their TypeScript process start.

extensions/codex/src/app-server/run-attempt.native-hook-relay-retention.test.ts treated any fs/remove as early disposal of the credential; it now counts only removals after the last write (none while the yielded parent waits, one after the retained child finishes). Its remote case fails with the old assertion and passes with the new one. The relay CLI keeps no test-only parameter: its test helper moves the performance.now() origin to the call, or to a simulated process age.

Not verified: a live run against a real Codex app-server; the fix was reproduced against a fake app-server with Codex 0.164's filesystem semantics (writes follow links, removal does not).

- Remove whatever occupies the stable credential name before the app-server
  writes the relay credential: fs/writeFile follows links and has no exclusive
  create, so a link planted between turns redirected the credential.
- Accept each remote pre_tool_use and post_tool_use callback once per tool use
  id and event, require the id, and log every remote callback outcome.
- Count the hook CLI deadline from process start: Codex starts its hook timeout
  before spawning the hook, so slow startup could let Codex kill the hook and run
  the tool call instead of receiving the relay's refusal.
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation cli CLI command changes agents Agent runtime and tooling extensions: codex size: M labels Nov 17, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 17, 2026
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR removes planted credential links before delivery, rejects duplicate remote tool callbacks, records callback outcomes, and includes process startup in hook deadlines.

Example: The remote relay receives PreToolUse callback call-1 twice.

  • Before: Both requests can reach policy evaluation.
  • After: The first request proceeds and the duplicate receives HTTP 409.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused patch has no identified correctness blocker, but required real behavior evidence remains test-only.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Dedicated Codex app-server hooks can write credentials through planted links, repeat remote callbacks, or miss the opportunity to refuse a tool when startup consumes the timeout.
Reason: The supplied maintainer direction supports this focused hardening, and the implementation preserves the existing same-user trust limitation and policy owner.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR contains useful remote-hook hardening absent from current main; both earlier findings are resolved, and no new concrete code defect was found.

Priority: P2
Reviewed head: 9456117cc37ea084f53ff65ee0f9c5f72ab26c78

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: Provide redacted logs or terminal screenshots from an isolated real Codex app-server and Gateway showing planted-link replacement, replay refusal, and startup-delayed timeout denial, including a latest-stable upgrade retaining existing settings for the tightened callback contract.
Agent review details

How this fits together

Codex hook subprocesses read a projected credential and send HTTPS callbacks to the Gateway, whose existing relay owner checks registration and policy before returning a native hook response.

flowchart LR
  A[Codex adapter] --> B[Remote credential file]
  B --> C[Native hook CLI]
  C --> D[Gateway HTTPS callback]
  D --> E[Registration and replay checks]
  E --> F[Existing hook policy]
  F --> C
Loading

Technical review

Best possible solution:

Retain the focused repairs in the existing credential projection, relay registration, and CLI deadline owners.

Do we have a high-confidence way to reproduce the issue?

Source inspection establishes link-following credential delivery, duplicate callback acceptance, and startup-excluding deadlines on the base; the author reports matching red/green tests, which were not executed during this read-only review.

Is this the best way to solve the issue?

The patch is a small repair using existing owners: unlink before projection, retain event-and-tool-use replay keys for the registration lifetime, and subtract process age from the existing deadline.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 75a31ed2854c.

Provenance checked

  • Remote credential delivery keeps the original intent (90d30a1: fix(codex): native children fail with remote app-servers #166400 established a stable credential path for remote and retained native hooks; subsequent refactors serialized projection and cleanup.)
  • CLI deadline origin changes intended behavior with a stated reason (14b1ebd: fix: bound native hook relay lifetime #91550 bounded stdin and transport work below Codex's parent timeout; this PR explains why process startup must consume that same budget.)
  • Remote callback acceptance changes intended behavior with a stated reason (90d30a1: The existing callback authenticates a registration-scoped capability before invoking Gateway policy; the patch explains the stricter once-per-tool-event acceptance rule.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Kimi Yu: Raw commit 90d30a1 adds extensions/codex/src/app-server/native-hook-relay-remote.ts:26 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 90d30a1178a7; files: extensions/codex/src/app-server/native-hook-relay-remote.ts)
  • Peter Steinberger: Raw commit 4f44864 adds extensions/codex/src/app-server/native-hook-relay-remote.ts:49 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 4f44864c33c8; files: extensions/codex/src/app-server/native-hook-relay-remote.ts)

PR surface

Source +51, Tests +215, Docs +15. Total +281 across 13 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 5 54 3 +51
Tests 7 308 93 +215
Docs 1 20 5 +15
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 13 382 101 +281

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: This is bounded hardening of dedicated remote Codex hooks, with no demonstrated widespread outage or newly introduced bypass.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (2 earlier review cycles)
  • reviewed 2026-11-17T23:13:34.278Z sha 34986d3 :: needs changes before merge. :: [P2] Update retention assertions for the new preparation removal | [P2] Keep simulated process age out of the production CLI API
  • reviewed 2026-11-17T23:38:24.768Z sha 2b47650 :: needs maintainer review before merge. :: none

Reviewed November 18, 2026, 1:19 AM ET / 06:19 UTC (Revision 3).

- The child-retention test treated any fs/remove as early disposal. Delivery
  now removes the stable credential name before it writes, so count only the
  removals after the last write: none while the yielded parent waits, one
  after the retained child finishes.
- Drop the process-start override from the relay CLI entry points. The
  deadline always counts from the performance.now() origin, and the test
  helper moves that origin to the call (or a simulated process age) instead.
@freeqaz

freeqaz commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Both findings are addressed in 2b47650:

  • Retention assertions: run-attempt.native-hook-relay-retention.test.ts now counts only fs/remove requests after the last fs/writeFile, so the preparation unlink is not mistaken for disposal. It still proves the credential survives the parent yield (no removal after delivery) and is removed once after the retained child finishes. The remote case fails with the old assertion and passes with the new one.
  • Test-only seam: runNativeHookRelayCli and runNativeHookRelayCliFromArgv no longer take a process-start argument; the deadline always counts from the performance.now() origin. The test helper moves that origin to the call (or by a simulated processAgeMs), wrapping whatever clock the test installed.

The relay suites (10 files, 147 tests) pass with these changes.

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Nov 17, 2026
@freeqaz

freeqaz commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The head is now 9456117. Since the reviewed head (2b47650) the branch only merged main four times; the change against main is unchanged (13 files, +382/-101, same patch ID).

@clawsweeper

clawsweeper Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@freeqaz
freeqaz merged commit 7de7053 into main Nov 18, 2026
124 checks passed
@freeqaz
freeqaz deleted the fix/codex-native-hook-relay-hardening branch November 18, 2026 06:21
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 19, 2026
…s and slow hook starts (openclaw#200835)

* fix(codex): harden the remote native hook relay

- Remove whatever occupies the stable credential name before the app-server
  writes the relay credential: fs/writeFile follows links and has no exclusive
  create, so a link planted between turns redirected the credential.
- Accept each remote pre_tool_use and post_tool_use callback once per tool use
  id and event, require the id, and log every remote callback outcome.
- Count the hook CLI deadline from process start: Codex starts its hook timeout
  before spawning the hook, so slow startup could let Codex kill the hook and run
  the tool call instead of receiving the relay's refusal.

* test(codex): follow the credential delivery change in the relay tests

- The child-retention test treated any fs/remove as early disposal. Delivery
  now removes the stable credential name before it writes, so count only the
  removals after the last write: none while the yielded parent waits, one
  after the retained child finishes.
- Drop the process-start override from the relay CLI entry points. The
  deadline always counts from the performance.now() origin, and the test
  helper moves that origin to the call (or a simulated process age) instead.
RomneyDa added a commit that referenced this pull request Nov 19, 2026
…s and slow hook starts (#200835)

* fix(codex): harden the remote native hook relay

- Remove whatever occupies the stable credential name before the app-server
  writes the relay credential: fs/writeFile follows links and has no exclusive
  create, so a link planted between turns redirected the credential.
- Accept each remote pre_tool_use and post_tool_use callback once per tool use
  id and event, require the id, and log every remote callback outcome.
- Count the hook CLI deadline from process start: Codex starts its hook timeout
  before spawning the hook, so slow startup could let Codex kill the hook and run
  the tool call instead of receiving the relay's refusal.

* test(codex): follow the credential delivery change in the relay tests

- The child-retention test treated any fs/remove as early disposal. Delivery
  now removes the stable credential name before it writes, so count only the
  removals after the last write: none while the yielded parent waits, one
  after the retained child finishes.
- Drop the process-start override from the relay CLI entry points. The
  deadline always counts from the performance.now() origin, and the test
  helper moves that origin to the call (or a simulated process age) instead.

(cherry picked from commit 7de7053)

Co-authored-by: RomneyDa <[email protected]>
github-actions Bot pushed a commit to clawhaven/openclaw that referenced this pull request Nov 23, 2026
…s and slow hook starts (openclaw#200835)

* fix(codex): harden the remote native hook relay

- Remove whatever occupies the stable credential name before the app-server
  writes the relay credential: fs/writeFile follows links and has no exclusive
  create, so a link planted between turns redirected the credential.
- Accept each remote pre_tool_use and post_tool_use callback once per tool use
  id and event, require the id, and log every remote callback outcome.
- Count the hook CLI deadline from process start: Codex starts its hook timeout
  before spawning the hook, so slow startup could let Codex kill the hook and run
  the tool call instead of receiving the relay's refusal.

* test(codex): follow the credential delivery change in the relay tests

- The child-retention test treated any fs/remove as early disposal. Delivery
  now removes the stable credential name before it writes, so count only the
  removals after the last write: none while the yielded parent waits, one
  after the retained child finishes.
- Drop the process-start override from the relay CLI entry points. The
  deadline always counts from the performance.now() origin, and the test
  helper moves that origin to the call (or a simulated process age) instead.

(cherry picked from commit 7de7053)
Release adaptation: approved 2026.11.7 models replay; see models-hook-safe-v2/REPORT.md.
Co-authored-by: RomneyDa <[email protected]>
tideclaw-bot pushed a commit that referenced this pull request Nov 24, 2026
…s and slow hook starts (#200835)

Baseline adaptation: Adapt callback custody, credential publication and process-start deadlines to the shipped remote Codex bridge and CLI, retaining owner checks and shipped fail-closed policy.

(cherry picked from commit 7de7053)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 046bfba2ba80b221c5305278d398c75e6f4a04b1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling cli CLI command changes docs Improvements or additions to documentation extensions: codex P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant