Skip to content

feat(security): Phase 4 — Staged Workflows / Lethal Trifecta privilege separation (#85)#403

Draft
aviadshiber wants to merge 2 commits into
mainfrom
claude/sweet-thompson-h4gakj
Draft

feat(security): Phase 4 — Staged Workflows / Lethal Trifecta privilege separation (#85)#403
aviadshiber wants to merge 2 commits into
mainfrom
claude/sweet-thompson-h4gakj

Conversation

@aviadshiber

Copy link
Copy Markdown
Owner

Summary

Closes #85.

Implements Phase 4: Staged Workflows — Privilege Separation so no single container run ever has all three "Lethal Trifecta" risk factors at once:

Risk Factor research implementation publish
Sensitive Data (credentials, source code) limited
Untrusted Content (web / external APIs)
External Communication (network)

Each stage runs in its own container with a tailored isolation posture:

Stage network_mode security_profile credentials
research filtered minimal none
implementation none strict filtered list
publish filtered standard GITHUB_TOKEN only

New files

  • scripts/lib/staged-workflows.sh — core library: YAML config loading, stage application (mutates NETWORK_MODE, KAPSIS_SECURITY_PROFILE, ENV_KEYCHAIN), approval gate evaluation (policies: small_changes / docs_only / tests_only / always), handoff file I/O, BiDi + zero-width sanitization at the untrusted→trusted boundary
  • scripts/workflow-orchestrator.sh — sequential multi-stage runner with per-stage env -i credential isolation, gate checking, and workflow state in ~/.kapsis/workflows/
  • scripts/kapsis-approve.sh — CLI tool to approve/reject pending gates from another terminal, Slack bot, or CI script; filesystem IPC via ~/.kapsis/workflows/*/approvals/*.pending.json
  • tests/test-staged-workflows.sh — 25 tests: config loading, stage application, all three approval policies, handoff I/O, JSON injection escaping, BiDi sanitization, status stage field, --stage dry-run integration

Modified files

  • scripts/launch-agent.sh — new --stage <name> flag; calls stage_apply_config() after parse_config() to override isolation per stage
  • scripts/lib/status.sh — adds stage field to status JSON (reads KAPSIS_STATUS_STAGE set by launch-agent.sh)
  • scripts/lib/constants.shKAPSIS_HANDOFFS_DIR, KAPSIS_WORKFLOWS_DIR, per-stage network/security defaults, approval thresholds
  • agent-sandbox.yaml.template — documented workflow.stages and workflow.approval config sections
  • tests/run-all-tests.sh — registered in security category + QUICK_TESTS
  • dashboard/shared/src/index.tsstage: string | null on AgentStatus (Dashboard Sync Rule)
  • dashboard/ui/src/views/AgentDetail.tsx — "Workflow Stage" card when stage is set
  • dashboard/ui/src/views/AgentList.tsx — Stage column in agents table

Key security design decisions (brainstormed with ensemble)

  1. Sanitizer runs host-side onlystage_sanitize_handoff() is called from workflow-orchestrator.sh on the host between stage exits, never inside a container. A container cannot forge its own sanitization.
  2. env -i isolationworkflow-orchestrator.sh uses env -i when calling launch-agent.sh so credentials in the orchestrator's environment cannot leak into the Research stage's container, bypassing the YAML credential filter.
  3. Approval timeout → deny_sw_interactive_approval() defaults to return 1 (deny) on timeout and in non-TTY contexts. An unattended terminal never silently advances.
  4. Network isolation as backstopnetwork_mode: none on Implementation means even a successful prompt-injection attack during Research cannot exfiltrate data, since the compromised Implementation container has no outbound path.
  5. Path traversal protection — Handoff file paths are constructed from sanitized stage_name + agent_id (both validated to [a-zA-Z0-9_-]+), never from user-supplied paths.

Test plan

  • bash tests/test-staged-workflows.sh — 25/25 PASS
  • bash -n syntax check on all new scripts
  • dashboard/shared/src/index.ts updated in lockstep with status.sh per Dashboard Sync Rule

https://claude.ai/code/session_01SKBncn264jjnABaUUqTMSW


Generated by Claude Code

…separation (#85)

Implement multi-stage agent workflows so no single container run has all three
risk factors simultaneously (Sensitive Data + Untrusted Content + External
Communication), eliminating the "Lethal Trifecta" attack surface.

New files:
- scripts/lib/staged-workflows.sh: core library — config loading, stage
  application (network/security/credential overrides), approval gate evaluation
  (small_changes / docs_only / tests_only / always policies), handoff file I/O,
  BiDi/zero-width sanitization at the untrusted→trusted boundary
- scripts/workflow-orchestrator.sh: multi-stage runner — sequential stage
  execution, per-stage env isolation via `env -i`, gate checking, workflow
  state tracking in ~/.kapsis/workflows/
- scripts/kapsis-approve.sh: CLI approval tool — approve/reject pending gates
  from another terminal, Slack bot, or CI script
- tests/test-staged-workflows.sh: 25 tests covering config loading, stage
  application, approval policies, handoff I/O, sanitization, status field,
  and launch-agent --stage dry-run integration

Modified files:
- scripts/launch-agent.sh: add --stage <name> flag; after parse_config(),
  source staged-workflows.sh and call stage_apply_config() to override
  NETWORK_MODE, KAPSIS_SECURITY_PROFILE, and ENV_KEYCHAIN per stage
- scripts/lib/status.sh: add stage field to status JSON (reads
  KAPSIS_STATUS_STAGE env var set by launch-agent.sh)
- scripts/lib/constants.sh: add KAPSIS_HANDOFFS_DIR, KAPSIS_WORKFLOWS_DIR,
  per-stage defaults, and approval threshold constants
- agent-sandbox.yaml.template: document workflow.stages and approval config
- tests/run-all-tests.sh: register test-staged-workflows.sh in security
  category and QUICK_TESTS list
- dashboard/shared/src/index.ts: add stage: string | null to AgentStatus
  (Dashboard Sync Rule — keeps TypeScript mirror in lockstep with status.sh)
- dashboard/ui/src/views/AgentDetail.tsx: render "Workflow Stage" card when
  stage field is set
- dashboard/ui/src/views/AgentList.tsx: add Stage column to agents table

Security properties of the default 3-stage layout:
  research       network=filtered  security=minimal  credentials=none
  implementation network=none      security=strict   credentials=filtered
  publish        network=filtered  security=standard credentials=GITHUB_TOKEN only

Brainstormed with ensemble (3 parallel agents): architecture exploration,
security design (env -i isolation, sanitizer-must-run-on-host invariant,
path traversal validation, approval-timeout-defaults-to-deny), test patterns.

Closes #85

https://claude.ai/code/session_01SKBncn264jjnABaUUqTMSW
: "${KAPSIS_HANDOFF_DIR:=${HOME}/.kapsis/handoffs}"

# Valid stage names built into Kapsis (users can define custom ones in YAML)
readonly KAPSIS_BUILTIN_STAGES="research implementation publish"
readonly KAPSIS_BUILTIN_STAGES="research implementation publish"

# Approval policy names supported natively
readonly KAPSIS_APPROVAL_POLICIES_LIST="small_changes docs_only tests_only always"
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
KAPSIS_ROOT="$(dirname "$SCRIPT_DIR")"
Comment thread tests/test-staged-workflows.sh Fixed
…us override

The /kapsis-status mount point exists in this environment, so _status_get_dir()
returns that path instead of $KAPSIS_STATUS_DIR. The test now uses status_get_file()
to resolve the actual path rather than constructing it from the overridden dir.

https://claude.ai/code/session_01SKBncn264jjnABaUUqTMSW
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.

🔀 Phase 4: Staged Workflows - Privilege Separation

3 participants