Repository navigation
fix(cache): opt-in vary_headers with safe key isolation (#53) - #81
Merged
Merged
Conversation
This was referenced Nov 20, 2026
…ry-key composition (#53)
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 — reviewer-blocking Vary bug corrected, draft / not merged
Exact candidate:
3fa1d6414b8bd63be6da1cc41d6aaee8d987d8c1fromstable@b4d7d968b97dedbf975b71dc65a94b6d34012f2c. No tag, dependency change, published release, or global key rewrite.This PR introduces opt-in
@cache(vary_headers=(...))for explicitly listed request headers with versioned hashed keys. It preserves the published default key, the explicit-Requestdefault-builder bypass and default auth bypass. It does not auto-detect hidden context, tenant identity, role, middleware state or arbitrary application permissions.Independent audit / reproduced blocker
The first implementation at
c55b3fcgenerated HTTPVaryonly on the injected FastAPIResponse; when the endpoint returnedJSONResponseor a plainResponsewithPickleCoder, Starlette transmitted that returned response instead. The returned object could therefore lackVaryon MISS and/or HIT, leaving intermediaries without a proper cache-variation contract even though the internal key was partitioned.Fix: check
effective_coder.can_encode(result)before addingVaryto a returnedResponse(otherwise a simple JSONResponse could be rejected by JsonCoder). Add declaredVarybefore encoding so a pickledResponseretains it. On MISS and HIT, apply the generated ETag, cache-status, Cache-Control andVarymetadata to the actual returned Response as well as the injected response, only for explicitly opted-in variants. 304 uses the injected response with the generatedVary. Other routes retain their prior behavior.Regression coverage and security scope
JSONResponseunder defaultJsonCoderand plainResponseunderPickleCoder, checking MISS, HIT, 304, Vary, ETag, cache status and variant isolation.:vary-v1:produces isolated route/header variants.Header(...)test to say it verifies default partitioning, not a specific literal key.tests/redis_vary_probe.pynow runs both returned-response cases on the published-range matrix (no RedisFLUSHDB).Vary: *, undeclared Vary even withPickleCoder, explicit Request and auth bypass, contextvars, seeded pre-opt-in keys, scoped namespace clear, custom builder and invalid header names.Authorization,CookieandProxy-Authorizationare rejected as credential-relatedvary_headersnames.X-API-Key/custom auth headers can pass syntax validation but are not automatically authenticated or safe; use validated identity and a full custom key builder or bypass. Attacker-controlled values can flood caches; SHA-256 does not encrypt, authenticate or bound key cardinality. Operators must rotate/clear historical namespaces.Exact-HEAD CI: all seven workflow runs successful
fastapi-cache2==0.2.2vs releasedfastapi-cache-stable==0.4.1, not the new code in fix(cache): opt-in vary_headers with safe key isolation (#53) #81. The new code is tested by the branch-built installed wheel tests; future test(migration): published legacy-to-stable parity, clean install and rollback contract for 0.5.0 #69 phase B must validate exact 0.5.0 wheel+sdist.These are 56/56 jobs across seven workflows, all verified completed successfully on the exact SHA. Earlier failed checks on previous commits and the user's independent reproduction remain part of review history.
Merge gate
Keep DRAFT. CI verifies the tested contract, not all application-level isolation. Independent reviewer should inspect returned Response/header semantics, custom-key-builder interactions, existing
Vary, proxy/tenant header trust and cache flooding controls before approving. No #53 issue closure or merge on CI alone.After approval/merge: re-run #69 phase A if desired as a historical invariant; #69 phase B must exercise the exact final version-stamped 0.5.0 wheel and sdist, then #66 fresh installed-audit/authenticated delta/risk signoff, then separate manual 0.5.0 release. #49/#54/#50 remain optional.
Refs #53 #69 #66 #70.