Skip to content

Batch 4a — Response envelope (drops polymorphic deserialization from the value path) - #19

Merged
jzills merged 1 commit into
developfrom
fix/batch-4a-response-envelope
Aug 23, 2026
Merged

jzills merged 1 commit into
developfrom
fix/batch-4a-response-envelope

Conversation

@jzills

@jzills jzills commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Stacked on #18, which is stacked on #17. Merge in order; each retargets cleanly.

First half of Batch 4. Splitting it because the replayable request travels inside the envelope, so the envelope has to land first — and two reviewable PRs beat one mega-change.

What was wrong

Cached values were IActionResult object graphs serialized by Newtonsoft with TypeNameHandling.Auto. A cache entry therefore named the types deserialization should construct, and the only defense was SafeSerializationBinder — a deny-list of five known gadget namespaces. Deny-lists lose; that is the whole problem with them.

What replaces it

A CachedResponse of primitives — status code, content type, already-rendered body — serialized with System.Text.Json through a source-generated context. No $type, no type resolution, nothing in a cached payload that can influence which types get constructed. The vulnerability class is removed, not filtered. A regression test asserts no type discriminator survives serialization.

Bodies are rendered with the application's own JsonSerializerOptions, so a cached body is byte-identical to what the action would have written. A test proves this by configuring a camelCase policy and asserting the body is camelCase rather than serializer defaults.

The envelope also carries VariesByRequest and the recorded CachedRequest (method, path, query) — unused here, consumed by 4b. Headers are deliberately not recorded: they routinely carry credentials, and a cache entry is no place for them.

One thing I found while implementing

JsonResult is not an ObjectResult. Handling results generically would have silently stopped caching a result that IsCacheableResult accepts, so it gets an explicit case.

Scope note — #5 is not fully closed by this PR

Cache keys still use Newtonsoft, now isolated behind KeyComponentSerializer with the binder attached. Reflection-based refresh reverses a key back into typed action arguments and needs TypeNameHandling to do it, so it cannot go until refresh stops reflecting. 4b deletes the reverse path, the binder, and the Newtonsoft dependency together. Saying so plainly rather than claiming a clean sweep.

Breaking changes (pre-1.0)

  • Existing cache entries are unreadable. Expect a cold cache on deploy. Recoverable by definition — it is a cache — but it should not be a surprise.
  • Results that do work at execution time (FileStreamResult, RedirectResult, PhysicalFileResult) are no longer cacheable. They arguably never were: the previous code serialized a file result by type name and rebuilt an object holding a disposed stream.
  • ActionCacheFilter / ActionCacheEndpointFilter and both abstract factories take a CachedResponseFactory.

Verification

Unit 517/517, integration 49/49, both on net8.0 and net10.0. Release build 0 warnings.

https://claude.ai/code/session_01VUPTqPh5w79mfa18fLnMLz

Cached values were IActionResult object graphs serialized by Newtonsoft with
TypeNameHandling.Auto, so a cache entry named the types deserialization should
construct and the only defense was a deny-list of five gadget namespaces.
Deny-lists lose.

Values are now a CachedResponse of primitives — status code, content type,
already-rendered body — serialized with System.Text.Json through a
source-generated context. There is no $type and no type resolution, so the
vulnerability class is removed rather than filtered, and the hot path is
trim/AOT friendly. A regression test asserts no type discriminator survives
serialization.

Bodies are rendered with the application's own JsonSerializerOptions, so a
cached body is byte-identical to what the action would have written; a test
proves the app's camelCase policy is honored rather than serializer defaults.
JsonResult is handled explicitly — it is not an ObjectResult, and treating it
generically would have silently stopped caching a result IsCacheableResult
accepts.

Scope note: cache *keys* still use Newtonsoft, now behind KeyComponentSerializer.
Reflection-based refresh reverses a key into typed action arguments and needs
the type information to do it, so the binder stays there until 4b replays real
requests and deletes the reverse path. #5 is not fully closed until then.

BREAKING: existing cache entries are unreadable — expect a cold cache on deploy.
BREAKING: results that do work at execution time (FileStreamResult,
RedirectResult) are no longer cacheable. The previous code "cached" a file
result by type name and rebuilt an object holding a disposed stream.
BREAKING: filter and abstract-factory constructors take a CachedResponseFactory.

Claude-Session: https://claude.ai/code/session_01VUPTqPh5w79mfa18fLnMLz
@jzills
jzills force-pushed the fix/batch-4a-response-envelope branch from 1bd57c1 to a95a8ee Compare August 23, 2026 03:20
@jzills
jzills changed the base branch from fix/batch-5-layering-api-surface to develop August 23, 2026 03:20
@jzills
jzills merged commit 7bdff5c into develop Aug 23, 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