Add AntiforgeryOptions.AllowBackForwardCache - #68738
Open
astralmaster wants to merge 1 commit into
Open
Conversation
By default the antiforgery system sets Cache-Control: no-cache, no-store on responses that carry a token. Browsers treat no-store as opting the response out of the back/forward cache, so navigating back re-fetches the page from the server. Add an opt-in AntiforgeryOptions.AllowBackForwardCache. When set, the header is no-cache, private instead, which keeps the response out of shared caches while allowing the browser's back/forward cache. The default is unchanged. Fixes dotnet#54464
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
Fixes #54464. By default the antiforgery system sets
Cache-Control: no-cache, no-storeon responses that carry a token (DefaultAntiforgery.SetDoNotCacheHeaders). Browsers treatno-storeas opting the response out of the back/forward cache, so using the back button re-fetches the page from the server, which is slower and shows up as a page diagnostics warning.Change
Add an opt-in
AntiforgeryOptions.AllowBackForwardCache(defaultfalse, so existing behavior is unchanged). When set totrue, the header becomesCache-Control: no-cache, privateinstead ofno-cache, no-store: droppingno-storelets the browser use the back/forward cache, andprivatekeeps the response out of shared caches.This follows the triage note on the issue, which suggested marking the header
privateto avoid storage in shared caches. I kept it opt-in so the default does not change and no existing behavior regresses. If you would rather change the default instead (the triage note mentioned that is reasonable, pending browser end-to-end testing), I am happy to adjust.Testing
Added tests to
DefaultAntiforgeryTestverifying that with the option set the header isno-cache, private, and that an existingno-cache, no-storeheader is replaced, while all existing tests for the defaultno-cache, no-storebehavior continue to pass. The full Antiforgery test suite (187 tests) passes on Linux and Windows.