# Security

> How Kavach Enterprise keeps sensitive data out of fixtures, handles secrets, authenticates the gateway, and leaves a tamper-evident record of every action.

## Sensitive data in fixtures

A fixture holds real production inputs. Before one leaves the cluster, the shadow healer **tokenizes** sensitive values with a keyed HMAC-SHA256, then **replays the scrubbed fixture and checks it still fails the same way**.

What is tokenized:

- JSON string values whose key is on a denylist (names, emails, card numbers, tokens…), or whose value has the exact shape of one;
- inline sensitive spans inside free text: emails, US Social Security numbers, phone numbers, card numbers that pass the Luhn check, and JWTs.

Allowlisted enum keys (a `status`, a `type`) are kept as they are, so the replay still takes the same branches. Tokens are deterministic for a given key, so the same customer ID tokenizes to the same value across a fixture and relationships survive.

When scanning values would stop the fixture reproducing the crash, a **key-denylist-only** mode tokenizes by key alone, which keeps the replay intact.

Keep the HMAC key secret and stable: rotating it changes every token.

## Secrets

The daemon reads every secret from an environment variable or a file, and **refuses secrets on the command line** (exit code `2`), where they would show up in process listings and logs.

| Secret | Environment | File |
| --- | --- | --- |
| Tokenization key | `KAVACH_HMAC_KEY_HEX` | `KAVACH_HMAC_KEY_FILE`, `--hmac-key-file` |
| Audit signing key | `KAVACH_AUDIT_HMAC_KEY_HEX` | `KAVACH_AUDIT_HMAC_KEY_FILE` |
| Gateway token | `KAVACH_ARMOR_AUTH_TOKEN` | `KAVACH_ARMOR_AUTH_TOKEN_FILE`, `--auth-token-file` |
| GitHub token (pull requests) | `GITHUB_TOKEN` | `KAVACH_GITHUB_TOKEN_FILE`, `--github-token-file` |
| Kubernetes token | `KAVACH_KUBE_TOKEN` | `KAVACH_KUBE_TOKEN_FILE`, `--kube-token-file` |

Mount them from Kubernetes secrets as files.

## Gateway authentication

The live gateway listens on a Unix socket and/or HTTP.

- **Unix socket**: created with mode `0600`, and only peers with an allowed user ID may connect (`--uds-allowed-uid`, default the daemon's own).
- **HTTP**: the control endpoints (`/v1/step`, `/v1/quarantine`) **fail closed**: they refuse every request unless a bearer token, TLS client certificates, or both, are configured. Tokens are compared in constant time.
- **TLS and mTLS**: `--tls-cert-file`, `--tls-key-file`, and for client certificates `--tls-client-ca-file` with `--tls-require-client-cert`.

`/healthz`, `/readyz` and `/metrics` stay unauthenticated, for probes and scrapers. Bind the HTTP listener to localhost or a pod-local address unless you need it elsewhere.

## Audit trail

Every capture, proof, pull request, rollout and drain is appended to a hash-chained ledger in your bucket. Each entry is a write-once object linked to the one before it by SHA-256; an optional HMAC signs each entry. The head is updated with a conditional write, so two writers cannot fork the chain.

Verify it at any time, from anywhere with read access:

```bash
KAVACH_AUDIT_HMAC_KEY_FILE=/run/secrets/audit_hmac_hex \
kavach-daemon --verify-audit --service payments --bucket gs://acme-kavach/audit --expected-min-seq 1
```

Exit `0` means the chain is intact; `1` means an entry was changed, removed, reordered or rolled back. `--expected-min-seq` catches a truncated chain.

## Container and cluster hardening

- Non-root (`65532:65532`), read-only root filesystem, all capabilities dropped, no privilege escalation.
- The operator rejects `ProtectedService` specs that weaken any of these, and can require a default-deny network policy with explicit egress (`egressCidrs`, `egressPort`).
- The operator itself runs with least-privilege RBAC.

## How the daemon is tested

- Its decision logic is a pure state machine with no I/O, clock or threads, verified on every build by checking the compiled library's symbols.
- Deterministic simulation runs node kills, network partitions, throttling and ambiguous storage writes across many seeds, asserting one writer per epoch, no leaked effects from aborted steps, and strict per-entity ordering.
- Address, undefined-behaviour and thread sanitizers run in CI on every pull request.
- All 17 parsers (fixtures, archives, cloud storage responses, Kubernetes resources, the wire protocol, audit records and more) have fuzz targets with seed corpora. Each runs a short fuzzing pass on every pull request and a longer one nightly.
- Storage clients are tested on every pull request against MinIO, Azurite and fake-gcs-server, which emulate S3, Azure Blob Storage and GCS.

For what leaves the cluster and where it is kept, see [Data handling](https://www.thekavachlabs.com/docs/enterprise/data-handling).

## Reporting a vulnerability

Write to hello@thekavachlabs.com with "security" in the subject. Include the image tag or digest you run, and steps to reproduce. Please do not open a public issue for a vulnerability.

Source: https://www.thekavachlabs.com/docs/enterprise/security
