How sharma-open-source/capwarden works
Purpose and execution surfaces
CapWarden is a Node.js/npm per-dependency capability change detector, not a sandbox. It observes dependency access to environment variables, filesystem, network, and child processes; stores reviewed policy; then blocks or logs newly acquired access during CI. Primary surfaces are the package API/preload registration in src/index.ts, src/register.ts, and src/register.mjs, plus the capwarden CLI in src/cli/index.ts.
CLI orchestration and process boundary
The CLI parses commands such as observe, enforce, update, reporting, policy migration, and lifecycle-script governance. For commands that execute an application/test command, it launches that command with CapWarden’s preload/hooks enabled, creating a child-process boundary between the controller and instrumented Node workload. Relevant paths: src/cli/index.ts, src/modes/index.ts, src/hooks.mjs.
Runtime instrumentation layer
Interceptors patch or wrap Node/global APIs to emit capability events. Coverage includes proxied process.env, filesystem APIs, network stacks/fetch/DNS, and child_process; a guard centralizes policy checks and failure behavior. This is the critical JS-level enforcement boundary and intentionally cannot control native addons or activity below Node’s JS APIs. Paths: src/interceptors/index.ts, src/interceptors/guard.ts, src/interceptors/env.ts, src/interceptors/fs.ts, src/interceptors/net.ts, src/interceptors/proc.ts.
Attribution pipeline
Captured access is assigned to the responsible installed dependency rather than merely the current process. Stack inspection and async-context propagation preserve attribution across asynchronous work; workspace detection classifies monorepo-local packages as first-party app. Paths: src/attribution/index.ts, src/attribution/stack.ts, src/attribution/async-context.ts, src/attribution/workspaces.ts.
Modes: observation, enforcement, and update
Observe accumulates non-blocking events and produces a proposed baseline. Enforce loads the committed policy and routes each intercepted event through the guard; violations block or log according to configuration. Update re-observes, computes a policy diff, and writes only when requested. Hook flushing ensures captured events are finalized at process completion. Paths: src/modes/observe.ts, src/modes/enforce.ts, src/modes/update.ts, src/modes/flush-hooks.ts.
Policy model and configuration
Policies represent package capability grants in v1 kind-level form or v2 strict detail-level form. v2 identifies packages by name/version and may preserve resolution provenance; strict policy can pin details such as host, path, or environment key. Policy loading auto-detects schemas, while configuration supplies ignored packages, explicit denials, and fail-open/fail-closed internal-error behavior. Paths: src/policy/load.ts, src/policy/generate.ts, src/policy/generate-v2.ts, src/policy/diff.ts, src/config.ts.
Reporting and install-script governance
Observation results are rendered for terminal, JSON consumption, and coverage analysis; metadata records keys/hosts/paths rather than secret values or file contents. A separate install-script subsystem inventories dependency lifecycle scripts and can govern/run permitted scripts, addressing install-time execution before normal application invocation. Paths: src/report/index.ts, src/report/terminal.ts, src/report/coverage.ts, src/install-scripts/inventory.ts, src/install-scripts/runner.ts.
Runtime and infrastructure
The implementation is TypeScript/ESM targeting Node.js, distributed through npm and exercised through GitHub Actions CI (.github/workflows/ci.yml). It interacts primarily with Node built-ins, npm package metadata/workspace layouts, project-local policy/config/report files, and optionally the caller’s CI runner; it does not require a server, database, or external SaaS.
Main entry points: modes/index.ts Mode orchestration · interceptors/index.ts API interceptors · observe.ts Observe mode · load.ts Policy loader
Components 21
CLI controller
Commands configure and launch instrumented workloads.
- capwarden CLI
src/cli/index.ts - Mode orchestration
src/modes/index.ts
Instrumented runtime
Preload hooks observe and enforce Node-level access.
- Preload hooks · Registers hooks in the child Node workload.
src/hooks.mjs - Package API
src/index.ts - Registration API
src/register.ts - API interceptors · Wrap environment, filesystem, network, and process APIs.
src/interceptors/index.ts - Policy guard · Blocks or logs intercepted accesses under enforcement.
src/interceptors/guard.ts - Dependency attribution · Assigns accesses to installed dependencies or first-party app code.
src/attribution/index.ts - Async context
src/attribution/async-context.ts - Application or tests · Caller command executed with CapWarden preloaded.
Policy and outputs
Reviewed grants, generated proposals, and reports.
- Observe mode · Produces a proposed capability baseline without blocking.
src/modes/observe.ts - Enforce mode
src/modes/enforce.ts - Update mode · Re-observes and computes policy changes before writing.
src/modes/update.ts - Policy loader · Auto-detects v1 grants and v2 strict package-detail policy.
src/policy/load.ts - Policy diff
src/policy/diff.ts - Configuration · Supplies ignores, explicit denials, and failure behavior.
src/config.ts - Reports · Terminal, JSON, and coverage output expose metadata rather than secrets.
src/report/index.ts - Project policy files · Committed reviewed policy and generated proposals.
Install-script governance
Separate control path for dependency lifecycle scripts.
- Script inventory · Finds dependency install lifecycle scripts.
src/install-scripts/inventory.ts - Permitted script runner
src/install-scripts/runner.ts
Other components
- Node and global APIs · Native addons and activity below JS APIs are outside enforcement.
Connections 24
- capwarden CLI → Mode orchestration: selects command
- capwarden CLI → Script inventory: governs install scripts
- Mode orchestration → Application or tests: launches with preload
- Application or tests → Preload hooks: loads
- Package API → Registration API: exposes
- Registration API → Preload hooks: registers hooks
- Preload hooks → API interceptors: installs
- Application or tests → Node and global APIs: uses
- API interceptors → Node and global APIs: wraps
- API interceptors → Dependency attribution: emits access events
- Async context → Dependency attribution: preserves async ownership
- Dependency attribution → Policy guard: attributed capability event
- Mode orchestration → Observe mode: observe
- Mode orchestration → Enforce mode: enforce
- Mode orchestration → Update mode: update
- Observe mode → Reports: observation results
- Observe mode → Project policy files: proposed baseline
- Enforce mode → Policy loader: loads committed policy
- Policy loader → Project policy files: reads
- Policy loader → Policy guard: capability grants
- Configuration → Policy guard: denials and failure mode
- Update mode → Policy diff: computes changes
- Policy diff → Project policy files: writes on request
- Script inventory → Permitted script runner: permitted scripts