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
2 Commits
Author SHA1 Message Date
Sharang ParnerkarandClaude Opus 5 a4c37b45cb 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
#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
2026-09-21 00:04:26 +02:00
Sharang ParnerkarandClaude Opus 5 c7fc7af249 feat(platform): isolate tenants on disk, in the runner API and on webhooks (#255)
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
2026-09-21 00:00:26 +02:00