Drop the session cookie if it's useless#2865
Merged
Merged
Conversation
Drop the session cookie if it's useless, to improve our CDN caching. A browser that received a session cookie (e.g. just logged out, or shown a stored flash) keeps sending it until the browser closes. Then the Fastly rule passes every such request to origin and the user never gets the CDN-cached anonymous pages. The method omit_session_cookie only skips *writing* a cookie; it can't remove one the browser already holds. This commit adds an after_action, drop_unneeded_session_cookie, that actively expires the cookie when: the browser sent it, the user is not logged in, there is no flash to carry, and the session holds no real per-user content. The "real content" check (session_has_user_content?) ignores framework bookkeeping keys (Rack's always-present session_id and a spent flash) and is read-only, since mutating the session itself generates a session_id. It fails safe: unknown keys count as content, and anonymous requests that did not send the cookie get no Set-Cookie at all (so cached responses are never polluted). SESSION_COOKIE_NAME is read from config so a cookie rename stays correct. Tests (test/integration/drop_session_cookie_test.rb) check various cases. They assert the observable cookie outcome (not middleware internals) across: spent cookie deleted, no-cookie emits nothing, logged-in kept, CSRF-token kept, flash-display kept, and spent-flash gone on the next request. Co-Authored-By: David A. Wheeler <dwheeler@dwheeler.com> Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2865 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 79 79
Lines 4343 4355 +12
=========================================
+ Hits 4343 4355 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Drop the session cookie if it's useless, to improve our CDN caching.
A browser that received a session cookie (e.g. just logged out, or shown a stored flash) keeps sending it until the browser closes. Then the Fastly rule passes every such request to origin and the user never gets the CDN-cached anonymous pages. The method omit_session_cookie only skips writing a cookie; it can't remove one the browser already holds.
This commit adds an after_action, drop_unneeded_session_cookie, that actively expires the cookie when: the browser sent it, the user is not logged in, there is no
flash to carry, and the session holds no real per-user content. The "real content" check (session_has_user_content?) ignores framework bookkeeping keys (Rack's always-present session_id and a spent flash) and is read-only, since mutating the session itself generates a session_id. It fails safe: unknown keys count as content, and anonymous requests that did not send the cookie get no Set-Cookie at all (so cached responses are never polluted).
SESSION_COOKIE_NAME is read from config so a cookie rename stays correct.
Tests (test/integration/drop_session_cookie_test.rb) check various cases. They assert the observable
cookie outcome (not middleware internals) across: spent cookie deleted, no-cookie emits nothing, logged-in kept, CSRF-token kept, flash-display kept, and spent-flash gone on the next request.