Skip to content

Properly validate segments and cap cache of pathDescriptors - #69186

Merged
DeagleGross merged 1 commit into
mainfrom
wtgodbe/Port62624
Sep 10, 2026
Merged

DeagleGross merged 1 commit into
mainfrom
wtgodbe/Port62624

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Sep 9, 2026

Copy link
Copy Markdown
Member

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.

…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.
@wtgodbe
wtgodbe requested review from DeagleGross and a lite review from Copilot September 9, 2026 23:26
@wtgodbe
wtgodbe requested a review from SamMonoRT as a code owner September 9, 2026 23:26
@github-actions github-actions Bot added the area-grpc Includes: GRPC wire-up, templates label Sep 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 FieldDescriptor identity (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.

Comment on lines 46 to +56
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);
}
Comment on lines 291 to 295
if (CanBindQueryStringVariable(serverCallContext, item.Key))
{
var pathDescriptors = GetPathDescriptors(serverCallContext, requestMessage, item.Key);

if (pathDescriptors != null)
if (pathDescriptors != null && !ConflictsWithRouteParameter(serverCallContext, pathDescriptors))
{
@wtgodbe wtgodbe changed the title properly validate segments and cap cache of pathDescriptors Properly validate segments and cap cache of pathDescriptors Sep 9, 2026
@DeagleGross
DeagleGross merged commit 9334d07 into main Sep 10, 2026
29 checks passed
@DeagleGross
DeagleGross deleted the wtgodbe/Port62624 branch September 10, 2026 08:31
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-grpc Includes: GRPC wire-up, templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants