Missing Authentication in Aspnet with Mutual Tls
Missing Authentication in Aspnet with Mutual Tls — how this specific combination creates or exposes the vulnerability
Mutual Transport Layer Security (mTLS) in an ASP.NET application requires both the client and the server to present and validate digital certificates. When authentication is missing or misconfigured in this setup, the protection that mTLS is intended to provide can be bypassed, leading to unauthorized access. middleBrick detects this category as part of its Authentication check, identifying cases where an endpoint accepts requests without properly enforcing client certificate validation or where the server-side logic does not require a certificate even though mTLS is advertised.
In ASP.NET, missing authentication in an mTLS context can occur when the server configuration or code path does not enforce client certificate validation. For example, an endpoint might rely on a default behavior that does not require a client cert, or the application may fall back to another authentication mechanism (such as cookies or tokens) without ensuring the mTLS requirement is satisfied. middleBrick tests the unauthenticated attack surface, so if an endpoint responds successfully to a request without presenting a valid client certificate, this is flagged as a finding. Attack patterns such as BOLA/IDOR and BFLA/Privilege Escalation can be chained with missing mTLS authentication to gain access to other users’ resources or perform actions beyond the intended scope.
Beyond the absence of enforcement, missing authentication can also stem from incorrect certificate trust validation. If an ASP.NET application validates that a client certificate is present but does not verify the certificate chain against a trusted root or does not check revocation, an attacker could supply a self-signed or stolen certificate that passes the presence check but is not trusted. middleBrick’s SSL/TLS and Authentication checks surface these gaps by probing endpoints without credentials and inspecting whether the server rejects requests with missing or invalid client certificates.
Consider an API endpoint designed to be mTLS-protected but implemented with incomplete validation logic in ASP.NET Core:
// Incomplete mTLS enforcement in ASP.NET Core (vulnerable)
app.Use(async (context, next) =>
{
// This check only ensures a certificate was presented, not that it is valid or trusted
if (context.Connection.ClientCertificate == null)
{
context.Response.StatusCode = 403;
return;
}
await next.Invoke();
});
In this example, the presence of a client certificate is verified, but there is no validation of the certificate’s signature, issuer, or revocation status. An attacker could present any certificate, and the request would be processed. middleBrick flags such findings under the Authentication and Property Authorization checks, emphasizing the need to enforce full chain validation and proper policy configuration.
When combined with other unchecked attack surfaces, missing authentication in mTLS-enabled endpoints can significantly increase risk. middleBrick’s 12 security checks run in parallel to identify these issues across Authentication, Data Exposure, and Encryption categories, providing prioritized findings and remediation guidance to help teams tighten their ASP.NET mTLS implementations.
Mutual Tls-Specific Remediation in Aspnet — concrete code fixes
To remediate missing authentication in an ASP.NET application using mTLS, you must enforce client certificate validation and ensure the server only accepts requests with valid, trusted certificates. The following examples demonstrate secure configurations and code practices for ASP.NET Core that align with the checks performed by middleBrick.
1) Enforce client certificates in Kestrel configuration:
// Program.cs — enforce client certificates
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
httpsOptions.ClientCertificateValidation = (cert, chain, errors) =>
{
// Validate the certificate chain and policy
if (cert is null) return false;
chain ??= new X509Chain();
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
chain.ChainPolicy.ExtraStore.Add(new X509Certificate2("path/to/trusted-root.cer"));
bool isValid = chain.Build(cert);
if (!isValid) return false;
// Optional: additional checks, e.g., thumbprint or subject
return true;
};
});
});
var app = builder.Build();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
This configuration requires a client certificate for every HTTPS request and validates the certificate chain against a trusted root, including revocation checks. middleBrick’s Authentication and Encryption checks validate that such enforcement is present and correctly configured.
2) Policy-based validation using authentication schemes:
// Program.cs — policy-based mTLS validation
builder.Services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
.AddCertificate(options =>
{
options.AllowedCertificateTypes = CertificateTypes.All;
options.RevocationMode = X509RevocationMode.Online;
options.ValidationFlags = X509VerificationFlags.NoFlag;
options.CustomIssuerStoreName = "My";
options.CustomIssuerStoreLocation = StoreLocation.LocalMachine;
options.Events = new CertificateAuthenticationEvents
{
OnCertificateValidated = context =>
{
// Additional custom validation, e.g., mapping to users or roles
var cert = context.ClientCertificate;
if (cert.Thumbprint == "EXPECTED_THUMBPRINT")
{
var claims = new[] { new Claim(ClaimTypes.Name, cert.Subject) };
context.Principal = new ClaimsPrincipal(new ClaimsIdentity(claims, context.Scheme.Name));
}
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers().RequireAuthorization();
app.Run();
This approach integrates certificate validation with ASP.NET Core’s authentication and authorization pipeline. It ensures that only certificates issued by a trusted CA and meeting custom criteria are accepted. middleBrick’s checks verify that such policies exist and are applied to endpoints, reducing the risk of missing authentication.
3) Ensuring secure defaults and rejecting unauthenticated requests:
// Middleware to reject requests without a valid client certificate (defense in depth)
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
context.Response.StatusCode = 401;
await context.Response.WriteAsync("Client certificate required.");
return;
}
await next();
});
Use such middleware only as a safety layer; the primary enforcement should be through Kestrel and authentication policies as shown above. middleBrick scans for cases where authentication is missing despite mTLS being expected and highlights the need for these validations.
By implementing these configurations and validating the full certificate chain, you address the missing authentication risk identified by middleBrick. The scanner’s Authentication, Encryption, and Property Authorization checks confirm that mTLS is enforced and that endpoints do not accept unauthenticated or improperly authenticated requests.
Related CWEs: authentication
| CWE ID | Name | Severity |
|---|---|---|
| CWE-287 | Improper Authentication | CRITICAL |
| CWE-306 | Missing Authentication for Critical Function | CRITICAL |
| CWE-307 | Brute Force | HIGH |
| CWE-308 | Single-Factor Authentication | MEDIUM |
| CWE-309 | Use of Password System for Primary Authentication | MEDIUM |
| CWE-347 | Improper Verification of Cryptographic Signature | HIGH |
| CWE-384 | Session Fixation | HIGH |
| CWE-521 | Weak Password Requirements | MEDIUM |
| CWE-613 | Insufficient Session Expiration | MEDIUM |
| CWE-640 | Weak Password Recovery | HIGH |