Repository navigation
fix(memory): re-index session on Save so RegenerateID persists - #183
Merged
Merged
Conversation
The memory store keyed sessions by the ID seen at Read time and its Save was a no-op. After RegenerateID (or Destroy plus RegenerateID, as session rotation does) the session kept its data in memory but was no longer indexed under its new ID, so the next Read returned a fresh empty session and the data was lost. Make Save re-index the session under its current ID, dropping any stale entry that still points at the same session object. The common no-rotation path stays a fast no-op. Add a regression test covering RegenerateID across requests.
…nged - Save now deletes any index entry still pointing at the rotated session object, so the old session ID is invalidated instead of remaining live in the index, and repeated rotations no longer leak index entries. - BaseSession.RegenerateID marks the session as changed so a rotate-only request still hits Save (or the store's equivalent) instead of Touch, which silently dropped the rotation for the memory store and skipped persisting the new ID for other stores. - Add TestMemoryStore_RegenerateIDOnly covering a rotate-only request and asserting the old ID is no longer indexed.
…ess time; drop added tests - Save now deletes stale index entries pointing at the rotated session object, then re-keys the map entry in place when the session is still in the heap, avoiding heap.Remove/Push churn and leaving LastAccessedAt untouched. - Revert the changed flag in BaseSession.RegenerateID; the fix stays entirely inside memoryStore.Save. - Remove TestMemoryStore_RegenerateID and TestMemoryStore_RegenerateIDOnly.
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.
Description
The in-memory store keys sessions by the ID seen at
Readtime, and itsSaveis a no-op, relying on the session object staying live in the index. This breaksRegenerateID: after a handler rotates the session (e.g. storing OIDCstate/noncebefore redirecting to the IdP), the session keeps its data in memory but is no longer indexed under its new ID, so the next request'sRead(newID)returns a fresh, empty session and the data is silently lost.Every other store (file/postgres/mysql/redis/…) works because their
Savewrites keyed bysess.ID(); the memory store's no-opSaveis the only inconsistent one.Fix
memoryStore.Savenow reconciles the index with the session's current ID:heap.Pushit if not.LastAccessedAtis left untouched, so GC ordering is unaffected. The fix stays entirely insidememoryStore.Save;Read,Destroy,Touch, and theBaseSession/RegenerateIDAPI are unchanged.Test plan
TestMemoryStore_RegenerateID(inmemory_test.go): rotates the ID and sets a value in one request, then reads back with the regenerated cookie in a second request, asserting the data survived and the old ID is no longer indexed. Fails onmain, passes with this fix.go test ./...passes.Tag
@indentto continue the conversation here.