Skip to content

fix(exec): replace TOCTOU check-then-read with atomic pinned-fd open in script preflight [AI] - #62333

Merged
pgondhi987 merged 11 commits into
openclaw:mainfrom
pgondhi987:fix/fix-247
Apr 9, 2026
Merged

pgondhi987 merged 11 commits into
openclaw:mainfrom
pgondhi987:fix/fix-247

Conversation

@pgondhi987

@pgondhi987 pgondhi987 commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: The script preflight validator called assertSandboxPath(), fs.stat(), and fs.readFile() as three separate operations on a mutable pathname, leaving a race window between validation and read. A symlink swap between those steps could redirect the final read to a file outside the sandbox/workspace boundary.
  • Why it matters: The sandbox boundary check was effectively bypassed during the TOCTOU window. Observable impact is limited to information derived from the swapped file's content by the validator (a matched token, a line number, or the first non-empty JS line), but the workspace boundary guarantee was broken.
  • What changed: Replaced the three-step pattern with a single readPathWithinRoot call from src/infra/fs-safe.ts, which opens the file with O_NOFOLLOW, performs post-open identity and boundary verification, and reads through the already-verified file descriptor — with maxBytes: 512 * 1024 to preserve the size gate. All SafeOpenError codes now cause the file to be skipped (best-effort preflight), and non-SafeOpenError failures are re-thrown.
  • What did NOT change: Command execution flow, approval/allowlist logic, and all other preflight checks are untouched.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • This PR fixes a bug or regression

Root Cause (if applicable)

  • Root cause: fs.readFile() re-opened the file by pathname after separate assertSandboxPath and fs.stat checks. Replacing the path between checks and read is possible in environments where the agent has write access to the working directory.
  • Missing detection / guardrail: No post-open identity verification; file descriptor was never pinned before boundary validation.
  • Contributing context (if known): Pattern predates the readPathWithinRoot / openVerifiedLocalFile safe-open helpers that now exist in src/infra/fs-safe.ts.

Regression Test Plan (if applicable)

Two new test cases added in src/agents/bash-tools.exec.script-preflight.test.ts:

  • Coverage level:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Test 1 — post-open swap: Spies on fs.stat to swap the file to an out-of-workdir symlink after the file is already open. Confirms the tool does not surface a preflight error based on the swapped target's content.
  • Test 2 — pre-open swap (O_NOFOLLOW path): Spies on fs.open to swap the file to an out-of-workdir symlink before the real open is attempted. Verifies that O_NOFOLLOW causes ELOOP, which is caught as SafeOpenError("symlink") → invalid-path → continue, and no preflight error surfaces. expect(swapped).toBe(true) guards that the swap actually fired.
  • Why this is the smallest reliable guardrail: Directly simulates both TOCTOU windows (post-open and pre-open) and confirms graceful skip behaviour in both cases.
  • Existing test that already covers this (if any): None.

User-visible / Behavior Changes

None. The preflight validator still rejects scripts that contain unescaped shell variable references; the only change is how the file is safely read.

Diagram (if applicable)

Before:
assertSandboxPath(path) -> fs.stat(path) -> [TOCTOU window] -> fs.readFile(path)

After:
readPathWithinRoot({ rootDir, filePath, maxBytes })
  -> openFileWithinRoot (O_NOFOLLOW + identity verify + boundary check)
  -> readOpenedFileSafely (through pinned fd)

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No — preflight validation logic is unchanged; only the file-read mechanism is hardened.
  • Data access scope changed? No — readPathWithinRoot enforces the same rootDir = workdir constraint as assertSandboxPath did, with stronger guarantees.
  • If any Yes, explain risk + mitigation: N/A

Repro + Verification

Environment

  • OS: Linux (Ubuntu)
  • Runtime/container: Node 22 / Bun
  • Model/provider: N/A
  • Integration/channel: N/A
  • Relevant config: security: "full", ask: "off"

Steps

  1. Run the two new test cases: pnpm test src/agents/bash-tools.exec.script-preflight.test.ts
  2. Observe both "swapped script pathname" and "pre-open symlink swap" tests pass.
  3. Confirm no existing preflight tests regress.

Expected

  • All preflight tests pass, including the two new TOCTOU cases.

Actual

  • All tests pass.

Evidence

  • Failing test/log before + passing after — both new tests verify the fixed behaviour; the fs.stat-spy test would have surfaced a false preflight error with the old fs.readFile pattern.

