feat(security): store S3 keys as encrypted session cookie#12860
Draft
mekarpeles wants to merge 2 commits into
Draft
feat(security): store S3 keys as encrypted session cookie#12860mekarpeles wants to merge 2 commits into
mekarpeles wants to merge 2 commits into
Conversation
On login, audit_accounts() now returns s3_keys in the audit dict instead of persisting them to the account store. _set_login_cookies() encrypts the keys with Fernet (AES-128-CBC + HMAC-SHA256, key derived from the app secret) and sets them as a Secure, HttpOnly, SameSite=Lax session cookie named "s3". Key retrieval in borrow.py and account.py is updated to read from the cookie first, falling back to the store for sessions predating this change (soft migration). Also adds scripts/purge_s3_keys.py for a future pass to remove plaintext s3_keys from all account store objects. Do not run yet. Closes #12856
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Moves patron S3 (archive.org) key storage from the account store (plaintext at rest) to an encrypted, HttpOnly session cookie set at login time.
Closes #12856
Lead: @jimchamp
What changed
Login flow (
accounts/model.py,plugins/upstream/account.py)audit_accounts()now returnss3_keysin the audit dict instead of persisting them viasave_s3_keys()._set_login_cookies()encrypts the keys with Fernet (AES-128-CBC + HMAC-SHA256; key derived from the app secret via SHA-256) and sets aSecure,HttpOnly,SameSite=Laxcookie nameds3with the same TTL as the session cookie.Key retrieval (
borrow.py,account.py)Both retrieval call sites now use
get_s3_keys(account), which reads from thes3cookie first and falls back to the store. This handles sessions predating this change without forcing re-login.Migration script (
scripts/purge_s3_keys.py)Iterates all account store objects and removes the
s3_keysfield. Do not run until the cookie path has been live long enough that no active sessions rely on the store fallback.Testing checklist
encrypt_s3_keys/decrypt_s3_keysround-trip in Dockers3cookie with correct attributes (Secure, HttpOnly, SameSite=Lax)s3cookie is rejected gracefully, falls back to storescripts/purge_s3_keys.py --dry-runlists accounts with s3_keys without writingmake testpasses in DockerNotes
cryptography==44.0.2added torequirements.txt(may already be a transitive dep viainternetarchive; pin version as needed)save_s3_keys()remains onOpenLibraryAccountfor now; will be removed in a follow-up once the purge script has been run