Skip to content

Backport #67591 to .NET 10: RDG binds a [FromHeader] parameter from the query string when another endpoint has a parameter of the same name and type #69676

Open

Description

@AdCodicem

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

#66912 reports the same root cause for [FromBody(EmptyBodyBehavior = Allow)]. #67591 fixed it for 11.0; this asks for that fix in 10.0 servicing.

With EnableRequestDelegateGenerator (on by default under PublishAot and PublishTrimmed), the generator in 10.0 gives one interceptor to minimal API handlers with the same HTTP method and return type whose parameters have the same types and names, even when their binding attributes differ. The second endpoint then binds its parameters as the first one does:

  • MapGet("/query", (Guid id) => ...) and MapGet("/header", ([FromHeader(Name = "X-Id")] Guid id) => ...): /header reads id from the route or the query string. A request carrying the X-Id header gets 400, and /header?id=... without the header gets 200.
  • ([FromHeader(Name = "X-A")] string value) and ([FromHeader(Name = "X-B")] string value): /header-b reads the X-A header.

The build reports no warning or diagnostic, and startup no error. With the generator off, every endpoint binds as written.

On release/10.0 (55d77e1), endpoints are grouped with Endpoint.SignatureEquals (Endpoint.cs#L116-L134), which compares parameters with EndpointParameter.SignatureEquals: type, symbol name and keyed-service key only (EndpointParameter.cs#L604-L611). #67591 replaced it on main with EndpointParameter.Equals, which also compares the binding source, the lookup name, the ordinal and optionality, and added MapAction_ExplicitHeaderParam_SharedDelegateSignature_PreservesBindingName, which covers the header-name case above. On 11.0.0-rc.1 every endpoint of the repro binds correctly.

For the servicing decision:

  • Impact: in an app that enables the generator (every app published with PublishAot or PublishTrimmed) and has two such handlers, the second binds from another source or under another name, with no diagnostic. 10.0 is an LTS release.
  • Regression: no. release/8.0 and release/9.0 have the same comparison.
  • Risk: low. The change (8 lines added and 11 removed in two files, whose context matches release/10.0) and its regression tests have been on main since 11.0-preview7 and ship in 11.0 RC1.
  • Workaround: give the parameters of such handlers different names.

Expected Behavior

Each endpoint binds its parameters as its own attributes say, with the generator on or off, as on 11.0.0-rc.1.

Steps To Reproduce

Repro project: https://github.com/AdCodicem/dotnet-upstream-repros/tree/main/aspnetcore-rdg-shared-interceptor. ./repro.sh builds and runs it with the generator off, then on; REPRO_TFM=net11.0 ./repro.sh does the same on .NET 11.

Repro.csproj (the repository's version takes the target framework from REPRO_TFM; net10.0 is its default):

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
  </PropertyGroup>

</Project>

Program.cs:

using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateSlimBuilder(args);
builder.Logging.ClearProviders();
builder.WebHost.UseUrls("http://127.0.0.1:0");
var app = builder.Build();

// Same parameter type and name; only the binding attributes differ.
app.MapGet("/query", (Guid id) => $"query {id}");
app.MapGet("/header", ([FromHeader(Name = "X-Id")] Guid id) => $"header {id}");

// Same source, different header names.
app.MapGet("/header-a", ([FromHeader(Name = "X-A")] string value) => $"X-A {value}");
app.MapGet("/header-b", ([FromHeader(Name = "X-B")] string value) => $"X-B {value}");

await app.StartAsync();
using var client = new HttpClient { BaseAddress = new Uri(app.Urls.First()) };

var id = Guid.NewGuid();
await Send("/query?id=" + id, null);
await Send("/header", ("X-Id", id.ToString()));
await Send("/header-a", ("X-A", "a"));
await Send("/header-b", ("X-B", "b"));

// What the second endpoint of each pair actually binds.
await Send("/header?id=" + id, null);
await Send("/header-b", ("X-A", "a"));

await app.StopAsync();

async Task Send(string path, (string Name, string Value)? header)
{
    using var request = new HttpRequestMessage(HttpMethod.Get, path);
    if (header is { } h) request.Headers.Add(h.Name, h.Value);
    using var response = await client.SendAsync(request);
    var body = await response.Content.ReadAsStringAsync();
    Console.WriteLine($"GET {path,-48} {(header is { } x ? x.Name + ": " + x.Value : ""),-44} -> {(int)response.StatusCode} {body}");
}

Output on SDK 10.0.401:

== EnableRequestDelegateGenerator=false
GET /query?id=5f4bfee9-a79e-4040-9c7c-12eef7409de0                                                -> 200 query 5f4bfee9-a79e-4040-9c7c-12eef7409de0
GET /header                                          X-Id: 5f4bfee9-a79e-4040-9c7c-12eef7409de0   -> 200 header 5f4bfee9-a79e-4040-9c7c-12eef7409de0
GET /header-a                                        X-A: a                                       -> 200 X-A a
GET /header-b                                        X-B: b                                       -> 200 X-B b
GET /header?id=5f4bfee9-a79e-4040-9c7c-12eef7409de0                                               -> 400
GET /header-b                                        X-A: a                                       -> 400

== EnableRequestDelegateGenerator=true
GET /query?id=83c65e51-e418-4371-a2fb-6dc95b51a845                                                -> 200 query 83c65e51-e418-4371-a2fb-6dc95b51a845
GET /header                                          X-Id: 83c65e51-e418-4371-a2fb-6dc95b51a845   -> 400
GET /header-a                                        X-A: a                                       -> 200 X-A a
GET /header-b                                        X-B: b                                       -> 400
GET /header?id=83c65e51-e418-4371-a2fb-6dc95b51a845                                               -> 200 header 83c65e51-e418-4371-a2fb-6dc95b51a845
GET /header-b                                        X-A: a                                       -> 200 X-B a

On SDK 11.0.100-rc.1.26425.128 (net11.0), the generator-on run prints the same results as the control.

Exceptions (if any)

None; the wrong binding is silent.

.NET Version

10.0.401

Anything else?

Found while adding native AOT checks to AdCodicem.ValueObjects. Investigation and write-up assisted by Claude (Anthropic); the linked repro was run on Windows 11 and Ubuntu, and I reviewed this report.

Activity

  1. earbona23 commented on Nov 1, 2026

    @earbona23
    Contributor

    I ran into this from a different direction and I think the impact is wider than the issue currently records, so I am adding what I measured in case it helps the triage.

    The collapse is not limited to picking the wrong parameter source. When the endpoint that loses the collision is a form endpoint, it also loses the antiforgery metadata, because AntiforgeryMetadata.ValidationRequired is emitted from inside the per-interceptor emission in StaticRouteHandlerModel.Emitter.cs. The endpoint stops being treated as a form endpoint at all, so it is never marked as requiring a token.

    Minimal repro, AddAntiforgery() and UseAntiforgery() wired up, nothing else unusual:

    app.MapPost("/a-plain/{v}", (string v) => Results.Text("plain v=" + v));
    app.MapPost("/b-form", ([FromForm] string v) => Results.Text("FORM HANDLER RAN, v=" + v));

    Reading Endpoint.Metadata.GetMetadata<IAntiforgeryMetadata>() for /b-form from inside the running app:

    build metadata on /b-form POST /b-form?v=x, no token
    default (runtime generation) RequiresValidation = true 400, handler does not run
    <PublishAot>true</PublishAot> no antiforgery metadata at all 200, handler runs

    Same result on 8.0.33, 9.0.22 and 10.0.14. <PublishAot>true</PublishAot> on its own is enough, which is what dotnet new webapiaot puts in the project file, so no extra opt-in is involved. The generated code for the collided endpoint shows the inherited source directly:

    // Endpoint Parameter: v (Type = string, ..., Source = RouteOrQuery)
    var v_raw = v_RouteOrQueryResolver(httpContext);
    

    while an uncollided [FromForm] endpoint in the same file gets Source = FormBody and the AntiforgeryMetadata.ValidationRequired line.

    Scope of the collision, measured rather than assumed:

    • the Map* verb helper is part of the key. MapGet("/plain", (string v)) before MapPost("/form", ([FromForm] string v)) does not collide.
    • the parameter names are part of the key. Renaming the parameter in one handler avoids it.
    • parameter types and the return type are also part of it. ([FromForm] int n) does not collide with (string v), and changing the return type separates them.
    • the binding attribute is not part of the key, which is the defect.
    • declaration order decides the winner, and the two call sites do not have to be in the same file. I reproduced it with the predecessor in a separate .cs file, which is the case that worries me most, since the endpoint that breaks is not the one being edited.
    • it also goes the other way: a plain endpoint declared after a form endpoint gains an antiforgery requirement and a form binding it never asked for. POST /plain?v=hello goes from 200 to a failure.
    • DisableAntiforgery() does not leak between the two.

    One thing that limits this, which I want to state plainly: the collided endpoint is also broken for legitimate use. A real form post with a valid token to /b-form returns 400 in the same build, because the handler is no longer reading the form. So a developer who exercises their own form will notice something is wrong. The exposure is the leftover query-string path into a handler that was written to be CSRF protected, which matters most when the collision is introduced later by an unrelated endpoint added elsewhere in the solution.

    A separate thing I noticed while measuring this, which may deserve its own issue: in generator builds the correct rejection of a token-less form post is an unhandled exception rather than a 400. The generated TryResolveFormAsync touches the form without consulting IAntiforgeryValidationFeature, so FormFeature.HandleUncheckedAntiforgeryValidationFeature throws:

    System.InvalidOperationException: This form is being accessed with an invalid anti-forgery token.
    Validate the `IAntiforgeryValidationFeature` on the request before reading from the form.
       at Microsoft.AspNetCore.Http.Features.FormFeature.HandleUncheckedAntiforgeryValidationFeature()
       at Microsoft.AspNetCore.Http.Features.FormFeature.get_HasFormContentType()
    

    and the response is 500 where the runtime path logs RequestDelegateFactory[9] and returns 400. It is fail closed, the handler does not run, so it is a robustness and diagnosability problem rather than a security one. I mention it because it also makes this family of bugs easy to misread when testing: a check that only looks for "not 400" will score the safe cases as broken too.

    This is the same root cause you already describe, so I am not opening anything new. I am adding it here because this issue is the one asking for the 10.0 backport, and the antiforgery consequence seems like it belongs in that servicing argument: on 8.0, 9.0 and 10.0 the collapse can take an endpoint the developer wrote as a CSRF-protected form endpoint and leave it reachable without a token. Happy to put the repro in a gist or send a PR to the test suite if that would help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions