Repository navigation
[C#][httpclient] Honor Configuration.RemoteCertificateValidationCallback in ApiClient - #25832
nagabalaji-b wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
1 issue found across 6 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache:517">
P1: This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| { | ||
| if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); | ||
| // HttpClientHandler properties are frozen after the first request, so only assign when not already set. | ||
| if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) |
There was a problem hiding this comment.
P1: This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache, line 517:
<comment>This lazy assignment can throw after the handler has already sent a request. Configure the callback before the first request, or create a stable handler callback during client construction instead of attempting to mutate the handler on every request.</comment>
<file context>
@@ -510,6 +510,18 @@ namespace {{packageName}}.Client
+ {
+ if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
+ // HttpClientHandler properties are frozen after the first request, so only assign when not already set.
+ if (_httpClientHandler.ServerCertificateCustomValidationCallback == null)
+ {
+ _httpClientHandler.ServerCertificateCustomValidationCallback =
</file context>
There was a problem hiding this comment.
1 issue found across 6 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache:533">
P2: The stored callback is never cleared, so a request whose configuration has no `RemoteCertificateValidationCallback` still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| #else | ||
| // Refresh the callback on every request; the delegate installed below reads this field, | ||
| // so a later request (or a replaced Configuration) is not stuck with the first callback. | ||
| _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; |
There was a problem hiding this comment.
P2: The stored callback is never cleared, so a request whose configuration has no RemoteCertificateValidationCallback still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache, line 533:
<comment>The stored callback is never cleared, so a request whose configuration has no `RemoteCertificateValidationCallback` still runs the callback from an earlier request. Also, a request using a different configuration overwrites the field that in-flight requests read. Clear the field when the configured callback is null, or document that the callback is sticky for the lifetime of the client.</comment>
<file context>
@@ -513,13 +524,39 @@ namespace {{packageName}}.Client
+#else
+ // Refresh the callback on every request; the delegate installed below reads this field,
+ // so a later request (or a replaced Configuration) is not stuck with the first callback.
+ _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback;
+ // Install the handler delegate only once. HttpClientHandler properties are frozen after the
+ // first request, so the callback must be configured before this client sends its first request
</file context>
The httpclient ApiClient accepted Configuration.RemoteCertificateValidationCallback but never applied it, so the setting was silently ignored. The callback is now assigned to the HttpClientHandler, with the following behavior: - Stored in a field refreshed on each request. If a later request has no callback, the stored one is cleared, so default validation applies. - Installed once; a callback set on a consumer-provided handler is not overwritten. - Throws a clear InvalidOperationException if the handler is already frozen. - Not supported on net47 (ServerCertificateCustomValidationCallback is unavailable there). The handler is shared by all requests of a client, so the most recent request's configuration wins. No change when the callback is not set. Regenerated httpclient samples.
…tpclient-remote-cert-callback
Summary
The C#
httpclientlibrary now appliesConfiguration.RemoteCertificateValidationCallbackto theHttpClientHandlerit uses, the same way it already appliesProxyandClientCertificates.Problem
Configuration.RemoteCertificateValidationCallbackis a public property shared by all C# libraries, and the RestSharp-based client honors it. Thehttpclientlibrary accepted the setting but never used it, so it was silently ignored.HttpClientandHttpClientHandlertoApiClient(HttpClient, HttpClientHandler), they can setServerCertificateCustomValidationCallbackon that handler themselves.new ApiClient(basePath)or an API class built from aConfiguration, the client creates its own handler internally. There was no way to apply a certificate validation callback, and setting it onConfigurationdid nothing.For code that relies on
Configuration.RemoteCertificateValidationCallback, for example when moving from the RestSharp client tohttpclient, certificate validation therefore silently stopped working.Change
In
libraries/httpclient/ApiClient.mustache, whenConfiguration.RemoteCertificateValidationCallbackis non-null:HttpClientHandler.ServerCertificateCustomValidationCallback;InvalidOperationExceptionis thrown if the client was created without anHttpClientHandler, consistent with the existingProxyandClientCertificateshandling.Nothing changes when the property is not set, so existing consumers are unaffected.
Usage
var config = new Configuration
{
BasePath = "https://localhost:5001",
RemoteCertificateValidationCallback = (request, certificate, chain, errors) => true // development only
};
var api = new PetApi(config);
Files
modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustachesamples/client/petstore/csharp/httpclient/**/ApiClient.csPR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Summary by cubic
The C#
httpclientlibrary now appliesConfiguration.RemoteCertificateValidationCallbackto its internalHttpClientHandler, so certificate validation no longer silently stops working when migrating from the RestSharp client.Configurationis honored; a cleared callback falls back to default validation.Interlockedguard so the handler callback is installed exactly once, and reads a volatile field so later callback changes stay effective.InvalidOperationExceptionwhen the client was created without anHttpClientHandler, aNotSupportedExceptionwhen targeting .NET Framework 4.7, and a clearInvalidOperationExceptionwhen the handler has already sent requests and its properties are frozen.ServerCertificateCustomValidationCallbackis never overwritten.Written for commit 8c5109e. Summary will update on new commits.