# Security and privacy

What TraceGuard captures, what it protects, and what it does **not**.

## What is captured

| Data | Captured | Notes |
| --- | --- | --- |
| HTTP method, route template (`/users/:id`), status, timing | always | |
| Raw request path | only with `capture_path: true` | Paths often contain identifiers. |
| Path parameters | only with `capture_path_params: true` | Redacted by key. |
| Request headers | allow-list only (`capture_request_headers`, default `accept`, `content-type`) | Sensitive names are redacted even if allow-listed. |
| Query string, request body, cookies | **never** | |
| Ecto query timing, table, row count, outcome, calling function | when `ecto_telemetry_prefixes` is set | |
| SQL text | only with `capture_sql: true` | Literals written into a query appear in it. |
| SQL parameters, result rows | **never** | |
| Exception module, message, stack frames (module/function/arity/file/line) | yes; message can be disabled (`capture_exception_message: false`) | Frames never include argument values. The message is scrubbed (below). |
| Throw values, exit reasons | **never inspected** | Described structurally (for example `throw of a map value`). |
| Task and GenServer arguments | **never** | Only lifecycle events and the request's leading atom (`operation`). |
| Client correlation ID | only with `incoming_header` | Validated (`[A-Za-z0-9._:-]`, 1–128 characters), stored separately from the trace ID. |

## Redaction

* **Key-based, recursive**, over maps, lists and `{key, value}` tuples, on
  the finished report, *before* it leaves the request process. No adapter
  ever receives unredacted data. Keys are normalized (`X-Api-Key`, `apiKey`
  and `:api_key` are equivalent) and matched by token (`csrf_token` matches
  `token`; `tokenizer` doesn't).
* **Built-in keys:** `password authorization token access_token
  refresh_token secret api_key cookie private_key`. `redact_keys` adds more;
  it can't remove the built-ins.
* **Exception messages** are scrubbed for labelled secrets (`key=value`,
  `key: value`, `"key": "value"`, `key => value` with a sensitive key, and
  `Bearer …` / `Basic …`). An **unlabelled** secret in a message (`"card
  4242… declined"`) is not recognized. Set
  `capture_exception_message: false` if that matters to you.

## What TraceGuard does NOT protect

* **Your application's logs.** TraceGuard's redaction applies only to
  TraceGuard's reports. Phoenix, Plug, Bandit, Cowboy, Ecto, OTP (for example
  a GenServer's crash report) and your own `Logger` calls log whatever they
  log, including **raw exception messages containing secrets**. This was
  verified: in the external integration test, both Bandit's and Cowboy's
  crash logs contained the raw API key that the report had redacted.
  TraceGuard doesn't change, filter or sanitize any logging.
* Data you put into a report's allow-listed fields yourself, for example a
  secret in a route template or in an allow-listed header under a
  non-sensitive name.
* Copies outside TraceGuard: backups, filesystem snapshots, crash dumps,
  `:observer` or remote shells (which can read any process state).

## Identifiers and lookup

* **Trace IDs** are 128 random bits from `:crypto.strong_rand_bytes/1`,
  hex-encoded, and carry no request data. Lookups accept only well-formed
  IDs (`^[0-9a-f]{32}$`); anything else is rejected before the store is
  touched.
* **The `x-traceguard-id` response header** exposes the trace ID to the
  client. This is safe only because TraceGuard has **no HTTP lookup**: every
  lookup (`get_report/1`, `list_reports/1`, `report_status/1`,
  `await_report/2`) is a node-local Elixir function. If you build an
  endpoint that serves reports, as the demo's dev-only `/dev/traces` does,
  restrict it to development or authenticated operators; never expose it
  publicly. Disable the header with `response_header: nil`.
* **Correlation IDs** are client-controlled labels. They help find reports
  but grant nothing; filters match exactly.

## Retained state and storage

* **Bounds:**
  * in memory, at most `max_reports` retained and `max_pending_reports`
    queued;
  * at most `max_drop_records` drop records, each expiring after
    `drop_record_ttl`;
  * per trace, `max_events`.
* **The in-memory store** is lost on restart; nothing is written to disk.
* **The SQLite adapter** (development and testing):
  * new database files are created `0600` (owner only; `:file_mode`), and
    SQLite's `-wal`/`-shm` files inherit that mode;
  * existing files keep their permissions;
  * deleted, cleared and cleaned-up reports are zeroed (`secure_delete`), and
    the WAL is truncated afterwards;
  * there is no encryption at rest, so treat the file as sensitive
    diagnostic data, and keep it out of version control and backups you
    share.
* **The Ecto/PostgreSQL adapter** (Phase 6):
  * stores only the redacted report and summary (`jsonb`), plus filter
    columns copied from that summary. A test scans every column of every row
    for planted secrets after a real failing request;
  * the table lives in your database: its access control, encryption at rest,
    backups and replication are yours;
  * deletion is logical. Old row versions remain until `VACUUM`, and
    deleted data can persist in WAL, replicas, backups and PITR archives.
    TraceGuard can't erase it there;
  * TraceGuard's queries are sent with `log: false` and
    `telemetry_event: nil`, so report contents don't reach your Ecto query
    logs or telemetry handlers;
  * PostgreSQL error messages (which can quote values) are never stored or
    logged, only error codes.
* **Storage failures** (overload, adapter errors, a locked or missing
  database) never change the application's response or exception. They are
  counted, emitted as `[:trace_guard, :report, :dropped]`, and recorded for
  `report_status/1`. TraceGuard logs nothing per dropped report, so a
  failure storm can't become a log storm.

## Installing the package

* **The Hex package contains only `lib/`** and five top-level files
  (`mix.exs`, `.formatter.exs`, `README.md`, `LICENSE`, `CHANGELOG.md`):
  no tests, demo, scripts, databases or build artefacts.
  `scripts/package_check.sh` enforces that in CI.
* **`mix.lock` records the package's checksum**, and Mix verifies it on
  every fetch, so a version you've locked can't change underneath you.
* **Retired versions:** if a version has to be withdrawn, it's retired on
  Hex with a reason, and `mix hex.audit` reports it in your application.
  Fixes are released as a new version, never by replacing one.
* **The 0.1.0 release candidates** were installed from Git tags
  (`v0.1.0-rc.*`). A Git dependency fetches the whole repository, and a
  tag can be moved on GitHub, though this project never does. They are no
  longer supported: switch to `{:trace_guard, "~> 0.1.0"}`.
* **Dropped reports are expected under pressure.** Queue saturation and
  storage failures drop diagnostic reports, and record each drop (see
  [storage.md](storage.md)). Don't rely on TraceGuard as an audit log.

## Continuous integration (Phase 7)

* Workflows have `permissions: contents: read` and use no secrets. Nothing
  in CI can publish, tag or push. The one exception is the `notify` job in
  `compat.yml`: it gets `issues: write` (and nothing else) to open or
  comment on an issue when a scheduled run fails.
* Third-party actions are pinned to commit SHAs (the tag is in a comment).
  Required CI pins PostgreSQL images to exact minor tags; the weekly
  workflow uses the floating major tags (Phase 8, see
  [compatibility.md](compatibility.md)).
* PostgreSQL runs as a per-job service container with trust
  authentication, reachable only from the job's runner. There is no
  password to commit or leak.
* Logs that CI keeps on failure (smoke-test and compatibility logs, 7 days)
  contain only the fake secrets planted by the tests. On a public
  repository, anyone can read them. Real secrets must never be used in
  tests.
* `scripts/package_check.sh` fails the build if the package would contain
  anything but `lib/**/*.ex` and the five top-level files, secret-like
  strings, databases or build artefacts.
* `scripts/local_postgres.sh` verifies the PostgreSQL project's apt
  repository signature against a pinned key fingerprint, and every package
  against the signed index, before extracting anything.

## Configuration validation

Unknown or ill-typed `:trace_guard` options raise at boot. `TraceGuard.Storage.SQLite` rejects unknown or invalid options when it starts.

## Reporting a vulnerability

Please report security issues privately, rather than in a public issue:
in the repository's
[Security tab](https://github.com/HataluliRandima/trace_guard/security),
choose **Report a vulnerability**. The
[security policy](https://github.com/HataluliRandima/trace_guard/blob/main/SECURITY.md)
lists the supported versions and what to include.