Human Verification (required)

  • Verified scenarios: Both new test cases pass; all existing exec script preflight tests pass.
  • Edge cases checked: Pre-open and post-open swap windows; too-large, not-found, outside-workspace, invalid-path, and symlink error codes all result in continue (skip) rather than throw.
  • What you did not verify: Live runtime execution on a real host with a genuine race-condition exploit attempt.

⚠️ AI-assisted PR — changes generated by OpenAI Codex and reviewed by Claude. No human modifications to source files.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Risks and Mitigations

  • Risk: readPathWithinRoot rejects symlinks unconditionally; if any legitimate workdir scripts are accessed via symlinks inside the workdir, preflight validation would silently skip them (best-effort, so execution still proceeds).
    • Mitigation: The previous assertSandboxPath path also rejected symlinks via assertNoPathAliasEscape, so the effective behaviour for symlinked scripts is unchanged.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S labels Apr 7, 2026
@greptile-apps

greptile-apps Bot commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR replaces a three-step TOCTOU-vulnerable pattern (assertSandboxPath → fs.stat → fs.readFile) in the exec script preflight with a single atomic readFileWithinRoot call that pins the file descriptor via O_NOFOLLOW, performs post-open identity and boundary verification, and reads through the already-verified descriptor. It also fixes a latent bug in isPathInside where files with names starting with .. (e.g. ..bad.js) inside the workspace were incorrectly classified as outside it, and adds allowSymlinkTargetWithinRoot to let preflight validate symlinked script entrypoints whose targets remain within the workspace.

Confidence Score: 5/5

Safe to merge — the TOCTOU fix is correct, the isPathInside regex fix is correct, and all remaining findings are informational.

Both core changes (TOCTOU fix via atomic readFileWithinRoot, and isPathInside regex tightening) are verified correct by reading the implementation and cross-checking the test coverage. The double sameFileIdentity guard in openVerifiedLocalFile robustly handles post-open swaps even when allowSymlinkTargetWithinRoot=true. All new tests are well-structured and the swap-guard assertions (expect(swapped).toBe(true)) prevent vacuous passes. No P0 or P1 issues found.

No files require special attention.

Vulnerabilities

The PR closes a concrete TOCTOU window in script preflight: the old assertSandboxPath → fs.stat → fs.readFile triple allowed a symlink swap between boundary validation and the final read, potentially exposing content from outside the workspace boundary to the preflight validator. The replacement (readFileWithinRoot with O_NOFOLLOW + post-open identity check + realPath boundary check) pins the file descriptor atomically, and the double sameFileIdentity guard detects swaps occurring between open and stat. The new allowSymlinkTargetWithinRoot option deliberately relaxes O_NOFOLLOW but still enforces the boundary via isPathInside(rootWithSep, opened.realPath) and identity verification, which is the correct trade-off for in-workdir symlinks. No new network calls, no secret/token handling changes, no elevated permissions.

Reviews (4): Last reviewed commit: "fix: address PR review feedback" | Re-trigger Greptile

@pgondhi987

Copy link
Copy Markdown
Contributor Author

@greptile review

@pgondhi987

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff285d098b

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ab2eed53b

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved
@pgondhi987

Copy link
Copy Markdown
Contributor Author

@greptile review

@pgondhi987

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Bravo.

ℹ️ 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 894d727656

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved
@openclaw-barnacle openclaw-barnacle Bot added the maintainer Maintainer-authored PR label Apr 8, 2026
@pgondhi987

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 18eea98407

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d3eefea8b

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5cfc96c69e

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2bb8d36d18

ℹ️ 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".

Comment thread src/agents/bash-tools.exec.ts Resolved
@pgondhi987

Copy link
Copy Markdown
Contributor Author

@greptile review

@pgondhi987

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Another round soon, please!

ℹ️ 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".

@pgondhi987
pgondhi987 merged commit b024fae into openclaw:main Apr 9, 2026
steipete pushed a commit that referenced this pull request Apr 10, 2026
…in script preflight [AI] (#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
…in script preflight [AI] (openclaw#62333)

* fix: address issue

* fix: address review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* address review feedback

* fix: address review-pr skill feedback

* fix: address PR review feedback

* fix: address PR review feedback

* fix: address PR review feedback

* docs: add changelog entry for PR merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant