feat(security): stop executing code from scanned repositories (#257) #296

Merged
sharang merged 1 commits from feat/no-untrusted-exec into main 2026-09-21 10:28:00 +00:00
Owner

Closes #257. Based on main (after #292 and #293). Part of #253 · MVP-0.

Why

Nearly every stage only reads customer code — Semgrep, gitleaks, Syft, Ruff and the PLC parser parse files. Four stages instead run it, inside the agent container, as root, with the Mongo URI, the Keycloak and registry client secrets and the LLM key in the process environment, and a database user that reaches every tenant:

Stage What executes
cargo clippy compiles the crate, so build.rs and proc macros run
ESLint the repo's own node_modules/.bin/eslint, plus JavaScript config files
pip-compile builds sdists to read metadata, running setup.py
Firmware reproducible build the project's build system, through Nix with sandbox = false

Scanning hostile code is the product's whole job, so "the repo we were handed is hostile" is the expected case, not the edge case.

What changes

compliance_core::ExecPolicy gates all four. Off by default; PW_ALLOW_UNTRUSTED_CODE_EXEC=1 re-enables them for single-tenant or sandboxed deployments, and a hardened deployment ignores it — same shape as the on-premise opt-out in #256. Skipped stages log a line naming the stage and the flag rather than failing.

Coverage is barely affected. Dependency resolution continues: npm already ran --package-lock-only --ignore-scripts, and cargo generate-lockfile and cargo metadata resolve without compiling, so the SBOM is unchanged. What is lost is clippy/ESLint lint findings — quality warnings, not security findings, and both are duplicated by Semgrep's rules for the same languages — and the sealed firmware build, whose complete SBOM arrives instead as an imported Firmwerk bundle (#280), built on the customer's side where running their build is their own risk.

pipeline::tool_cmd is now how every external tool spawns:

  • an empty environment plus a named allowlist (PATH, HOME, TMPDIR, LANG, and a tool's own cache variables such as CARGO_HOME). Adding a variable is a deliberate edit to one file, so a future secret cannot be inherited by accident;
  • kill_on_drop(true): dropping a tokio Child does not kill the process, so a timed-out semgrep kept running with its CPU, memory and clone directory. The comment in lint/mod.rs asserting it was SIGKILLed on drop was wrong;
  • run_bounded for the lockfile and metadata commands, which had no timeout at all.

Verification

  • cargo fmt --all --check and cargo clippy -p compliance-core -p compliance-agent -p compliance-mcp -p werkbank-exec -p control-map -- -D warnings clean.
  • cargo test --lib → 330 + 71 + 9 pass (werkbank-exec unchanged at 28).
  • New tests: the policy defaults and its skip message; the tool-environment allowlist contains nothing secret-shaped; and a gating test that builds a repo with both a Cargo.toml and a stub node_modules/.bin/eslint that would fail the test if it ran — the scan returns empty and never executes it.
  • Docs: docs/guide/scanning.md gains "What the scanner runs on your code", and .env.example documents the flag.

Follow-up

The stages return as sandboxed Werkbank jobs — a separate container, no secrets, no network — which is the right home for running a customer's build. That belongs with the Werkbank work in R1 rather than here.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Closes #257. Based on `main` (after #292 and #293). Part of #253 · MVP-0. ## Why Nearly every stage only *reads* customer code — Semgrep, gitleaks, Syft, Ruff and the PLC parser parse files. Four stages instead **run** it, inside the agent container, as root, with the Mongo URI, the Keycloak and registry client secrets and the LLM key in the process environment, and a database user that reaches every tenant: | Stage | What executes | |---|---| | `cargo clippy` | compiles the crate, so `build.rs` and proc macros run | | ESLint | the repo's own `node_modules/.bin/eslint`, plus JavaScript config files | | `pip-compile` | builds sdists to read metadata, running `setup.py` | | Firmware reproducible build | the project's build system, through Nix with `sandbox = false` | Scanning hostile code is the product's whole job, so "the repo we were handed is hostile" is the expected case, not the edge case. ## What changes **`compliance_core::ExecPolicy`** gates all four. Off by default; `PW_ALLOW_UNTRUSTED_CODE_EXEC=1` re-enables them for single-tenant or sandboxed deployments, and a hardened deployment ignores it — same shape as the on-premise opt-out in #256. Skipped stages log a line naming the stage and the flag rather than failing. **Coverage is barely affected.** Dependency resolution continues: npm already ran `--package-lock-only --ignore-scripts`, and `cargo generate-lockfile` and `cargo metadata` resolve without compiling, so the SBOM is unchanged. What is lost is clippy/ESLint lint findings — quality warnings, not security findings, and both are duplicated by Semgrep's rules for the same languages — and the sealed firmware build, whose complete SBOM arrives instead as an imported Firmwerk bundle (#280), built on the customer's side where running their build is their own risk. **`pipeline::tool_cmd`** is now how every external tool spawns: - an empty environment plus a named allowlist (`PATH`, `HOME`, `TMPDIR`, `LANG`, and a tool's own cache variables such as `CARGO_HOME`). Adding a variable is a deliberate edit to one file, so a future secret cannot be inherited by accident; - `kill_on_drop(true)`: dropping a tokio `Child` does **not** kill the process, so a timed-out semgrep kept running with its CPU, memory and clone directory. The comment in `lint/mod.rs` asserting it was SIGKILLed on drop was wrong; - `run_bounded` for the lockfile and metadata commands, which had no timeout at all. ## Verification - `cargo fmt --all --check` and `cargo clippy -p compliance-core -p compliance-agent -p compliance-mcp -p werkbank-exec -p control-map -- -D warnings` clean. - `cargo test --lib` → **330 + 71 + 9 pass** (werkbank-exec unchanged at 28). - New tests: the policy defaults and its skip message; the tool-environment allowlist contains nothing secret-shaped; and a gating test that builds a repo with both a `Cargo.toml` and a stub `node_modules/.bin/eslint` that would fail the test if it ran — the scan returns empty and never executes it. - Docs: `docs/guide/scanning.md` gains "What the scanner runs on your code", and `.env.example` documents the flag. ## Follow-up The stages return as **sandboxed Werkbank jobs** — a separate container, no secrets, no network — which is the right home for running a customer's build. That belongs with the Werkbank work in R1 rather than here. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-09-21 07:03:29 +00:00
feat(security): stop executing code from scanned repositories (#257)
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 5m46s
bf89e57b58
Most of the pipeline reads customer code. Four stages ran it — inside the
agent container, as root, with the Mongo URI, the Keycloak and registry client
secrets and the LLM key in the environment, and a database user that reaches
every tenant:

- `cargo clippy` compiles the repo, so its `build.rs` and proc macros run;
- ESLint is executed from the repo's own `node_modules/.bin`, and its config
  files are JavaScript;
- `pip-compile` builds source distributions to read metadata, running `setup.py`;
- the firmware reproducible build shells out to Nix with `sandbox = false`.

Scanning hostile code is the job, so new `compliance_core::ExecPolicy` gates
all four. It is off by default and a hardened deployment ignores the
`PW_ALLOW_UNTRUSTED_CODE_EXEC` opt-out, which stays for single-tenant and
sandboxed deployments. Dependency resolution is unaffected: npm already runs
`--package-lock-only --ignore-scripts`, and `cargo generate-lockfile` and
`cargo metadata` resolve without compiling. A complete firmware SBOM comes from
an imported Firmwerk bundle (#280) instead of a build here.

Every external tool now spawns through `pipeline::tool_cmd`:

- an empty environment plus a named allowlist (PATH, HOME, TMPDIR, LANG, and a
  tool's own cache variables), so semgrep, syft, gitleaks, ruff, cargo and npm
  no longer inherit the agent's credentials;
- `kill_on_drop`, because a tokio child outlives its timeout otherwise — the
  comment claiming a timed-out scanner was SIGKILLed was wrong;
- a bounded run for the lockfile and metadata commands, which had no timeout.

Part of #253 · MVP-0 safety baseline.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang force-pushed feat/no-untrusted-exec from 5975ba45f9 to bf89e57b58 2026-09-21 07:03:29 +00:00 Compare
sharang merged commit 06d2f458e6 into main 2026-09-21 10:28:00 +00:00
sharang deleted branch feat/no-untrusted-exec 2026-09-21 10:28:00 +00:00
Sign in to join this conversation.