Repository navigation
Properly validate segments and cap cache of pathDescriptors - #69186
Conversation
…and cap cache of pathDescriptors Fixing 2 problems: 1) A query-string parameter written with a mixed or shortened field-name spelling (e.g. `?tenantScope.tenant_id=…` or a parent like `?timestamp_value=…`) could bypass the existing name check and overwrite a value already bound from the URL path. This change compares the resolved field identities instead of raw name strings, so query parameters can no longer clobber route-bound fields regardless of how they're spelled. 2) A transcoded method whose request has a self-referential message field allows an unbounded set of valid dotted paths, so the per-endpoint path descriptor cache could grow without limit from repeated requests. Cap the cache size (default 1000, overridable via AppContext) to prevent it.
There was a problem hiding this comment.
🟡 Changes recommended
The new cache-cap implementation relies on ConcurrentDictionary.Count (O(n)) and non-atomic checks, and conflicting query paths can still consume cache entries before being rejected.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR hardens gRPC JSON transcoding request binding by preventing query-string parameters (including mixed proto/JSON spellings and ancestor paths) from overwriting values already bound from route parameters, and by bounding the per-endpoint dotted-path descriptor cache to avoid unbounded growth (configurable via AppContext, default 1000).
Changes:
- Detect query-vs-route collisions by comparing resolved
FieldDescriptoridentity (including ancestor/descendant overlap), not raw string keys. - Add a bounded path-descriptor cache with a default max size and AppContext override, plus tests to validate capped behavior and continued binding when at capacity.
- Extend test protos and unit tests to cover mixed-name nested route collisions and recursive message path growth.
File summaries
| File | Description |
|---|---|
| src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonRequestHelpers.cs | Blocks query parameters that resolve to the same (or ancestor) route-bound field by comparing descriptor paths. |
| src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/CallHandlers/CallHandlerDescriptorInfo.cs | Introduces a max-size cap for the path descriptor cache (default 1000, overridable via AppContext). |
| src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/UnaryServerCallHandlerTests.cs | Adds tests for cache bounding, binding behavior at capacity, and mixed-name/ancestor collision scenarios. |
| src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/Proto/transcoding.proto | Adds proto fields/messages needed to exercise mixed-name nested routing and recursive-path caching tests. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| public ConcurrentDictionary<string, List<FieldDescriptor>> PathDescriptorsCache { get; } | ||
|
|
||
| public bool TryAddPathDescriptors(string path, List<FieldDescriptor> pathDescriptors) | ||
| { | ||
| if (PathDescriptorsCache.Count >= MaxPathDescriptorsCacheCount) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return PathDescriptorsCache.TryAdd(path, pathDescriptors); | ||
| } |
| if (CanBindQueryStringVariable(serverCallContext, item.Key)) | ||
| { | ||
| var pathDescriptors = GetPathDescriptors(serverCallContext, requestMessage, item.Key); | ||
|
|
||
| if (pathDescriptors != null) | ||
| if (pathDescriptors != null && !ConflictsWithRouteParameter(serverCallContext, pathDescriptors)) | ||
| { |
Fixing 2 problems:
A query-string parameter written with a mixed or shortened field-name spelling (e.g.
?tenantScope.tenant_id=…or a parent like?timestamp_value=…) could bypass the existing name check and overwrite a value already bound from the URL path. This change compares the resolved field identities instead of raw name strings, so query parameters can no longer clobber route-bound fields regardless of how they're spelled.A transcoded method whose request has a self-referential message field allows an unbounded set of valid dotted paths, so the per-endpoint path descriptor cache could grow without limit from repeated requests. Cap the cache size (default 1000, overridable via AppContext) to prevent it.