Analyzer: warn when SslCertificateTrust is set without a RemoteCertificateValidationCallback
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
On the raw callback path (`TlsHandshakeCallbackOptions.OnConnection` returning a `SslServerAuthenticationOptions`), setting `ServerCertificateContext` with an `SslCertificateTrust` looks like it restricts which client certs are accepted. It doesn't. `SslCertificateTrust` only fills the `certificate_authorities` list in the TLS `CertificateRequest` — it's a hint to the client, not a validation gate.
Without a `RemoteCertificateValidationCallback`, a client can ignore the advertised CAs and present any cert chaining to a system-trusted root. This bites hardest on HTTP/3, where the enforcement behavior of MsQuic differs from SslStream, so an app that "worked" on H1/H2 can silently accept unintended client certs over H3.
related https://github.com/dotnet/runtime/issues/73053
## Proposal
Add a Roslyn analyzer that flags the misconfiguration at build time.
Rule: if `SslCertificateTrust` is passed to `SslStreamCertificateContext.Create(...)` (or `ServerCertificateContext.Trust` is otherwise set) **and** `ClientCertificateRequired` is true, **and** no `RemoteCertificateValidationCallback` is assigned on the same `SslServerAuthenticationOptions`, emit a warning.
- Category: Security
- Severity: Warning
- Message: "A client-certificate trust list is set but no RemoteCertificateValidationCallback is configured. Add a validation callback that checks the client chain against your roots."
- Code fix: insert a `RemoteCertificateValidationCallback` stub that builds an `X509Chain` with `X509ChainTrustMode.CustomRootTrust`.
Contributor guide
Research direction
Start by locating the analyzer entry point for SslStreamCertificateContext.Create and SslServerAuthenticationOptions. Trace how ServerCertificateContext.Trust, ClientCertificateRequired, and RemoteCertificateValidationCallback are represented, then add the warning and code fix described; done means the specified misconfiguration is diagnosed with the stated message and a callback stub is offered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- security, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100