Repository navigation
fix(harness): refuse python calls that would import shadow modules from cwd - #309
raymondginger2018-sudo wants to merge 1 commit into
Conversation
…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.
|
Requesting a security review for this one, since it changes the Two things I could not do from a fork account, so flagging them explicitly
The change is additive and fails open: The security rationale, the deliberately narrow module list, and the negative |
|
Thanks a lot for digging into this, and for the review request. The 1. Two of the names only add refusals. 2. The screen covers one shape of command. All of these still run the planted module:
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 ( 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 If you'd like to revisit, we'd look at a version that:
Closing for now. Thanks again for the careful write-up. |
Summary
A Python interpreter puts its cwd on
sys.path, so a same-named file in thatdirectory shadows the standard library. An archive that ships
struct.pyexecutes arbitrary code as soon as the caller runs
import base64(base64imports 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.pyscreen_cwd_shadow(command, workspace)— when the command starts a Pythoninterpreter, inspects the workspace and every
cdtarget named on thecommand line (the unpack-and-run shape is
cd <archive-dir> && python3 ...).(
sitecustomize/usercustomize) or pulled in by very common stdlib modules(
struct) are listed, so ordinary source trees are unaffected.DEEPCODE_COMMAND_SCREEN=strict:this covers a structural gap in the sandbox, not an extra hardening layer.
DEEPCODE_ALLOW_CWD_SHADOW=1is the explicit waiver.screen_all()gains an optionalworkspaceargument; existing callers keeptheir current behaviour (screen is inert without it).
core/harness/tools/shell.py— passworkspace=self._workspaceat the onescreen_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, waiverspellings,
screen_allordering vs the destructive screen, and theno-workspace back-compat path.
tests/test_command_guard.py,test_command_guard_egress.py,test_harness_sandbox.py→ 236 passed, 15 skipped (baseline parity).ruff check+ruff format --checkat the pinnedv0.15.21→ clean.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_shadowreturnsNoneon:os.scandirwrapped intry/except OSError),So a filesystem quirk degrades to "nothing found", never to "block everything".
Why the scope is narrow on purpose. A blanket "any
.pybeside 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 -> headhas 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.