Skip to content

fix(cache): opt-in vary_headers with safe key isolation (#53) - #81

Merged
agustin18 merged 30 commits into
stablefrom
fix/53-explicit-vary-headers
Nov 20, 2026
Merged

agustin18 merged 30 commits into
stablefrom
fix/53-explicit-vary-headers

Conversation

@agustin18

@agustin18 agustin18 commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Summary — reviewer-blocking Vary bug corrected, draft / not merged

Exact candidate: 3fa1d6414b8bd63be6da1cc41d6aaee8d987d8c1 from stable@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-Request default-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 c55b3fc generated HTTP Vary only on the injected FastAPI Response; when the endpoint returned JSONResponse or a plain Response with PickleCoder, Starlette transmitted that returned response instead. The returned object could therefore lack Vary on 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 adding Vary to a returned Response (otherwise a simple JSONResponse could be rejected by JsonCoder). Add declared Vary before encoding so a pickled Response retains it. On MISS and HIT, apply the generated ETag, cache-status, Cache-Control and Vary metadata to the actual returned Response as well as the injected response, only for explicitly opted-in variants. 304 uses the injected response with the generated Vary. Other routes retain their prior behavior.

Regression coverage and security scope

  • New parameterized tests for bare JSONResponse under default JsonCoder and plain Response under PickleCoder, checking MISS, HIT, 304, Vary, ETag, cache status and variant isolation.
  • New factory-handler composition test for issue fix(keys): test collisions between generated routes and isolate cached identities safely #68: route-scoped generated key plus :vary-v1: produces isolated route/header variants.
  • Renamed the direct Header(...) test to say it verifies default partitioning, not a specific literal key.
  • Installed-wheel/real-Redis consumer tests/redis_vary_probe.py now runs both returned-response cases on the published-range matrix (no Redis FLUSHDB).
  • Existing tests guard repeated/missing/empty values, Vary: *, undeclared Vary even with PickleCoder, explicit Request and auth bypass, contextvars, seeded pre-opt-in keys, scoped namespace clear, custom builder and invalid header names.
  • Documentation is explicit: only Authorization, Cookie and Proxy-Authorization are rejected as credential-related vary_headers names. 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

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.

@agustin18
agustin18 marked this pull request as ready for review November 20, 2026 23:33
@agustin18
agustin18 merged commit 58fd446 into stable Nov 20, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant