Back to blog
·4 min read·Cell Webb

Strict Mode for Secrets at Rest

How KittyLog's token-encryption migration reached its third step: a fail-closed flag that rejects any plaintext GitHub token instead of tolerating it.

An encryption-at-rest migration has three steps: encrypt every new write, backfill the rows you already have, then flip a switch that makes plaintext an error instead of a warning. Most teams stop after the first step, and I understand why, because the third is the only one that can break production on purpose.

KittyLog stores GitHub access tokens, which is the kind of data you really do not want sitting readable in a database. This is the story of how our migration got through all three steps, including the awkward stretch where step three existed only in a docstring.

The tolerant middle

Step one landed a long time ago: every token written to our database goes through Fernet encryption with an Argon2id-derived key. Step two was a one-off script, scripts/migrate_encrypt_tokens.py, that dry-runs by default, counts plaintext-looking rows, and only rewrites them with --execute.

Between those steps and the finish line sits a function called safe_decrypt_token. During a migration you need it to be tolerant: if the stored value looks encrypted, decrypt it; if it looks like plaintext, log a warning and return it anyway, trusting that the next write will encrypt it. That fallback is correct while the backfill is in flight. Left in place forever, it quietly cancels the guarantee. A bug that writes one plaintext token, or a manual database fix done at 2am, produces a row that works fine and never gets noticed. After enough of those, you can no longer say with confidence that every token is encrypted at rest.

The docstring that got ahead of the code

The embarrassing part: issue #1305 was filed because the docstring in api/core/crypto.py referenced a REQUIRE_ENCRYPTED_TOKENS config flag, and no such setting existed anywhere. Someone had written down the enforcement mode before anyone built it. The code beneath the docstring still accepted plaintext with a warning, indefinitely.

Commit 61fd555d closed the gap. There is now a real require_encrypted_tokens setting, default false. When it is on, safe_decrypt_token raises a ValueError on any plaintext value instead of returning it. The error message tells you exactly which script re-encrypts the row. One small detail I like: the check is settings.require_encrypted_tokens is True, an identity comparison, so a careless test mock can't accidentally flip strict mode on.

Why it ships off

The flag defaults to false because flipping it is a commitment, not a checkbox. The moment it is true, a plaintext row causes a failure in whatever request touched it. A user's repository sync breaks. So the order matters: audit first. The admin security audit endpoint counts plaintext-looking rows, and the migration script's dry run reports the same number. You flip the flag when that count is zero and you believe your write paths, not before.

What you get in exchange is that regressions become impossible to miss. Any future code path that writes plaintext will blow up loudly the first time the token is read, instead of surviving for months as a warning nobody greps for. Steps one and two make the data encrypted today; step three keeps it encrypted next year. That is the entire point.

This fits a habit that shows up elsewhere in the codebase. The JWT blacklist check fails closed by default: if Redis is down and a token isn't in the local allowlist, it is denied, and you have to explicitly configure jwt_redis_fail_mode=open to prefer availability. Model selection rejects unknown model IDs outright rather than passing them through to a provider. In each case the default answer to "we can't verify this" is no.

What encryption at rest actually buys

This is a spot where security writing tends to inflate. Encryption at rest means a database dump or a stolen backup yields ciphertext instead of usable GitHub tokens. That is a real and worthwhile property.

It does not protect you from an attacker who compromises the running application, because the app holds the encryption key and decrypts tokens to use them. It does nothing about tokens in memory, or in transit to GitHub (that's TLS's job). It is one layer, and we treat it as one layer: the GitHub App also requests minimal permissions, so even a decrypted token has a bounded blast radius.

If you shipped step one of your own migration a while ago, the practical move is small: write the query that counts plaintext rows, run it, and schedule the backfill if the number isn't zero. The strict flag is maybe thirty lines of code. The audit that makes it safe to enable is the actual work.

Source records

  • Issue #1305, enforce encrypted-tokens-only mode.
  • Commit 61fd555d / PR #1314, the REQUIRE_ENCRYPTED_TOKENS strict mode.
  • PR #62, the token encryption migration script.
  • api/core/crypto.py, encryption and the strict-mode check.
  • api/core/token_revocation.py, the fail-closed blacklist behavior.

Ready to automate your changelogs?

Try KittyLog free - no credit card required.

Get Started Free