fix(cache): mark auth-required responses no-store instead of 1h public - #82
Merged
Merged
Conversation
The /v1 migration in #149 left the Cache-Control middleware's path prefix check pointing at pre-v1 routes, so authenticated endpoints quietly fell into the public-cache branch. Browsers were storing user inventory lists for an hour with no cookie-keyed cache variance, which reproduced as "deleted items come back on reload" and "newly uploaded saves don't appear" — the write path hits the backend while the read path is served from the HTTP cache. Tie the cache decision to the auth dependency itself: get_current_user sets request.state.auth_required before any raise, and the middleware emits Cache-Control: no-store + Vary: Cookie for any response flagged that way. New auth-required routes are covered automatically without having to remember to update a prefix list.
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
/v1path migration in #149 left theCache-Controlmiddleware's path prefix check (startswith("/user/")) pointing at pre-v1 routes, so authenticated endpoints silently fell into the public-cache branch — prod was emittingCache-Control: public, max-age=3600onGET /v1/user/inventoriesand/v1/loadout/*with noVary: Cookie.GETrequests reaching the API after the DELETE in the repro session — the browser cache was answering them.get_current_usersetsrequest.state.auth_required = True(before any raise, so 401s are also flagged), and the cache middleware emitsCache-Control: no-store+Vary: Cookiewhenever that flag is present. Any future auth-required route is covered automatically by depending onget_current_user— no prefix list to maintain.Test plan
uv run pytest— 24 passed, including newTestCacheHeadersclassuv run ruff check/ruff format --check— cleanGET /v1/blades→Cache-Control: public, max-age=3600GET /v1/user/inventories(unauthed 401) →Cache-Control: no-store+Vary: CookieGET /v1/user/inventoriesreturnsCache-Control: no-storeand a repro of the original delete→reload flow no longer sticks