Skip to content

Commit f71c0cc

Browse files
fix(security): replace 2 generic catches in Node.Runtime/Program.cs
Added FileNotFoundException, UnauthorizedAccessException, CryptographicException handlers for certificate loading Refs: CodeQL cs/catch-of-all-exceptions
1 parent cbc62cb commit f71c0cc

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/Node.Runtime/Program.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
// Configure gRPC client for LeaseService with mTLS support
3434
builder.Services.AddSingleton(sp =>
3535
{
36-
var logger = sp.GetRequiredService<ILogger<Program>>();
37-
38-
GrpcChannel channel;
39-
36+
var logger = sp.GetRequiredService<ILogger<Program>>();
37+
38+
GrpcChannel channel;
39+
4040
if (mtlsConfig.Enabled)
4141
{
4242
logger.LogInformation("mTLS is enabled for gRPC client connections");
@@ -61,6 +61,18 @@
6161
clientCertificate = X509Certificate2.CreateFromPem(certPem, keyPem);
6262
logger.LogInformation("Loaded client certificate from {CertPath}", mtlsConfig.ClientCertificatePath);
6363
}
64+
catch (FileNotFoundException fnfEx)
65+
{
66+
throw new InvalidOperationException($"Client certificate file not found: {mtlsConfig.ClientCertificatePath}", fnfEx);
67+
}
68+
catch (UnauthorizedAccessException uaEx)
69+
{
70+
throw new InvalidOperationException($"Access denied reading client certificate: {mtlsConfig.ClientCertificatePath}", uaEx);
71+
}
72+
catch (System.Security.Cryptography.CryptographicException cryptoEx)
73+
{
74+
throw new InvalidOperationException($"Invalid certificate format in {mtlsConfig.ClientCertificatePath}", cryptoEx);
75+
}
6476
catch (Exception ex)
6577
{
6678
throw new InvalidOperationException($"Failed to load client certificate from {mtlsConfig.ClientCertificatePath}", ex);
@@ -76,6 +88,18 @@
7688
serverCaCertificate = X509Certificate2.CreateFromPem(caPem);
7789
logger.LogInformation("Loaded server CA certificate from {CertPath}", mtlsConfig.ServerCaCertificatePath);
7890
}
91+
catch (FileNotFoundException fnfEx)
92+
{
93+
throw new InvalidOperationException($"Server CA certificate file not found: {mtlsConfig.ServerCaCertificatePath}", fnfEx);
94+
}
95+
catch (UnauthorizedAccessException uaEx)
96+
{
97+
throw new InvalidOperationException($"Access denied reading server CA certificate: {mtlsConfig.ServerCaCertificatePath}", uaEx);
98+
}
99+
catch (System.Security.Cryptography.CryptographicException cryptoEx)
100+
{
101+
throw new InvalidOperationException($"Invalid CA certificate format in {mtlsConfig.ServerCaCertificatePath}", cryptoEx);
102+
}
79103
catch (Exception ex)
80104
{
81105
throw new InvalidOperationException($"Failed to load server CA certificate from {mtlsConfig.ServerCaCertificatePath}", ex);
@@ -285,13 +309,13 @@
285309
{
286310
var metricsService = host.Services.GetRequiredService<INodeMetricsService>();
287311

288-
Node.Runtime.Observability.TelemetryConfig.ActiveLeasesGauge =
312+
Node.Runtime.Observability.TelemetryConfig.ActiveLeasesGauge =
289313
Node.Runtime.Observability.TelemetryConfig.Meter.CreateObservableGauge(
290314
"active_leases",
291315
() => metricsService.GetActiveLeases(),
292316
description: "Current number of active leases being processed");
293317

294-
Node.Runtime.Observability.TelemetryConfig.AvailableSlotsGauge =
318+
Node.Runtime.Observability.TelemetryConfig.AvailableSlotsGauge =
295319
Node.Runtime.Observability.TelemetryConfig.Meter.CreateObservableGauge(
296320
"available_slots",
297321
() => metricsService.GetAvailableSlots(),

0 commit comments

Comments
 (0)