Rotating session encryption keys without logging everyone out
Session encryption keys are easy to forget because they often work silently. A cookie, token, cache record, or database column is encrypted with a key that was installed months ago. When the key is rotated, the application either keeps the old key forever or invalidates every session at once. Both outcomes weaken the control.
OWASP's key management guidance describes key lifecycle, cryptoperiods, separation of duties, and the need to protect keys throughout their use. NIST SP 800-57 defines a cryptoperiod as the time during which a key is authorized for use or remains in effect. [1] [2] Rotation is therefore a planned lifecycle event, not an emergency-only action.
Inventory the key and its data
Before rotating, identify every place the key is used. A session encryption key might protect browser cookies, server-side session records, refresh tokens, cache values, or signed URLs. The inventory should include the application, environment, data store, algorithm, key version, owner, creation time, and allowed decrypt-only period.
Do not reuse one key across unrelated purposes. A key that protects browser cookies should not also sign email links or encrypt backup files. Separate keys limit the blast radius of a leak and make rotation easier. Version every key so the application can tell which key produced a record.
Verify before encrypting with the new key
Deploy the new key in a verify-only state first. The application should be able to decrypt old records and accept new writes under the new active key. During the overlap, new records use the new key, while old records are re-encrypted lazily or by a bounded background job. The old key remains decrypt-only until the migration completes.
Define a maximum overlap window. A permanent "old key still works" path defeats rotation. Track how many records still use each version. If a record is too old or the migration fails, the application should surface a specific error rather than silently falling back to an untracked key.
Keep sessions valid
A session cookie can carry a key version without exposing the plaintext session ID. On read, decrypt with the matching version and verify the session's server-side state. On write, use the active version. If the session store already has a record, rotate the encrypted value on the next authenticated request. If the application is stateless, use a short overlap and an explicit maximum session age.
For refresh tokens and long-lived API credentials, separate rotation from the session cookie. A new encryption key should not imply that every user must re-authenticate. The goal is to change the protection key while preserving the authorization decision and its recorded lifetime.
Protect the rotation process
Store keys in a secret manager or hardware-backed service with access controls and audit logs. Do not put the new key in a deployment log or a chat message. Require two-person approval for production key activation when the impact is high. Test the rollback path in a non-production environment.
The old key should be destroyed only after the migration is verified and the retention period for old records has ended. Keep a sealed recovery copy if policy requires it, with an independent access path. A key that is deleted too early can make data unrecoverable; a key that is kept indefinitely keeps the compromise window open.
Test failure modes
Test a record encrypted with the old key, a record with an unknown key version, a record with a corrupt tag, and a record written during the transition. Test a restart with only the new key active. Test a rollback to the previous configuration. The application should fail closed for invalid cryptographic data and should not log the key or the plaintext.
Monitor decrypt failures by key version and application. A sudden increase after deployment may indicate a missing key, a bad rollout, or an attempted tampering attack. Keep the alert actionable: identify the service, key version, record class, and safe remediation step.
Key rotation is complete when new writes use the new key, old data is either migrated or deliberately retired, sessions remain valid where policy requires it, and the old key has a defined destruction date. Without that final state, rotation is just a second key sitting beside the first.
References
[1] OWASP, "Key Management Cheat Sheet." https://cheatsheetseries.owasp.org/cheatsheets/Key_Management_Cheat_Sheet.html
[2] NIST, "Recommendation for Key Management: Part 1 - General." https://csrc.nist.gov/pubs/sp/800/57/pt1/r5/final
Top comments (0)