# Security and audit

[中文](security-and-audit.md) · [Back to README](../README.en.md)

## Fail-closed policy

- Automatic approval produces `allowed-once` only.
- `critical` is never automatically approved.
- `high` may be auto-approved only when a complete direct-user message establishes `high` authorization covering the exact target and important side effects.
- Truncated user messages cannot establish authorization. Assistant text, arguments, tool results, file/Web content, and plugin context are untrusted evidence—not reviewer instructions.
- Missing or ambiguous call identity, tool mismatch, sandbox reason/argument mismatch, oversized evidence/action, missing route, timeout, provider failure, invalid JSON, or reviewer tool calls block automatic authorization.
- Generic extension approval can be automatic or manual only when one exact logged action is uniquely reconstructable.

## Read proof and review targets

The conservative Shell classifier exempts proven forms of `pwd`, restricted `ls/stat/cat/head/tail/wc/grep`, and read-only Git commands. Every path must remain under the workspace; when available, the Filesystem Service supplies backend canonical containment to prevent symlink escapes.

Pipelines, redirection, command substitution, multiple commands, interpreters, network commands, hidden-input or side-effecting options, and workspace-external paths are never guessed safe. Network targets expose bounded protocol/host/port metadata without extra DNS or I/O. MCP names lack trusted provenance and are reviewed as opaque remote capabilities.

## Reviewer data boundary

Guardian receives planned arguments and context summaries within configured byte budgets. Use only a provider trusted to process this data. Full tool output and reasoning blocks are omitted.

Ordinary logs and Guardian-card payloads exclude:

- raw tool arguments;
- complete evidence/transcript;
- unprocessed Guardian rationale.

The live UI payload uses owned scalar summaries, enum-like statuses, and one Guardian decision reason capped at 240 characters. Before watcher publication or audit recovery, whitespace is compacted and common token/password/Authorization/API-key values plus URL query values are redacted; the model prompt also forbids repeating commands, arguments, credentials, or other sensitive evidence. The payload is not written to Session history. Host restart recovery reads an existing final audit record and projects the same minimal fields again.

## JSONL audit-before-allow

Default path:

```text
${DSH_HOME:-~/.dsh}/audit/auto-review.jsonl
```

Each Review appends one final `auto-review/final` record:

- `summary` stores argument size, SHA-256, and structured summaries;
- `full` stores the complete target and may contain commands, URLs, or credentials—use a trusted private path only;
- directories/files use `0700`/`0600`;
- one Host process serializes appends and `fsync`s them, rolling back partial writes;
- first append validates existing JSONL, repairs a complete tail missing LF, and isolates malformed tails;
- the current file rotates to `.1` before exceeding `auditMaxBytes`.

With auditing enabled, automated and manual grants return only after append + fsync. Failure converts a would-be allow to `unavailable`. Only explicit `auditEnabled: false` permits operation without persistence.

The writer is safe for one Host process only; processes must not share a path.

## Two event surfaces

- Observer-only Cordis: `auto-review/started`, `auto-review/completed`. Throwing/rejecting listeners cannot change approval.
- Guardian live UI: in-memory `start`, `status`, and `end` updates plus the latest snapshot reach the current Web Session through a Typert Remote watcher. Host restart may recover a redacted snapshot from an existing final audit line, but the UI writes no Session event or additional audit line.

Both payload families follow minimal field allowlists. The old durable `auto-review/start|status|end` Session path remains under `TODO(dsh-session-events)` but is disabled from production entry points.
