Skip to content

HCR062

Prefer per-request headers over mutating DefaultRequestHeaders.

Why

HttpClient.DefaultRequestHeaders applies to every request sent by that client. Mutating it for per-request data can leak tenant, user, authorization, correlation, or feature headers across requests when the client is reused.

Bad

client.DefaultRequestHeaders.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);

return client.GetAsync("/orders", cancellationToken);
client.DefaultRequestHeaders.Add("X-Tenant", tenantId);
return client.PostAsync("/orders", content, cancellationToken);

Better

using var request = new HttpRequestMessage(HttpMethod.Get, "/orders");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

return client.SendAsync(request, cancellationToken);
using var request = new HttpRequestMessage(HttpMethod.Post, "/orders")
{
    Content = content
};
request.Headers.Add("X-Tenant", tenantId);

return client.SendAsync(request, cancellationToken);

Current Detection

The implementation reports visible mutations under a real System.Net.Http.HttpClient.DefaultRequestHeaders receiver. It catches direct header mutations such as Add(...), TryAddWithoutValidation(...), Remove(...), Clear(...), and ParseAdd(...), nested collection mutations such as client.DefaultRequestHeaders.Accept.Add(...), and assignments such as client.DefaultRequestHeaders.Authorization = ....

The receiver and resolved mutator method are validated with Roslyn type information when available, with syntactic fallback for unresolved snippets that visibly declare an HttpClient receiver. Reads such as client.DefaultRequestHeaders.Contains(...) are skipped, request-scoped HttpRequestMessage.Headers mutations are skipped, and resolved custom HttpClient or header-extension lookalikes are skipped.

One-time configuration sites are skipped: mutations on the HttpClient parameter of an AddHttpClient/ConfigureHttpClient configure delegate, mutations on the injected client (parameter or instance member) inside the constructor of a type registered via AddHttpClient<T>, and mutations in a constructor where the receiver is an instance member of the containing type and that member is assigned a newly constructed HttpClient in the constructor or a field initializer. A static member configured in a static constructor is treated the same way. In those cases the type owns the client, so the header is set once for the client's lifetime. A helper method that creates a fresh client with new HttpClient(...) or IHttpClientFactory.CreateClient(), configures it, and returns that same local is also treated as one-time configuration because the client has not been published to concurrent requests yet. A local client created the same way and only used through member access in its method is treated as local configuration too; passing it to another method, storing it in a field, or reassigning it before the mutation keeps the diagnostic.

Mutating a client assigned from a constructor parameter, a static client from an instance constructor, or an owned client from a method other than its constructor is still reported. Test code is skipped: types ending in Test/Tests, types carrying common test attributes, types deriving from a test-named base type, types in a Test, Tests, Testing, *Test, or *Tests namespace, and files under a test, tests, testassets, testapps, or acceptance directory.

Known Limitations

  • A client returned by a factory delegate is treated as shared because the analyzer cannot prove freshness. _httpClientFactory() and HttpClientFactory() patterns are reported even when the delegate always returns a new client (Xabaril HealthChecks Consul and the Dapr generic client builder). If the delegate is documented to return a fresh client, suppress with that reason or create the client inline.
  • A client injected through a constructor parameter and mutated in the constructor is reported. The analyzer treats a constructor as one-time configuration only when it sees the instance member assigned a newly constructed client there, or the type is registered through AddHttpClient<T>().
  • Configuration moved into a helper on another type is not followed, even when the helper creates and owns the client.
  • Test-support libraries that do not look like test code can still report.

Suppression

Suppress only for process-wide static headers that are intentionally constant for the entire lifetime of the client, such as a fixed product header. Prefer setting those once during client construction or typed-client initialization.

References