TLS certificate validation is switched off by a callback that accepts every certificate

tx0/csharp:certificate-validation-disabledrecommendederrorsecurity/cryptodepth fastsecurity, cwe-295

What it finds

A validation callback that returns true whatever it is given accepts any certificate, so anyone between the client and the server can read and change the traffic. The callbacks checked are ServerCertificateValidationCallback, ServerCertificateCustomValidationCallback and RemoteCertificateValidationCallback assigned a lambda that returns the literal true, and DangerousAcceptAnyServerCertificateValidator.

From the rule's source, certificate_validation_disabled.rs.

Example from the rule's tests
using System.Net;using System.Net.Http; class Client{    HttpClient Make()    {        ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, e) => true;tx0 reports: this callback accepts every certificate        var handler = new HttpClientHandler();        handler.ServerCertificateCustomValidationCallback = delegate { return true; };tx0 reports: this callback accepts every certificate        handler.ServerCertificateCustomValidationCallback = (m, c, ch, e) => e == System.Net.Security.SslPolicyErrors.None;        return new HttpClient(handler);    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:certificate-validation-disabled" = "error"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:certificate-validation-disabled] reason="..."

tx0 explain tx0/csharp:certificate-validation-disabled shows the same in the terminal. Found a false positive? Report it.