Security
On this page
Rule#
Standard primitives, applied consistently: Argon2 for passwords, ChaCha20-Poly1305 for secrets at rest, HMAC-SHA256 (constant-time) for service handshakes, strict CSP/security headers at the edge, and defensive parsing (SSRF allowlists, decompression caps) for any attacker-influenced input. Auth/authorization is covered in auth-integration.md.
Passwords — Argon2#
Hash with argon2 (Argon2id defaults), random salt via SaltString::generate(&mut OsRng),
verify with verify_password. Mitigate user-enumeration timing: on user-not-found, hash a
dummy password anyway so the response time doesn't reveal whether the account exists.
Secrets at rest — ChaCha20-Poly1305#
Seal sensitive values (API keys, tokens) before they touch the DB:
- 32-byte master key from env (
<PRODUCT>_SECRET_KEY, 64 hex characters), a Komodo secret Variable. Set but malformed ⇒ refuse to boot (a truncated paste is a mistake). Unset ⇒ the features that seal are off, and boot logs a warning saying so. - Never a file under the data dir. The data dir is what the backup sidecar snapshots, and a
key in the snapshot beside its own ciphertext protects nothing (backups.md).
Older services generate
<data_dir>/secret.keyon first boot; that is the pattern being retired, and until a service migrates, its backups can't restore sealed values without the key file kept separately. - Store a
key_versionwith each sealed value so a rotation can be added later without a migration. - Fresh random nonce per encryption; store nonce alongside ciphertext.
- AEAD: a tampered ciphertext fails authentication (don't ignore the tag); reject wrong-length nonces.
Service handshakes — HMAC#
Inter-service auth derives a per-app key HMAC-SHA256(admin_key, app_id) and compares it with
subtle::ConstantTimeEq — never ==. The root key is a shared deployment secret. Details in
auth-integration.md.
Edge headers / CSP#
The internal Caddy proxy sets security headers on every response in one place (deployment.md):
script-src 'self'— nounsafe-inline/unsafe-eval. This is the control that turns an XSS into a no-op for token theft; keep it strict (the built bundle is a single external module + modulepreloads, no inline scripts).style-src 'self' 'unsafe-inline'(React/Radix/motion need inline styles),connect-src 'self' wss://<host>(update if the hostname changes),img-src 'self' data: https:,frame-ancestors 'none'+X-Frame-Options: DENY,base-uri 'self',object-src 'none'.Strict-Transport-Security,X-Content-Type-Options: nosniff,Referrer-Policy: no-referrer, stripServer.
Token handling#
- Bearer JWT in
Authorizationheaders. Web stores it inlocalStorage(native clients can't share a cookie, and the strict CSP neutralises the XSS read vector). <img>and WebSocket upgrades can't set headers, so the token rides as?token=only on those routes — and the request logger strips query strings so it never lands in logs (observability.md).- Native: store in the platform keychain (Apple Keychain, service
dev.thmsn.<product>,afterFirstUnlock; Windows DPAPI). Origin (non-secret) in UserDefaults/settings.
Defensive parsing#
Any attacker-influenced fetch or decode is bounded:
- SSRF allowlist for server-side fetches (remote images): host must match a configured
allowlist (subdomain wildcards ok); reject IP-literal hosts (blocks
169.254.169.254, loopback, private ranges); disable automatic redirects and re-validate every hop'sLocationbefore following; cap redirects and cap body bytes. - Decompression caps: bound zlib/zTXt decompression (e.g. 8 MiB) and detect overflow by
reading one byte past the cap rather than silently truncating. Build the HTTP client without
auto-decompression (
gzip/brotli/deflateoff). - Login throttling on auth endpoints.
Checklist#
- Passwords: Argon2id + random salt + dummy-hash on user-not-found.
- Secrets sealed with ChaCha20-Poly1305; 32-byte key from env (never under the data dir),
key_versionstored; per-secret nonce; AEAD verified. - Service handshakes:
HMAC-SHA256(admin_key, app_id), constant-time compared. - Edge CSP
script-src 'self'; full header set;connect-srchost kept current. ?token=only on asset/WS routes; query strings stripped from logs; native tokens in keychain.- SSRF allowlist + IP-literal rejection + manual redirect re-validation + size caps; decompression bounded.