Narrow SessionToken saves to mutated fields to preserve concurrent revocations - #34
Merged
Merged
Conversation
…vocations A full save() after loading a token via .active() wrote the stale in-memory revoked_at=None back to the database, silently un-revoking a token revoked between the SELECT and the save. Use update_fields at all three save sites (authenticate_payload, ObtainSessionTokenView, ObtainAuthorizationTokenView) so only the fields actually mutated are written. Fixes #33 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Fixes #33
Problem
utils.authenticate_payloadloaded the token viaSessionToken.objects.active().get(...), stampedlast_used_at(andip_address/user_agent/versionviaupdate_attributes), then called a baresession_token.save(). The in-memory instance still carriedrevoked_at=None, so a revocation landing in the DB between the SELECT and the save was silently overwritten — the token was un-revoked.The review found the same pattern at the two other
save()sites in the library:ObtainSessionTokenView.postandObtainAuthorizationTokenView.post, where the resurrected session additionally received a freshly issued JWT.Fix
Save with
update_fieldslimited to the fields each call site actually mutates:authenticate_payload:last_used_at, plusip_address/user_agent/versiononly when a request was passedObtainSessionTokenView.post:ip_address,user_agent,version,last_issued_at(full insert for new tokens, detected via_state.addingsince the UUID pk is pre-assigned)ObtainAuthorizationTokenView.post:ip_address,user_agent,versionSide benefit: a concurrently deleted row now raises ("did not affect any rows") instead of Django's update-then-insert silently resurrecting it.
Tests
SessionToken.saveto inject a queryset-level revocation between the SELECT and the save, assertrevoked_atsurvives in the DB. All three fail against the unfixed code.test_authenticate_persists_request_attributes_and_last_used_atguards theupdate_fieldslist completeness against future additions toupdate_attributes.🤖 Generated with Claude Code