Skip to content

fix(cycle): let read-only stages write only inside their $TMPDIR - #273

Merged
datacas merged 3 commits into
mainfrom
fix/272-readonly-stage-tmpdir
Oct 19, 2026
Merged

datacas merged 3 commits into
mainfrom
fix/272-readonly-stage-tmpdir

Conversation

@datacas

@datacas datacas commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Summary

Since #269, a read-only stage (review, rereview, issue_review) cannot write any file, so the comment body its skill writes to $TMPDIR/pr-comment.md for gh pr comment --body-file never exists, and no review can publish. Observed on PR #271's round 0 review.

  • check_file_edit: a non-writing stage may edit only paths that resolve strictly inside policy.tmpdir (via inside_tmpdir, which resolves .. and symlinks). Writing stages keep their rules; .git and Git configuration stay refused for every stage.
  • allowed_tool_patterns: adds one Edit(//<tmpdir>/**) rule (plus the resolved spelling when it differs), so Claude's --permission-mode default approves the same write in -p mode. Windows paths use Claude's //c/... form.
  • Docs (docs/host-permissions.md) and the executor comment updated.

Verification

  • python3 -m unittest discover -s tests: 1601 tests, OK.
  • python3 scripts/validate-package.py: VALID.
  • New tests: allowed $TMPDIR targets; denied repository, $TMPDIR itself, sibling code-cycle-tmp-*, .. traversal, a symlink inside $TMPDIR pointing out, .git inside $TMPDIR, relative paths, and a missing tmpdir; exactly one Edit rule per role; end to end Write → gh pr comment --body-file $TMPDIR/pr-comment.md through both barriers.
  • Live probe with Claude Code 2.1.303 (-p --permission-mode default --allowedTools "Edit(//<dir>/**)"): the write inside the directory succeeded and the write outside it was blocked.
  • Not verified yet: a real review publishing on GitHub. That happens on the next cycle after merge.

Closes #272

Since #269 a review, rereview or issue review could write no file, so the
comment body its skill writes to $TMPDIR/pr-comment.md for --body-file
could not exist and no review could publish. The hook now allows those
stages to edit paths that resolve strictly inside the dispatch's TMPDIR,
and allowed_tool_patterns grants one Edit(//<tmpdir>/**) rule so Claude's
default permission mode approves the same write. The repository, .git,
Git's configuration, sibling temporary directories, '..' and symlinks out
of TMPDIR stay refused.

Closes #272
@datacas

datacas commented Oct 19, 2026

Copy link
Copy Markdown
Owner Author

Code Cycle initial review

[CCR-20261019-001] · senior_reviewer · anthropic/claude-opus-5-5→? · high · schema:2

Verdict: BLOCKED (environment) — code review of head 1400f3997b46ac030dda2eb006475dd996f307b7 is complete, and no open finding blocks approval. But the configured full suite python3 -m unittest discover -s tests could not pass inside this reviewer's sandbox: 1 failure and 62 errors, all caused by the executor's filesystem sandbox and all in modules this PR does not touch (details below). A failed run is not a passing run, so the stage stops BLOCKED with an environment_blocker. A retry on an executor without these restrictions should rerun the suite; nothing in the code needs to change for that.

Summary and scope

Reviewed the full accumulated diff origin/main...1400f39 (merge base 3091cf39efa6cdc74621cb9a0e9460b2ee9856ff, 176 lines, 4 files): scripts/stage_permissions.py, scripts/executors.py (comment only), docs/host-permissions.md, tests/test_stage_permissions.py.

The fix is correct for its stated goal (#272): a non-writing stage (review, rereview, issue_review) may now edit only paths that inside_tmpdir resolves strictly inside policy.tmpdir, and allowed_tool_patterns grants one root-anchored Edit(//<tmpdir>/**) rule (plus the resolved spelling when it differs) so --permission-mode default approves the same write. Checked in detail:

  • check_file_edit: the $TMPDIR test runs before the relative-path branch, so a relative path is refused for a non-writing stage (inside_tmpdir requires an absolute path); the .git and Git-configuration checks still run afterwards for every stage, so $TMPDIR/.git/config stays refused; tmpdir=None refuses everything, as before.
  • inside_tmpdir resolves .. and symlinks with strict=True on the root, so $TMPDIR itself, a sibling code-cycle-tmp-*, .. traversal and an outward symlink are all refused; the table grants no ln, so a stage cannot plant a symlink itself.
  • _absolute_rule_path: POSIX /tmp/x/ → //tmp/x; a Windows drive path → //c/.... Writing roles also receive the rule, which is harmless (they already run with acceptEdits, and the hook keeps their rules).
  • Tests use a resolved base directory, so the "exactly one Edit rule" assertion holds on macOS (/var → /private/var) too.

Findings

[REV-001] · medium · open · - · blocks:no — The Edit grant covers host-owned files the executor keeps in the same $TMPDIR

scripts/stage_permissions.py:403, scripts/stage_permissions.py:1083

The new rule allows a non-writing stage to edit anything under the dispatch $TMPDIR, not just the comment body, report and check logs the skills write there. Claude Code places its own per-session state inside that same directory. In this very dispatch (policy tmpdir /tmp/code-cycle-tmp-a08vjizd), ls -la $TMPDIR shows claude-settings-2f3c004aa6835f81.json (mode 0444), bash-edit-diff/ and the session's tasks/ directory holding background-command output files. The host Bash sandbox explicitly lists those paths under denyWithinAllow, so the host treats them as protected; but the Edit tool runs in Claude's own process, outside that Bash sandbox, and both the Edit(//<tmpdir>/**) rule and check_file_edit now approve it.

Impact: a read-only stage could overwrite a background verification command's output file (making a failed suite read as passed to itself) or, if the 0444 mode did not stop it, the materialised settings file. I did not attempt either write: probing the harness's own control files is outside this stage's mandate, so this is from code inspection plus the observed directory layout (PLAUSIBLE, not reproduced).

Suggested fix: narrow both barriers to what the skills actually write, for example top-level pr-comment.md, report.md and *.log directly in $TMPDIR (Edit(//<tmpdir>/*.md), Edit(//<tmpdir>/*.log) and the matching test in check_file_edit), or exclude the executor's claude-* subtree. Not blocking because the fix does what #272 asks and the 0444 mode guards the settings file, but it widens the stated invariant ("edits only where it writes its comment body") more than the docs say.

Security triage

The repository declares no security_review.always_when rule, so the default list applies. The deterministic half did not match paths or labels (the PR has no labels); my own reading activated the audit because the change modifies security tooling (the stage permission barrier). The audit ran inline in this review:

  • No confirmed vulnerability introduced. Traversal, symlink, sibling-directory, .git, relative-path and missing-tmpdir cases are refused, as the new tests assert and the suite confirms.
  • Potential risk: REV-001.
  • Pre-existing risk (not introduced here, out of scope): a read-only stage could already write chosen content into $TMPDIR with an allowed git log --format=format:<text> --output=$TMPDIR/<file> (observed: git log -1 --format=format:probe-from-git-log --output=$TMPDIR/probe.txt was allowed and wrote 18 bytes). Combined with a prefix verification grant such as Bash(python3 -m unittest:*), which _check_words matches by prefix, python3 -m unittest discover -s $TMPDIR could import code the stage wrote, outside the closed table. This PR adds a second, simpler way to write such a file (Write). I did not run that combination. A follow-up issue is worth considering: refuse verification-command arguments that point into $TMPDIR, or pin non-exact verification grants for read-only roles.

Commands executed

Command Result
python3 -m unittest discover -s tests exit 1 — Ran 1601 tests in 430.694s, FAILED (failures=1, errors=62); environmental, see below
python3 -m unittest tests.test_stage_permissions (recovery: the PR's module alone) exit 0 — Ran 69 tests, OK (includes every new test)
python3 scripts/validate-package.py exit 0 — VALID: 16 skills (7 cycle, 9 supporting), shared sections in sync, cross-agent manifests aligned
git log -1 --format=format:probe-from-git-log --output=$TMPDIR/probe.txt allowed by the stage hook; wrote an 18-byte file (pre-existing risk evidence)
Write $TMPDIR/pr-comment.md (this comment body) succeeded under this dispatch's policy

Why the full suite failed here (environment, not code):

  • 62 errors, all in test_validate_package.ValidatePackageTests: copy_package() → shutil.copytree hits [Errno 13] Permission denied on the clone's .bashrc, .gitconfig, .mcp.json, .claude/settings.json, .gitmodules and similar entries. The reviewer's bubblewrap sandbox masks those paths against reads, and the copy fails before any assertion runs.
  • 1 failure, test_executors.ReadOnlyVerificationTests.test_scratch_never_uses_a_temp_parent_inside_the_checkout: with gettempdir() patched into the checkout, the fallback candidates /tmp and /var/tmp (scripts/executors.py:143-150) are not writable in this sandbox, so it raises no temporary directory outside the dispatch checkout.
  • Recovery tried: ran the PR's own test module on its own (passes). Nothing else within this stage's command table and sandbox can lift the read masks or make /tmp writable.

Unverified

  • The full configured suite in an unrestricted environment (see above). The Windows CI shards run it.
  • REV-001 was not reproduced by execution (on purpose; see the finding).
  • The Windows //c/... spelling is covered by a test that runs only on os.name == "nt"; here it rests on the Windows CI shards and the author's live probe. Context7 was not available in this dispatch to recheck Claude Code's path-rule documentation.
  • The configured python3 -m unittest (third verification command) is the same suite as python3 -m unittest discover -s tests, so it was not run a second time.

CI (policy: parallel, not waited for)

Re-read just before publishing, on head 1400f39:

Check State
Skills, manifests, and Bash installer pass
Windows test shard (windows-1) pass
Windows test shard (windows-2) pass
Windows test shard (windows-3) pass (pending at the first read)
Windows test shard (windows-4) pass
PowerShell installer pass

All 6 reported checks passed. The Windows shards run the full suite that this sandbox could not complete.

Residual risks

  • The only test-shard checks are Windows ones, and this sandbox's Linux run was environment-blocked. I did not check whether Skills, manifests, and Bash installer also runs the unit suite on Linux.
  • REV-001 (edit scope wider than the documented need).
  • The pre-existing $TMPDIR + prefix-verification execution path described above.
  • The author notes that a real review publishing on GitHub through the new rule is only proven after merge.

…e-tmpdir

# Conflicts:
#	tests/test_stage_permissions.py
…TMPDIR

REV-001 on PR #273: Edit(//<tmpdir>/**) also covered the files Claude keeps
in the same directory (materialised settings, tasks/ output,
bash-edit-diff/), so a read-only stage could overwrite a background
verification log. Both barriers now name only pr-comment.md, report.md,
reply.md and top-level <check>.log, and the hook refuses a helper name that
is not directly in $TMPDIR or resolves to another file there.

Refs #272
@datacas

datacas commented Oct 19, 2026

Copy link
Copy Markdown
Owner Author

Evidence for a manual merge decision: head fc548b4

REV-001: fixed by hand in fc548b4 (not yet confirmed by a cycle rereview)

Both barriers now grant only the named helper files directly in $TMPDIR: pr-comment.md, report.md, reply.md and <check>.log, matching [A-Za-z0-9][A-Za-z0-9_-]*\.log. There is no ** rule. The hook refuses a helper name that is not directly in $TMPDIR or that resolves to another file there.

  • New tests deny Claude's own state: claude-settings-*.json, tasks/*.output, tasks/*.log and bash-edit-diff/*. They also deny other names, hidden or suffixed logs, and symlinked helpers (report.md → tasks/b1.output, reply.md → tasks/pr-comment.md). The same tests fail on the previous /** implementation: 162 failures.
  • Live probe with Claude Code 2.1.303 (-p --permission-mode default and the four Edit(...) rules): writes to pr-comment.md and unittest.log succeeded; writes to tasks/b1.log, claude-settings.json and notes.md were blocked.

Host results (fc548b4)

  • python3 -m unittest discover -s tests: 1603 tests, OK.
  • python3 scripts/validate-package.py: VALID.
  • GitHub CI: 6/6 checks SUCCESS.

Review sandbox results: environment only, tracked in #274

  • main de2e35b in the review sandbox: 1599 tests, FAILED (failures=1, errors=62). The host passes the same commit.
  • The failures are identical with and without this PR: 62 test_validate_package errors (copytree hits the sandbox's unreadable placeholder dotfiles) and one test_executors failure (no writable temp parent).
  • Both rereview attempts on fc548b4 returned no verdict. The suite takes 5–7 minutes in the sandbox and ran in the background; the agent ended its turn before it finished (unreadable_result). This comes from the same environment limitation, not from this PR.

Pre-existing, out of scope

As the first review noted, Bash-side writes (git … --output, > redirects) may still target any path inside $TMPDIR. That predates this PR, and the Bash sandbox's denyWithinAllow protects Claude's files there when the sandbox is active.

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.

Read-only stages cannot write their own $TMPDIR, so reviews cannot publish their comment

1 participant