feat(platform): isolate tenants on disk, in the runner API and on webhooks (#255) #295

Merged
sharang merged 2 commits from feat/tenant-isolation into main 2026-09-21 06:53:37 +00:00
Owner

Closes #255. Stacked on #292 (feat/fail-closed-config) — it uses app_env.is_hardened() from there, so that is the base; it retargets to main when #292 merges. Reviewed and re-verified locally before pushing.

Five cross-tenant defects

1 · Clone directories were shared. The checkout path was <base>/<target-name>, so two tenants with a target of the same name shared one directory — and the second tenant to scan it fetched the first tenant's origin. New pipeline::git::clone_dir(base, tenant_id, repo_name) is now the single convention, and every producer and consumer moved onto it: the orchestrator (both call sites), PR review, ingest, the graph build, the chat embedding build and the file-content reader. GitOps and PipelineOrchestrator carry the tenant id; the Database handle isolates Mongo, not the filesystem.

New tenant_path module builds the segments. Collapsing a hostile id to a safe component is lossy — acme/eu, acme:eu and acme_eu all collapse to acme_eu — which would re-create the shared-directory bug, so the tenant segment appends a digest of the raw id whenever collapsing changed anything. Well-formed ids (UUIDs, slugs) pass through unchanged and stay readable.

2 · One SSH deploy key for every tenant. The agent authenticated every clone with one key, which is a cross-tenant read primitive: tenant A grants that key access to a repo, tenant B names the same repo, and the agent clones it for B. Per the decision taken for the MVP, git access is HTTPS-token only: the key, its generator (ssh.rs), the public-key endpoint and the dashboard panel that displayed it are gone, and an ssh://, git+ssh:// or scp-style URL now fails with a message naming the HTTPS-plus-token alternative instead of silently borrowing someone else's credential. Per-tenant keys are R1. ArtifactAuth::ssh_key_path stays in the model so stored documents still deserialize, documented as ignored. docs/guide/repositories.md is rewritten with token creation per provider.

3 · serve_artifact had no tenant check. Blobs are addressed by hash and the hash is in the job record, so any valid runner key could fetch any tenant's program source. The store is now rooted per tenant (<base>/tenants/<tenant>/blobs/…), which makes it structural rather than a filter: another tenant's digest is simply absent. The read is bound to the tenant the key is bound to.

4 · The runner scope check was too loose. It accepted the werkbank:runner scope or any key whose product was pruefwerk, so a dashboard or reporting key could lease and complete jobs. The scope is now required.

5 · Unauthenticated webhooks created databases. A POST naming any tenant_id ran createIndex and so materialized <prefix>_<tenant_id> before anything verified the tenant existed. The pre-signature lookup now goes through a new for_existing_tenant_id() handle that creates nothing; unknown tenant or target still 404s, and the authenticated path still provisions as before.

Plus: the static WERKBANK_RUNNER_TOKEN is refused outright on a hardened deployment (it names no tenant, so a holder can act for all of them), before the token is compared.

Two contract notes

  • GET /api/v1/werkbank/artifacts/{hash} now needs ?tenant= when the caller uses the static token, which only a dev deployment can use. A runner authenticating with a registry API key — including werkbank-runner-stage — is unaffected, since the tenant comes from the key.
  • Existing runner blobs under the old shared <base>/blobs/ path are not migrated. They are re-stored per tenant on the next enqueue, and staging has no queued jobs (the runner is off under source_only), so there is nothing to migrate there.

Verification (re-run by me, not just reported)

  • cargo fmt --all --check clean; cargo clippy -p compliance-agent -p compliance-core -p compliance-mcp -p werkbank-exec -p control-map -- -D warnings clean.
  • cargo test --lib → 339 + 60 + 27 + 9 pass; cargo test -p compliance-dashboard --features server → 21 pass; both dashboard feature sets build.
  • Integration, against a live Mongo: tenant_isolation 7 pass, werkbank_api 5 pass — including a test where the attacker holds an equally valid runner credential and the victim's blob hash and still gets a 404, and one asserting that an unknown-tenant webhook lookup leaves no database behind.
  • New unit tests cover path injectivity and traversal, two tenants with the same target name getting two directories, SSH rejection, per-tenant blob invisibility and the runner scope regression.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Closes #255. **Stacked on #292** (`feat/fail-closed-config`) — it uses `app_env.is_hardened()` from there, so that is the base; it retargets to `main` when #292 merges. Reviewed and re-verified locally before pushing. ## Five cross-tenant defects **1 · Clone directories were shared.** The checkout path was `<base>/<target-name>`, so two tenants with a target of the same name shared one directory — and the second tenant to scan it fetched the *first* tenant's `origin`. New `pipeline::git::clone_dir(base, tenant_id, repo_name)` is now the single convention, and every producer and consumer moved onto it: the orchestrator (both call sites), PR review, ingest, the graph build, the chat embedding build and the file-content reader. `GitOps` and `PipelineOrchestrator` carry the tenant id; the `Database` handle isolates Mongo, not the filesystem. New `tenant_path` module builds the segments. Collapsing a hostile id to a safe component is lossy — `acme/eu`, `acme:eu` and `acme_eu` all collapse to `acme_eu` — which would re-create the shared-directory bug, so the tenant segment appends a digest of the raw id whenever collapsing changed anything. Well-formed ids (UUIDs, slugs) pass through unchanged and stay readable. **2 · One SSH deploy key for every tenant.** The agent authenticated every clone with one key, which is a cross-tenant read primitive: tenant A grants that key access to a repo, tenant B names the same repo, and the agent clones it for B. Per the decision taken for the MVP, git access is HTTPS-token only: the key, its generator (`ssh.rs`), the public-key endpoint and the dashboard panel that displayed it are gone, and an `ssh://`, `git+ssh://` or scp-style URL now fails with a message naming the HTTPS-plus-token alternative instead of silently borrowing someone else's credential. Per-tenant keys are R1. `ArtifactAuth::ssh_key_path` stays in the model so stored documents still deserialize, documented as ignored. `docs/guide/repositories.md` is rewritten with token creation per provider. **3 · `serve_artifact` had no tenant check.** Blobs are addressed by hash and the hash is in the job record, so any valid runner key could fetch any tenant's program source. The store is now rooted per tenant (`<base>/tenants/<tenant>/blobs/…`), which makes it structural rather than a filter: another tenant's digest is simply absent. The read is bound to the tenant the key is bound to. **4 · The runner scope check was too loose.** It accepted the `werkbank:runner` scope **or** any key whose product was `pruefwerk`, so a dashboard or reporting key could lease and complete jobs. The scope is now required. **5 · Unauthenticated webhooks created databases.** A POST naming any `tenant_id` ran `createIndex` and so materialized `<prefix>_<tenant_id>` before anything verified the tenant existed. The pre-signature lookup now goes through a new `for_existing_tenant_id()` handle that creates nothing; unknown tenant or target still 404s, and the authenticated path still provisions as before. **Plus:** the static `WERKBANK_RUNNER_TOKEN` is refused outright on a hardened deployment (it names no tenant, so a holder can act for all of them), before the token is compared. ## Two contract notes - `GET /api/v1/werkbank/artifacts/{hash}` now needs `?tenant=` **when the caller uses the static token**, which only a dev deployment can use. A runner authenticating with a registry API key — including `werkbank-runner-stage` — is unaffected, since the tenant comes from the key. - Existing runner blobs under the old shared `<base>/blobs/` path are not migrated. They are re-stored per tenant on the next enqueue, and staging has no queued jobs (the runner is off under `source_only`), so there is nothing to migrate there. ## Verification (re-run by me, not just reported) - `cargo fmt --all --check` clean; `cargo clippy -p compliance-agent -p compliance-core -p compliance-mcp -p werkbank-exec -p control-map -- -D warnings` clean. - `cargo test --lib` → **339 + 60 + 27 + 9 pass**; `cargo test -p compliance-dashboard --features server` → 21 pass; both dashboard feature sets build. - Integration, against a live Mongo: `tenant_isolation` 7 pass, `werkbank_api` 5 pass — including a test where the attacker holds an equally valid runner credential and the victim's blob hash and still gets a 404, and one asserting that an unknown-tenant webhook lookup leaves no database behind. - New unit tests cover path injectivity and traversal, two tenants with the same target name getting two directories, SSH rejection, per-tenant blob invisibility and the runner scope regression. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang changed target branch from feat/fail-closed-config to main 2026-09-20 22:04:28 +00:00
sharang added 2 commits 2026-09-20 22:04:28 +00:00
A security audit found five ways one tenant could reach another tenant's
data or act on its behalf. All five are closed here.

1. Clone directories were shared. Every producer and consumer resolved a
   checkout to `<git_clone_base>/<sanitized-name>`, so two tenants that
   onboarded a target with the same name got one directory: the second one
   to scan fetched the first one's `origin` instead of cloning its own, and
   then scanned it. Checkouts are now `<base>/<tenant>/<name>` through a
   single helper, `pipeline::git::clone_dir`, which every call site uses —
   the orchestrator, PR review, ingest, the graph build, the chat embedding
   build and the file-content reader. Both segments collapse to one safe
   path component, and the tenant segment additionally carries a digest
   whenever collapsing was lossy, so two distinct tenant ids can never map
   to one directory.

2. One SSH deploy key authenticated every tenant's clone. A tenant that
   added the agent's key to a repository made that repository readable by
   any other tenant that named its URL. Per-tenant key management is R1, so
   at MVP the key is gone entirely: `RepoCredentials` is HTTPS-token only,
   an `ssh://`/`git@` URL is rejected with a message pointing at the HTTPS
   URL and a token, and `SSH_KEY_PATH`, the generator, the
   `/api/v1/settings/ssh-public-key` endpoint, the dashboard panel that
   advertised the key and the onboarding docs are all removed.

3. `serve_artifact` had no tenant check. Runner blobs are addressed by
   content hash and the hash is in the job record, so any valid runner key
   could fetch any tenant's control-logic source. The blob store is now per
   tenant (`<base>/tenants/<tenant>/blobs/…`) and the read is bound to the
   tenant the key is bound to; another tenant's digest is simply not there.

4. The runner scope check accepted the `werkbank:runner` scope *or* any key
   whose product was `pruefwerk`, which made every product API key a runner
   key able to lease and complete other tenants' jobs. The scope is now
   required.

5. Webhooks created databases before authenticating. A POST with any
   `tenant_id` reached `for_tenant_id`, whose index-ensure materializes
   `<prefix>_<tenant_id>` — an unauthenticated write primitive. The target
   lookup now goes through `DatabasePool::for_existing_tenant_id`, which
   creates nothing; an unknown tenant or target is a 404.

Also: the static `WERKBANK_RUNNER_TOKEN` names no tenant, so a holder acts
for all of them. It is still accepted in dev, where it is the only option
before the platform is wired up, and refused when `app_env.is_hardened()`.

URL-scheme and SSRF validation (file://, local paths, server-side
`source_ref`) is deliberately untouched — that is #256.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
refactor(security): one answer for SSH git remotes
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 6m48s
a4c37b45cb
#256 let `ssh://` through the URL guard (host rules still applied) and #255
then rejected every SSH remote at the clone, because the agent holds no git
SSH key any more. Two rules, two messages, for one decision.

The guard now refuses the `ssh` scheme outright, so the refusal happens at the
same place as `file://` and `git://`, and `pipeline::git::reject_ssh_url` stays
as the check for the scp-style form (`git@host:org/repo`), which carries no
scheme at all.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang force-pushed feat/tenant-isolation from 20b356fa1c to a4c37b45cb 2026-09-20 22:04:30 +00:00 Compare
sharang merged commit cc3a9c7130 into main 2026-09-21 06:53:37 +00:00
sharang deleted branch feat/tenant-isolation 2026-09-21 06:53:37 +00:00
Sign in to join this conversation.