# sharma-open-source/capwarden architecture

> Architecture diagram of the GitHub repository sharma-open-source/capwarden, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sharma-open-source/capwarden
- Repository: https://github.com/sharma-open-source/capwarden

## Overview

#### 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: [`src/modes/index.ts`](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/index.ts) (Mode orchestration), [`src/interceptors/index.ts`](https://github.com/sharma-open-source/capwarden/blob/main/src/interceptors/index.ts) (API interceptors), [`src/modes/observe.ts`](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/observe.ts) (Observe mode), [`src/policy/load.ts`](https://github.com/sharma-open-source/capwarden/blob/main/src/policy/load.ts) (Policy loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_controller["CLI controller"]
  node_cli["capwarden CLI<br/>command controller<br/>[index.ts]"]
  node_modes["Mode orchestration<br/>mode dispatcher<br/>[index.ts]"]
end

subgraph group_runtime["Instrumented runtime"]
  node_preload["Preload hooks<br/>Node preload<br/>[hooks.mjs]"]
  node_package_api["Package API<br/>library entrypoint<br/>[index.ts]"]
  node_register["Registration API<br/>preload registration<br/>[register.ts]"]
  node_interceptors{{"API interceptors<br/>JS API wrappers<br/>[index.ts]"}}
  node_guard{{"Policy guard<br/>enforcement boundary<br/>[guard.ts]"}}
  node_attribution["Dependency attribution<br/>event attribution<br/>[index.ts]"]
  node_async_context["Async context<br/>attribution propagation<br/>[async-context.ts]"]
  node_node_workload["Application or tests<br/>child Node process"]
end

subgraph group_policy["Policy and outputs"]
  node_observe["Observe mode<br/>event accumulator<br/>[observe.ts]"]
  node_enforce["Enforce mode<br/>policy evaluator<br/>[enforce.ts]"]
  node_update["Update mode<br/>policy updater<br/>[update.ts]"]
  node_policy_load["Policy loader<br/>schema loader<br/>[load.ts]"]
  node_policy_diff["Policy diff<br/>policy comparison<br/>[diff.ts]"]
  node_config["Configuration<br/>runtime settings<br/>[config.ts]"]
  node_reports["Reports<br/>result rendering<br/>[index.ts]"]
  node_policy_files["Project policy files<br/>local artifacts"]
end

subgraph group_install["Install-script governance"]
  node_install_inventory["Script inventory<br/>lifecycle inspection<br/>[inventory.ts]"]
  node_script_runner["Permitted script runner<br/>lifecycle executor<br/>[runner.ts]"]
end

node_node_apis{{"Node and global APIs<br/>runtime boundary"}}

node_cli -->|"selects command"| node_modes
node_cli -->|"governs install scripts"| node_install_inventory
node_modes -->|"launches with preload"| node_node_workload
node_node_workload -->|"loads"| node_preload
node_package_api -->|"exposes"| node_register
node_register -->|"registers hooks"| node_preload
node_preload -->|"installs"| node_interceptors
node_node_workload -->|"uses"| node_node_apis
node_interceptors -->|"wraps"| node_node_apis
node_interceptors -->|"emits access events"| node_attribution
node_async_context -->|"preserves async ownership"| node_attribution
node_attribution -->|"attributed capability event"| node_guard
node_modes -->|"observe"| node_observe
node_modes -->|"enforce"| node_enforce
node_modes -->|"update"| node_update
node_observe -->|"observation results"| node_reports
node_observe -->|"proposed baseline"| node_policy_files
node_enforce -->|"loads committed policy"| node_policy_load
node_policy_load -->|"reads"| node_policy_files
node_policy_load -->|"capability grants"| node_guard
node_config -->|"denials and failure mode"| node_guard
node_update -->|"computes changes"| node_policy_diff
node_policy_diff -->|"writes on request"| node_policy_files
node_install_inventory -->|"permitted scripts"| node_script_runner

click node_cli "https://github.com/sharma-open-source/capwarden/blob/main/src/cli/index.ts"
click node_modes "https://github.com/sharma-open-source/capwarden/blob/main/src/modes/index.ts"
click node_preload "https://github.com/sharma-open-source/capwarden/blob/main/src/hooks.mjs"
click node_package_api "https://github.com/sharma-open-source/capwarden/blob/main/src/index.ts"
click node_register "https://github.com/sharma-open-source/capwarden/blob/main/src/register.ts"
click node_interceptors "https://github.com/sharma-open-source/capwarden/blob/main/src/interceptors/index.ts"
click node_guard "https://github.com/sharma-open-source/capwarden/blob/main/src/interceptors/guard.ts"
click node_attribution "https://github.com/sharma-open-source/capwarden/blob/main/src/attribution/index.ts"
click node_async_context "https://github.com/sharma-open-source/capwarden/blob/main/src/attribution/async-context.ts"
click node_observe "https://github.com/sharma-open-source/capwarden/blob/main/src/modes/observe.ts"
click node_enforce "https://github.com/sharma-open-source/capwarden/blob/main/src/modes/enforce.ts"
click node_update "https://github.com/sharma-open-source/capwarden/blob/main/src/modes/update.ts"
click node_policy_load "https://github.com/sharma-open-source/capwarden/blob/main/src/policy/load.ts"
click node_policy_diff "https://github.com/sharma-open-source/capwarden/blob/main/src/policy/diff.ts"
click node_config "https://github.com/sharma-open-source/capwarden/blob/main/src/config.ts"
click node_reports "https://github.com/sharma-open-source/capwarden/blob/main/src/report/index.ts"
click node_install_inventory "https://github.com/sharma-open-source/capwarden/blob/main/src/install-scripts/inventory.ts"
click node_script_runner "https://github.com/sharma-open-source/capwarden/blob/main/src/install-scripts/runner.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_modes toneBlue
class node_preload,node_package_api,node_register,node_interceptors,node_guard,node_attribution,node_async_context,node_node_workload toneAmber
class node_observe,node_enforce,node_update,node_policy_load,node_policy_diff,node_config,node_reports,node_policy_files toneMint
class node_install_inventory,node_script_runner toneRose
class node_node_apis toneNeutral
```

## Components

### CLI controller

Commands configure and launch instrumented workloads.

- [capwarden CLI](https://github.com/sharma-open-source/capwarden/blob/main/src/cli/index.ts): command controller · `src/cli/index.ts`
- [Mode orchestration](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/index.ts): mode dispatcher · `src/modes/index.ts`

### Instrumented runtime

Preload hooks observe and enforce Node-level access.

- [Preload hooks](https://github.com/sharma-open-source/capwarden/blob/main/src/hooks.mjs): Node preload · Registers hooks in the child Node workload. · `src/hooks.mjs`
- [Package API](https://github.com/sharma-open-source/capwarden/blob/main/src/index.ts): library entrypoint · `src/index.ts`
- [Registration API](https://github.com/sharma-open-source/capwarden/blob/main/src/register.ts): preload registration · `src/register.ts`
- [API interceptors](https://github.com/sharma-open-source/capwarden/blob/main/src/interceptors/index.ts): JS API wrappers · Wrap environment, filesystem, network, and process APIs. · `src/interceptors/index.ts`
- [Policy guard](https://github.com/sharma-open-source/capwarden/blob/main/src/interceptors/guard.ts): enforcement boundary · Blocks or logs intercepted accesses under enforcement. · `src/interceptors/guard.ts`
- [Dependency attribution](https://github.com/sharma-open-source/capwarden/blob/main/src/attribution/index.ts): event attribution · Assigns accesses to installed dependencies or first-party app code. · `src/attribution/index.ts`
- [Async context](https://github.com/sharma-open-source/capwarden/blob/main/src/attribution/async-context.ts): attribution propagation · `src/attribution/async-context.ts`
- **Application or tests**: child Node process · Caller command executed with CapWarden preloaded.

### Policy and outputs

Reviewed grants, generated proposals, and reports.

- [Observe mode](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/observe.ts): event accumulator · Produces a proposed capability baseline without blocking. · `src/modes/observe.ts`
- [Enforce mode](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/enforce.ts): policy evaluator · `src/modes/enforce.ts`
- [Update mode](https://github.com/sharma-open-source/capwarden/blob/main/src/modes/update.ts): policy updater · Re-observes and computes policy changes before writing. · `src/modes/update.ts`
- [Policy loader](https://github.com/sharma-open-source/capwarden/blob/main/src/policy/load.ts): schema loader · Auto-detects v1 grants and v2 strict package-detail policy. · `src/policy/load.ts`
- [Policy diff](https://github.com/sharma-open-source/capwarden/blob/main/src/policy/diff.ts): policy comparison · `src/policy/diff.ts`
- [Configuration](https://github.com/sharma-open-source/capwarden/blob/main/src/config.ts): runtime settings · Supplies ignores, explicit denials, and failure behavior. · `src/config.ts`
- [Reports](https://github.com/sharma-open-source/capwarden/blob/main/src/report/index.ts): result rendering · Terminal, JSON, and coverage output expose metadata rather than secrets. · `src/report/index.ts`
- **Project policy files**: local artifacts · Committed reviewed policy and generated proposals.

### Install-script governance

Separate control path for dependency lifecycle scripts.

- [Script inventory](https://github.com/sharma-open-source/capwarden/blob/main/src/install-scripts/inventory.ts): lifecycle inspection · Finds dependency install lifecycle scripts. · `src/install-scripts/inventory.ts`
- [Permitted script runner](https://github.com/sharma-open-source/capwarden/blob/main/src/install-scripts/runner.ts): lifecycle executor · `src/install-scripts/runner.ts`

### Other components

- **Node and global APIs**: runtime boundary · Native addons and activity below JS APIs are outside enforcement.

## Connections

- 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
