Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,21 @@ with the same rules.
nonce before the ciphertext. The key file is made through a temp file and a hard link, so it never
exists half written; on a file system without hard links (some network, FUSE and Docker bind
mounts) it is made in place with `create_new`, and a start that meets it mid-write waits for it.
Two Koppels starting together end up with one key either way. Data Protection's key ring is no longer used. As before, anyone who can
read the keys directory on Linux can decrypt the files.
Two Koppels starting together end up with one key either way. Data Protection's key ring is no longer used. By default anyone who can
read the keys directory on Linux can decrypt the files, since the key sits beside them; a key from
`KOPPEL_KEY_FILE`, a systemd credential or a Docker secret moves it out (see "Keeping the key outside
the volume" in `docs/security.md`).
- **The key ring:** the box reads every key it can find and seals with one. A key from outside the
volume seals when there is one (and `koppel-secrets.key` in the keys directory is then only read,
never made); otherwise `koppel-secrets.key` seals. `koppel-secrets.<anything>.key` files are older
keys, only read. A file's key id says which key opens it, and at every start the secret files
(and `ui-session.key`) that an older key sealed are written again with the current one, so a
rotation, or a move of the key out of the volume, is one start. A key from outside is any secret
of 32 to 4096 bytes; the AES key and the id come from it by HKDF-SHA256.
- **Secrets in memory:** the secret fields of the file structs are `Secret<String>`, which prints
`Secret(<redacted>)`, wipes itself when dropped and reads and writes as the string it holds, so the
files do not change. The plaintext of a file being read or written is wiped after use. No copy of a
file's JSON is kept to see whether it changed (a SHA-256 of the bytes and a typed compare do that).
- **The upgrade from 0.1.x:** at the first start, each of `secrets.json`, `connection.json` and
`ui-auth.json` that is still a Data Protection payload is decrypted with the 0.1.x key ring (machine
DPAPI on Windows, plain XML on Linux) and written again in the new format, with the same JSON inside.
Expand Down
12 changes: 12 additions & 0 deletions docs/decisions.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@
- **Names.** `KOPPEL_` and the option in capitals (`KOPPEL_UI_PORT`), flat, not `KOPPEL_PORTS_UI`: the section half of 0.1.x's names only mirrored .NET's configuration tree. 0.1.x's names are aliases that log once as deprecated and are kept through 0.2.1; dropping them is the owner's call for a later release. The aliases are rewritten to the new options before clap reads anything, so there is one parser and one set of rules, and a test checks that every variable in the struct has an old name and every old name has an option.
- **`healthcheck` is a TLS handshake and one fixed request, not an HTTP client.** Measured on the same build: the hyper client version was 7,824,896 bytes, the raw version 7,795,712, so 29,184 bytes (0.4 percent) less, and `koppel` no longer asks for hyper's `client` feature or the `hyper-util` and `http-body-util` crates. The code is shorter too: it sends `GET /healthz HTTP/1.1` with `Connection: close` and reads only as far as the status line. The certificate check is still off, for the same reason as before (see `healthcheck.rs`). The saving is small; what decided it is that an HTTP client was more machinery than a status line needs.
- **Blank is unset.** A blank value (`KOPPEL_UI_PORT=` from a compose file's `${VAR:-}`) leaves the setting at its default, as a blank value did in 0.1.x.


## Secrets at rest decisions (2026-11-12)

- **A key outside the volume, not a passphrase.** Koppel starts unattended, so nothing can ask for a passphrase. `KOPPEL_KEY_FILE`, a systemd credential and a Docker secret keep the key apart from the volume, which is what a backup copies. All three are a file; one code path reads them. They are tried in that order, and a `KOPPEL_KEY_FILE` that names nothing stops start-up instead of quietly falling back to a key beside the files.
- **The key file is any secret, derived with HKDF.** An admin makes it with `openssl rand -base64 32` rather than a Koppel tool. HKDF-SHA256 (ring, already a dependency) turns it into the AES key and the key id, so the id stays stable and says which files the key opened. Considered: requiring Koppel's own `KPSK` format. That would need a command to make one for a Docker secret or a credential, for no gain.
- **A key ring of every readable key, sealing with one.** The container already carried a key id and refused any other, so a rotation was a dead end. The ring is cheap: the keys directory's `koppel-secrets.key`, any `koppel-secrets.<anything>.key` (older keys) and the outside key. The sealing key is the outside key when there is one, otherwise `koppel-secrets.key`. "Newest by file time" was rejected as fragile; the name decides. Files sealed with another key are written again at start, so a rotation or a move is one restart.
- **Password hash stays PBKDF2-SHA256 through ring.** 600,000 iterations is the OWASP figure for PBKDF2-HMAC-SHA256. Argon2id would be better against GPUs, but it needs a new dependency (the RustCrypto `argon2` crate), a second hash format next to the 0.1.x one that carried over from existing installs (so a login would verify two formats and rehash), and the hash only protects a password that is already behind a rate limit, a lock-out and a 128 character cap, inside a file that is encrypted. The gain is small and the migration is not. Revisit if the hash ever leaves the encrypted file.
- **Client secrets stay retrievable (`KeepClientSecrets`).** Showing a secret once and keeping only its hash would stop an admin copying it again from the client's row, which they do. The gain is small: the token signing key sits in the same encrypted file and lets whoever reads it forge a token for any client, so a reader of the file does not need the client secrets. It would also change the UI. If that file's confidentiality is ever split (signing key kept elsewhere), show-once becomes worth it.
- **`Secret<T>` over a hand-written `Debug` per struct.** A newtype cannot be forgotten on a new field the way an impl can, and it carries the wipe on drop. Cost: `.as_deref()` on an optional secret gives `&String`, and code that builds the structs needs `.into()`.
- **No proptest, no cargo-fuzz.** The properties (seal then open is the identity, a flipped byte or a truncation is an error and never a panic, the PKCS#12 reader never panics on random or damaged DER) run as ordinary tests on a fixed xorshift generator, thousands of cases in about a second. proptest would add a dev dependency tree and shrinking, which short byte inputs do not need. A cargo-fuzz harness needs nightly and a separate crate; add one if the PKCS#12 reader is ever opened to input from outside a file the admin carried over.
- **The 0.1.x key ring is not deleted after conversion.** Deleting it would stop a rollback to 0.1.x, and the code that converts is being moved out of the daemon (issue #144). It is documented in the threat model instead.
30 changes: 29 additions & 1 deletion docs/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ The UI on port 5240 (HTTPS) needs a password. There is one admin password and no
& "C:\Program Files\Matumi\Koppel\Koppel.exe" reset-password
```

### Keeping the key outside the volume

The files are sealed with AES-256-GCM. The key can come from a file kept apart from the config and keys directories, so a backup or a copy of the volume does not carry it. The first of these that applies is used:

1. **`KOPPEL_KEY_FILE`**: the path of a file. When it is set the file must exist, otherwise Koppel stops and says so.
2. **A systemd credential** named `koppel-key`: `LoadCredential=koppel-key:/etc/koppel/key` (or `ImportCredential=`) in the unit makes `$CREDENTIALS_DIRECTORY/koppel-key`, readable by the service only and not by the other users on the machine.
3. **A Docker secret** named `koppel-key`: mounted at `/run/secrets/koppel-key`. In Compose, list it under `secrets:` for the service and under the top level `secrets:` as a `file:` or `external` secret.

The file is any secret of 32 bytes or more, up to 4096; a trailing line break is ignored. Make one with:

```sh
openssl rand -base64 32 > koppel-key
chmod 600 koppel-key
```

The AES key and a key id are derived from it with HKDF-SHA256, so the same file is the same key every time. Windows ignores all of this and keeps DPAPI (Koppel warns if `KOPPEL_KEY_FILE` is set there).

Moving an install over: set the source and start Koppel. The old `koppel-secrets.key` in the keys directory is then only read, never made again, and at start every secret file sealed with it is written again with the new key (the log says how many). Once the log says so, stop Koppel, move `koppel-secrets.key` out of the keys directory (keep a copy with your offline backup, not in the volume), and start again. Losing the key outside the volume makes the secret files unreadable, as losing the keys directory does: see "If the key is lost" in `docs/config.md`. Back the key up separately from the volume.

Rotating the key, with the key in the keys directory: stop Koppel, rename `koppel-secrets.key` to `koppel-secrets.<anything>.key`, start Koppel. It makes a new `koppel-secrets.key`, reads the renamed one, and seals every file again with the new key. When the log says so, delete the renamed file. Rotating a key from outside the volume is the same with a new key file: copy the old key file into the keys directory as `koppel-secrets.<anything>.key` (a retired key may be a Koppel key file or a plain secret like the ones above), point the source at the new one, start Koppel, and delete the copy once the log says the files were sealed again.

What this buys: a copy of the volume alone, such as a backup, a snapshot or a disk image of the data folder, no longer opens `secrets.json`, `connection.json` and `ui-auth.json`. What it does not: a copy of the machine's whole file system, or root on the running machine, still has the key.

### Guards on the web surface

- **Origin and header (UI surface):** an `Origin` that does not match the request's own scheme, host and port gets 403 on any `/api/*` call, the live push included. Behind a TLS proxy the scheme comes from `X-Forwarded-Proto`. A change (anything but GET or HEAD) also needs `X-Koppel-UI: 1`, otherwise 403. A page on another site cannot add that header without a CORS preflight, which this server never grants. The UI adds it in `api/http.ts` (`uiFetch`, used by every mutating fetch). The live push is a GET, so it needs no header, which an `EventSource` cannot send.
Expand Down Expand Up @@ -75,5 +98,10 @@ What it does not cover:
- `KOPPEL_UI_ALLOW_FROM` and the per-caller limits on sign-in and pairing go by the caller's address as Koppel sees it; forwarded headers are not read. Behind NAT, such as port publishing that rewrites the source address, every caller arrives from one address: the local network check then lets everyone in, and one caller's wrong passwords lock out everyone else's address too.
- The gateway on 3242 is plain USB/IP with no login (see ["USB client"](usb.md#usb-client)). Whoever reaches it uses the client's access to the server. On native Windows with a loopback address only the kernel's own connections get in, and only for imports Koppel asked the driver for; elsewhere, such as a container, the port publishing is all that keeps it local.
- A registered client and the server it is paired with trust each other's USB/IP traffic: what one sends goes to the other's kernel driver (`usbip-host`, `vhci_hcd` or usbip-win2's driver) as it comes, and an attached device can be any device, a keyboard that types included. Koppel checks the bus ID and VID:PID of an import, not the traffic after it.
- Anyone who can read the keys directory can decrypt the secret files, unless `Paths:KeyCertificate` protects the key ring (see ["Config on disk"](config.md#config-on-disk)). That setting is deprecated and goes in 0.2.0.
- **On Linux and in Docker the key sits beside the files it protects.** By default `koppel-secrets.key` is in the keys directory, and the keys and config directories are one volume (`/koppel` in the image). A backup or a copy of the volume holds the key and the ciphertext together, so the encryption then protects only a copy of the config directory on its own. Anyone who can read the keys directory can decrypt the secret files. Keep the key elsewhere to change that (see "Keeping the key outside the volume" below). On Windows the key is machine-scope DPAPI: any process on the machine can unseal the files, as with 0.1.x.
- **The key is not a passphrase.** Koppel has to start unattended after a power cut, so nothing asks for one at start. A key kept outside the volume is still a file that Koppel reads at start, and root on the machine can read that file, or the process's memory, as it can for any service.
- **Memory.** Secrets are decrypted into memory while Koppel runs: the signing key, the certificate's private key, client secrets, the UI password hash. The values Koppel owns are wiped when they are dropped (`Secret`, and the buffers holding a decrypted file). That is best effort: the JSON parser's intermediate copies, the TLS stack's keys and the AES key schedule are not wiped, nothing is locked out of swap, and a core dump holds them. Turn core dumps off on a machine that matters (`LimitCORE=0` in the service, `--ulimit core=0` in Docker).
- **Logs.** The config structs print `Secret(<redacted>)` for every secret, so a `{:?}` of one cannot leak it. A test holds that line. Koppel's own log messages do not carry secrets; a log line written by hand that formats a secret is the way one gets out, and the `Secret` type has no `Display` to make that easy.
- **The 0.1.x key ring stays.** After a 0.1.x install is converted, its Data Protection key ring (`keys/aspnet`, plain XML on Linux) is left where it was, so the install can go back to 0.1.x. Until you delete it, it can decrypt whatever 0.1.x sealed. Keep it with the backup or delete it once you are sure.
- **Client secrets can be read again.** Koppel keeps each client's secret inside the encrypted `secrets.json` so the admin can copy it again from the client's row. Anyone who can read the decrypted file can read them; see "Show-once client secrets" in `docs/decisions.md`.
- The Linux container is privileged and runs as root, and the Windows service runs as LocalSystem. A flaw in Koppel is a flaw with that reach.
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ serde_json = { version = "1.0.154", features = ["raw_value", "preserve_order"] }
base64 = "0.23.1"
time = "0.3.55"
ring = "0.17.14"
# Wiping a secret when it is dropped (koppel-config's `Secret`). zeroize (MIT OR Apache-2.0, RustCrypto) is already here
# through the crypto crates.
zeroize = "1.9.1"
# What ring lacks for reading 0.1.x's files: AES-256-CBC for Data Protection payloads and 3DES-CBC
# for .NET's PKCS#12 export. RustCrypto, MIT OR Apache-2.0.
aes = "0.9.4"
Expand Down
1 change: 1 addition & 0 deletions src/koppel-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ time.workspace = true
tokio.workspace = true
tracing.workspace = true
ring.workspace = true
zeroize.workspace = true
rustls.workspace = true
aes.workspace = true
cbc.workspace = true
Expand Down
8 changes: 5 additions & 3 deletions src/koppel-config/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use serde_json::Value;
use time::OffsetDateTime;
use tokio::sync::{mpsc, oneshot};
use tokio::task::JoinHandle;
use zeroize::Zeroizing;

use crate::json;

Expand Down Expand Up @@ -501,7 +502,7 @@ where
}

async fn write(&self, value: &T) -> Result<Seen, ConfigFileError> {
let json = versioned_json(value);
let json = Zeroizing::new(versioned_json(value));
let path = self.path.clone();
let codec = self.codec.clone();
tokio::task::spawn_blocking(move || {
Expand Down Expand Up @@ -774,13 +775,14 @@ fn load_blocking<T: Serialize + DeserializeOwned>(
if known == Some(seen.hash) {
return Ok(Loaded::Same(seen));
}
let text = codec.decode(&bytes).map_err(|e| {
// The plaintext of an encrypted file is wiped when this function is done with it.
let text = Zeroizing::new(codec.decode(&bytes).map_err(|e| {
ConfigFileError::new(
path,
ConfigErrorKind::Decrypt,
format!("it could not be decrypted ({e})"),
)
})?;
})?);
let not_json = |e: serde_json::Error| {
ConfigFileError::new(
path,
Expand Down
Loading