Skip to content

fix(harness): refuse python calls that would import shadow modules from cwd - #309

Closed
raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/cwd-shadow
Closed

raymondginger2018-sudo wants to merge 1 commit into
HKUDS:mainfrom
raymondginger2018-sudo:pr/cwd-shadow

Conversation

@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor

Summary

A Python interpreter puts its cwd on sys.path, so a same-named file in that
directory shadows the standard library. An archive that ships struct.py
executes arbitrary code as soon as the caller runs import base64 (base64
imports struct transitively) — the model's own safety decision ("I will not run
the supplied binary, I will write my own decoder") is what puts it in the
vulnerable directory.

Every step of that chain (download, unpack, run) stays inside the workspace,
so the sandbox write fence cannot see it: the fence governs where bytes may
land, not whether what just landed may run. That makes cwd module shadowing the
one class the sandbox structurally cannot cover.

Changes

  • core/harness/command_guard.py
    • screen_cwd_shadow(command, workspace) — when the command starts a Python
      interpreter, inspects the workspace and every cd target named on the
      command line
      (the unpack-and-run shape is cd <archive-dir> && python3 ...).
    • Scope is deliberately narrow: only names auto-imported at interpreter start
      (sitecustomize/usercustomize) or pulled in by very common stdlib modules
      (struct) are listed, so ordinary source trees are unaffected.
    • Runs unconditionally rather than behind DEEPCODE_COMMAND_SCREEN=strict:
      this covers a structural gap in the sandbox, not an extra hardening layer.
    • DEEPCODE_ALLOW_CWD_SHADOW=1 is the explicit waiver.
    • screen_all() gains an optional workspace argument; existing callers keep
      their current behaviour (screen is inert without it).
  • core/harness/tools/shell.py — pass workspace=self._workspace at the one
    screen_all() call site (real runtime entry point, not an orphan module).

Tests

  • tests/test_command_guard_cwd_shadow.py — 26 cases: payload detection,
    case-insensitivity, directory-not-file, missing-directory degradation,
    the cd-target shape, non-Python commands not being screened, waiver
    spellings, screen_all ordering vs the destructive screen, and the
    no-workspace back-compat path.
  • Existing regression: tests/test_command_guard.py, test_command_guard_egress.py,
    test_harness_sandbox.py → 236 passed, 15 skipped (baseline parity).
  • ruff check + ruff format --check at the pinned v0.15.21 → clean.
  • pre-commit (trim trailing whitespace, end-of-files, ruff, ruff format) → pass.

Security Considerations

Attack surface changed. This adds one refusal path in front of shell
execution. It cannot be used to grant a capability: the only new outcome is
"this command is refused with a reason string". A false positive costs one
blocked command plus an explicit waiver env var, never a bypass.

Why it fails safe. screen_cwd_shadow returns None on:

  • no workspace supplied (every existing caller today),
  • unreadable/missing directory (os.scandir wrapped in try/except OSError),
  • commands that do not start a Python interpreter.

So a filesystem quirk degrades to "nothing found", never to "block everything".

Why the scope is narrow on purpose. A blanket "any .py beside the command"
rule would fire on ordinary source trees and be switched off within a day, which
is worse than not having it. The listed names are either auto-imported at
interpreter start or imported transitively by base64/json/pickle.

Security validation performed. 26 new cases including the published
cd <dir> && python3 -c 'import base64' shape, plus the negative cases above.
No network is used; no real payload is executed.

Dependency note

No new dependencies, no lockfile changes (base -> head has zero pin deltas).

Reference

Pattern reported by Johann Rehberger (Embracethered), "Breaking Claude Code
Opus 5 Auto Mode", Aug 2026 — module shadowing via a shipped struct.py.
This change is the defensive counterpart on the harness side.

…om cwd

A Python interpreter puts its cwd on sys.path, so a same-named file in that
directory shadows the standard library. An archive that ships `struct.py`
executes arbitrary code as soon as the caller runs `import base64` (base64
imports struct transitively).

Every step of that chain -- download, unpack, run -- stays inside the
workspace, so the sandbox write fence cannot see it: the fence governs where
bytes may land, not whether what just landed may run. That makes cwd module
shadowing the one class the sandbox structurally cannot cover.

This adds `screen_cwd_shadow`, which inspects the workspace and every `cd`
target named on the command line (the unpack-and-run shape is
`cd <archive-dir> && python3 ...`) when the command starts a Python
interpreter. Only names auto-imported at interpreter start
(sitecustomize/usercustomize) or pulled in by very common stdlib modules
(struct) are listed, so ordinary source trees are unaffected.

The screen runs unconditionally rather than behind
DEEPCODE_COMMAND_SCREEN=strict: it covers a structural gap in the sandbox,
not an extra hardening layer. DEEPCODE_ALLOW_CWD_SHADOW=1 is the explicit
waiver for a project that genuinely vendors one of these names.

`screen_all` gains an optional `workspace` argument, so existing callers keep
their current behaviour.
@raymondginger2018-sudo

Copy link
Copy Markdown
Contributor Author

Requesting a security review for this one, since it changes the
command-screening path that sits in front of shell execution
(core/harness/command_guard.py + its single call site in tools/shell.py).

Two things I could not do from a fork account, so flagging them explicitly
rather than leaving them looking like omissions:

  • adding a security label (POST /issues/309/labels is 403 without write access)
  • requesting a reviewer via POST /pulls/309/requested_reviewers (404 from a fork)

The change is additive and fails open: screen_cwd_shadow returns None when no
workspace is supplied, when the directory is unreadable, or when the command does
not start a Python interpreter. It cannot grant any capability - the only new
outcome is a refusal with a reason string.

The security rationale, the deliberately narrow module list, and the negative
cases are in the PR description's Security Considerations section.

@Zongwei9888

Copy link
Copy Markdown
Contributor

Thanks a lot for digging into this, and for the review request. The struct.py → import base64 chain is real; I reproduced it locally. After a security review we've decided not to merge this one, for these reasons.

1. Two of the names only add refusals. sitecustomize.py and usercustomize.py in the working directory aren't imported at startup: python -c pass in a folder with both prints nothing, on 3.9 and on 3.13. Yet a repo with sitecustomize.py at its root has every Python call refused, including python --version.

2. The screen covers one shape of command. All of these still run the planted module:

  • python3 catalogue/main.py (it runs catalogue/struct.py, sandbox on and off)
  • bash -c "cd catalogue && …"
  • env -C catalogue …
  • (cd catalogue && …)
  • an archive that ships json.py instead

Running Python inside an unpacked archive runs its code in many ways (setup.py, conftest.py, its own scripts). A short name list can't close that class, and listing every stdlib name would catch ordinary repos (test.py, types.py).

3. It is on in every mode, and the model can't waive it. It runs in every access mode, Full access included, and ignores DEEPCODE_COMMAND_SCREEN=off. The only waiver, DEEPCODE_ALLOW_CWD_SHADOW, is undocumented and read from the service's own environment. So the refusal's advice to "re-run with the matching DEEPCODE_* waiver" can't be followed, and docs/guide/security.md promises that only the destructive-command screen is always on.

If you'd like to revisit, we'd look at a version that:

  • sits with the opt-in strict screens (DEEPCODE_COMMAND_SCREEN=strict);
  • also checks the directory of the script being run;
  • drops sitecustomize/usercustomize;
  • is documented in docs/guide/configuration.md.

Closing for now. Thanks again for the careful write-up.

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.

2 participants