Prevent shared caching of session cookie responses - #412
Merged
Merged
Conversation
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
SessionMiddlewarecreates a session on every cookie-less route it wraps. With a broadly wrapped mux, the built-in CSS and JS handlers can replace an earlier cache policy with a public one while the response still carriesSet-Cookie.When the middleware creates a Session, protect its cookie at header commit with
Cache-Control: no-store. Requests with an existing Session use the original response writer and keep the handler's cache policy, so ordinary asset requests remain publicly cacheable. This covers explicit writes, flushes, implicit 200 responses, and WebSocket upgrades. Applications that create or rotate Sessions directly remain responsible for their own response cache policy.Fixes #399.
Validation
go test -race ./...go vet ./...go build ./...gosec -quiet ./...gofumpt -l session.go session_test.goPackage race coverage is 99.7%; the session response writer methods are fully covered.
BenchmarkSessionMiddlewareExistingSessionmeasures the existing-session path with a 204 response. Six runs per version, compared withbenchstat:golangci-lintis not installed here. The installedstaticcheckreports that./...matches no packages.