Skip to content

Batch 1: correctness & key hygiene (error-response caching, namespace injection, key-encoding docs) - #12

Merged
jzills merged 10 commits into
developfrom
fix/batch-1-correctness-key-hygiene
Jul 7, 2026
Merged

jzills merged 10 commits into
developfrom
fix/batch-1-correctness-key-hygiene

Conversation

@jzills

@jzills jzills commented Jul 6, 2026 •

Copy link
Copy Markdown
Owner

Batch 1 — Correctness & Key Hygiene

First of four themed batches addressing findings from a full repository recon. This batch fixes correctness and key-handling issues; later batches cover resilience/startup, concurrency, and serialization/refresh.

What's fixed

1. Stop caching (and replaying) error responses
The read-through filters cached any non-null result, so NotFound()/BadRequest() bodies were cached and served as Hit for the whole TTL. Both the MVC (ActionCacheFilter) and Minimal API (ActionCacheEndpointFilter) filters now cache only successful (2xx / status-less → 200) responses. A supporting fix makes a null-status ObjectResult (a raw POCO return) count as 200. The MVC gate uses IStatusCodeActionResult so JsonResult/ContentResult/FileResult successes are still cached — symmetric with the Minimal API path.

2. Neutralize namespace / key injection from route values
Templated namespaces (e.g. Teams:{id}) bound user-controlled route values and URL-decoded them, letting a : inside a value merge into the namespace's structural separators (cross-namespace collision / eviction). Route values are now escaped before binding while the template's own separators are preserved, so an injected : becomes %3A (inert) and a clean value still resolves to the same namespace as the equivalent literal (factory.Create("Teams:<id>")).

3. Correct the false "AES encryption" claim (docs-only)
Cache keys use reversible hex encoding, not encryption. XML docs and the README now state this plainly and note keys are not confidential (secure the cache store; don't put secrets in route values/args). Real key encryption is fundamentally at odds with deterministic cache hits + refresh reversibility — see the spec for the rationale.

4. De-flake the absolute-expiration tests
They slept exactly on the 5s expiry boundary (observed ~1-in-3 flake). Now use a 2s expiration with a 4s wait and await Task.Delay instead of Thread.Sleep.

⚠️ Behavior changes (pre-1.0, intentional)

  • Non-2xx responses are no longer cached or replayed on either path.
  • Templated namespaces now escape URL-reserved characters in route values. Clean values (GUIDs, ints, alphanumerics) are unchanged and still match the literal form; only values containing reserved characters (e.g. :) change, and any affected existing entries age out via TTL after upgrade.

Testing

  • Unit suite: 395/395 on net8.0 and net10.0; Release build clean (0 warnings/errors).
  • Integration: the previously-failing/regressed refresh test and all templated-namespace tests pass. An in-flight regression (an earlier whole-namespace-escaping approach broke factory.Create equivalence) was caught by the integration suite and fixed by escaping values only.
  • Note: local integration runs showed intermittent Cosmos DB emulator InternalServerError (500) failures on expiration tests this branch does not touch — pre-existing emulator flakiness, unrelated to these changes.

Follow-ups (non-blocking review notes)

  • DRY the duplicated 200–226 range check across the ~4 status helpers.
  • Harden the (currently unreachable) non-string route-value branch in AttachRouteValues.
  • Minor XML-doc polish (ObjectResultExtensions, IActionResultExtensions summaries).

🤖 Generated with Claude Code

jzills added 10 commits July 6, 2026 16:21
A raw POCO action return produces an ObjectResult with a null StatusCode
that the framework serializes as 200. IsSuccessStatusCode now treats null
as success so success-gated caching (Task 2) keeps caching POCO returns.
Previously any non-null IActionResult was cached, so NotFound()/BadRequest()
error bodies were cached and replayed for the whole TTL. Gate the cache write
on IsSuccessfulResult().
IStatusCodeHttpResult results (e.g. TypedResults.NotFound) are now checked
against the 2xx range before caching; status-less results (raw objects) are
treated as 200 and remain cacheable.
Bound namespace values were URL-decoded, letting a ':' in a user-supplied
route value merge into namespace structure (cross-namespace collision /
eviction). Re-escape the resolved value instead.

BREAKING: changes the on-the-wire namespace format for templated namespaces;
existing entries under templated namespaces age out via TTL after upgrade.
Correct the false 'AES encryption' claim and 'decrypt' wording; document that
keys embed route values and action arguments in cleartext and are not
confidential.
The tests slept exactly on the 5s expiry boundary and flaked. Use a 2s
expiration with a 4s wait for a comfortable margin, and replace blocking
Thread.Sleep with await Task.Delay in these async tests.
Supersedes the whole-string escaping from the prior Task 4 commit
(fa1207e), which escaped the entire resolved namespace and broke the
equivalence between a templated namespace (e.g. "Teams:{id}") and its
literal form (e.g. "Teams:42"). That regression caused
Test_ActionCacheRefreshFilter_WithRouteTemplateParameter to find 0
cached keys instead of 10, because factory.Create("Teams:<id>") no
longer matched the escaped, templated-resolved namespace.

Now each route VALUE is escaped with Uri.EscapeDataString before
TemplateBinder.BindValues runs, and a single HttpUtility.UrlDecode is
applied after. Empirically verified against BindValues' actual
encoding behavior: the template's own structural ':' is single-encoded
by BindValues and is restored to a raw ':' by the single decode
(preserving literal/templated equivalence), while a ':' embedded in a
user-supplied value is escaped before binding, so BindValues
double-encodes it and the single decode leaves it singly-escaped and
inert (injection stays neutralized).
Broadens the MVC cache gate from ObjectResult/StatusCodeResult to any
successful (2xx / status-less) IActionResult - JsonResult, ContentResult,
FileResult - so it no longer silently drops caching for them and stays
symmetric with the Minimal API filter's IsSuccessfulEndpointResult. The
refresh filter's IsSuccessfulResult is unchanged.
@jzills
jzills force-pushed the fix/batch-1-correctness-key-hygiene branch from 071fc76 to 2f30a8f Compare July 6, 2026 21:21
@jzills
jzills merged commit c3b7f71 into develop Jul 7, 2026
4 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