Skip to content

Keep secrets out of logs and memory, and the key out of the volume - #166

Merged
RikusVanRensburg merged 3 commits into
mainfrom
fix/RVR/config-secrets
Nov 12, 2026
Merged

RikusVanRensburg merged 3 commits into
mainfrom
fix/RVR/config-secrets

Conversation

@RikusVanRensburg

Copy link
Copy Markdown
Contributor

Closes #156

What changed and why

  • No plaintext copy of a file. Done in Make config file writes durable and reads safe #161: State.current_json is gone, "unchanged" is a typed PartialEq compare plus a SHA-256 of the bytes.
  • Secret<T> (koppel-config/src/redact.rs): Debug prints Secret(<redacted>), the value is zeroized on drop, serde is transparent so no file changes, it derefs to the value, and there is no Display. Used for client secrets (ClientConnection, SkakelClientRecord, LegacyClient), private keys, PKCS#12 certificates, 0.1.0's signing key and the UI password hash. secret_hash (SHA-256 of a random secret) stays a plain string. A test fills every secret with a sentinel and checks {:?} and {:#?} of every file struct show none. Cost: .as_deref() on an optional secret gives &String, and the code that builds these structs needs .into(). That touched koppel-web (auth/mod.rs, api/client.rs, roles/skakel/{client,links,store}.rs) and one koppel-usb test helper, only as type conversions.
  • Plaintext buffers are wiped after use: the decrypted JSON in ConfigFile load and write, and the buffers in SecretBox::protect, unprotect and reseal. Not wiped, and documented: serde_json's intermediate values, ring's key schedule, TLS keys, swap, core dumps.
  • Key sources outside the volume: KOPPEL_KEY_FILE, a systemd credential koppel-key ($CREDENTIALS_DIRECTORY) and a Docker secret koppel-key (/run/secrets), tried in that order. KOPPEL_KEY_FILE that names nothing is an error. The file is any secret of 32 to 4096 bytes (openssl rand -base64 32), a trailing newline ignored; HKDF-SHA256 gives the AES key and the key id. Windows keeps DPAPI and warns if the variable is set. No new dependency beyond zeroize (already in the tree).
  • Key ring: the box reads every key it finds and seals with one. An outside key seals when present (the keys directory's koppel-secrets.key is then only read, never made); otherwise koppel-secrets.key seals. koppel-secrets.<anything>.key are older keys, only read, in Koppel's format or a plain secret. SecretBox::reseal runs at start from koppel/src/main.rs over the three secret files and ui-session.key, so a rotation or a move out of the volume is one restart. The start log says which source seals.
  • Threat model written plainly in docs/security.md (key beside the files by default, no passphrase and why, memory limits, logs, the 0.1.x key ring staying, retrievable client secrets), with the procedures to move the key out and to rotate.
  • Decisions recorded in docs/decisions.md: password hash stays PBKDF2-SHA256 (reasons: OWASP figure met, Argon2id needs a new dependency and a second format next to the 0.1.x one that carries over, the hash sits behind a rate limit in an encrypted file); client secrets stay retrievable (the signing key in the same file already allows forged tokens); no proptest or cargo-fuzz (property loops on a fixed generator instead).

Not done, and why

  • The 0.1.x key ring is not deleted after conversion (migrate.rs). The brief keeps that file to the agent moving the upgrade code (The 0.1.x upgrade code leaves the daemon, and certificates are stored as DER #144), and deleting it stops a rollback to 0.1.x. Documented in the threat model; say if you want the deletion.
  • No cargo-fuzz harness (needs nightly and its own crate); randomized tests stand in. The PKCS#12 reader did not panic on 3000 random inputs, 3000 framed random inputs, or truncations and byte changes of a good file.
  • Compose and unit files are not changed (outside this scope): the Docker secret and LoadCredential examples are in docs/security.md only.

Tests

  • New: key sources and their order, short and missing key files, move to an outside key with reseal (twice is a no-op, then the volume key can be deleted), rotation with a renamed key file, rotation of an outside key, foreign-key files left alone, sentinel Debug test, Secret unit tests, seal/open round trip (400 cases up to 8 KiB), every byte flipped (two bits each) and every truncation is an error, random and header-forged input is an error, PKCS#12 no-panic loops.
  • Changed: the secret container's wrong-key message now says "another key than the ones this Koppel has" instead of "another key file than the ... in the keys directory", because the key may no longer be a key file. The test checked the old wording, so its assertion became contains(\"another key\").
  • Run (Linux container rust:1.99.1-trixie): cargo test --workspace --locked all green; cargo clippy --workspace --all-targets --locked -- -D warnings clean. Native Windows: cargo build, cargo clippy -D warnings, cargo fmt --all --check, cargo deny check (advisories, bans, licenses, sources ok).
  • NOT run: tests natively on Windows (project rule; CI runs them), koppel-contract, UI tests, a real systemd credential or Docker secret (the code reads a file; tests use files), an install with a real upgrade.

Docs

docs/security.md, docs/config.md, docs/decisions.md.

Changelog notes:

  • On disk: none by default. With an outside key source, secrets.json, connection.json, ui-auth.json and ui-session.key are written again with a key that is not in the volume (the container format is unchanged, the key id differs). Do not delete the outside key: the files cannot be read without it.
  • Settings: new KOPPEL_KEY_FILE environment variable (Linux and Docker); a systemd credential or Docker secret named koppel-key is used when present. Migration: set the source, start, wait for the log line that files were sealed again, then move koppel-secrets.key out of the keys directory.
  • koppel-secrets.<anything>.key files in the keys directory are now read as older keys.
  • Wire: none. Public API of koppel-config: Secret, KeySource, KeySources, SecretBox::{with_sources, source, keys_read, reseal}; the secret fields of the file structs changed type.

🤖 Generated with Claude Code

@RikusVanRensburg
RikusVanRensburg force-pushed the fix/RVR/config-secrets branch 2 times, most recently from fefb3ea to 30b4071 Compare November 12, 2026 20:07
RikusVanRensburg and others added 3 commits November 12, 2026 22:09
…e config files

Secret<T> prints as Secret(<redacted>) in Debug, wipes its value on drop, and reads and writes
as the value it holds, so no file changes. It covers client secrets, private keys, certificates
with their keys and the password hash in the file structs, and the places that build them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… plaintext

- KOPPEL_KEY_FILE, a systemd credential (koppel-key) and a Docker secret (koppel-key) can
  hold the key. Any secret of 32 bytes or more; HKDF-SHA256 gives the AES key and key id.
- The box reads every key it finds and seals with one. koppel-secrets.<anything>.key files are
  older keys. At start, secret files sealed with another key are written again.
- Plaintext buffers of a decrypted file are wiped after use.
- Property tests for the secret container and the PKCS#12 reader.
- docs: threat model and key sources in security.md, decisions in decisions.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Secrets are not copied, printed or left beside the key

1 participant