Skip to content

Commit 0c959dc

Browse files
fix(security): replace 3 generic catches in LeaseServiceLogic
Added NpgsqlException, TimeoutException, InvalidOperationException handlers Refs: CodeQL cs/catch-of-all-exceptions
1 parent 6ec61c6 commit 0c959dc

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/ControlPlane.Api/Services/LeaseServiceLogic.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,21 @@ public LeaseServiceLogic(
130130
}
131131
}
132132
}
133+
catch (Npgsql.NpgsqlException npgEx)
134+
{
135+
_logger.LogError(npgEx, "Database error preparing leases for node {NodeId}", nodeId);
136+
}
137+
catch (TimeoutException timeoutEx)
138+
{
139+
_logger.LogWarning(timeoutEx, "Timeout preparing leases for node {NodeId}", nodeId);
140+
}
141+
catch (InvalidOperationException invalidOpEx)
142+
{
143+
_logger.LogError(invalidOpEx, "Invalid operation preparing leases for node {NodeId}", nodeId);
144+
}
133145
catch (Exception ex)
134146
{
135-
_logger.LogError(ex, "Error preparing leases for node {NodeId}", nodeId);
147+
_logger.LogError(ex, "Unexpected error preparing leases for node {NodeId}", nodeId);
136148
}
137149

138150
// Yield all prepared leases
@@ -227,9 +239,24 @@ public async Task<bool> CompleteRunAsync(
227239
_logger.LogInformation("Run {RunId} completed successfully", runId);
228240
return true;
229241
}
242+
catch (Npgsql.NpgsqlException npgEx)
243+
{
244+
_logger.LogError(npgEx, "Database error completing run {RunId}", runId);
245+
return false;
246+
}
247+
catch (TimeoutException timeoutEx)
248+
{
249+
_logger.LogWarning(timeoutEx, "Timeout completing run {RunId}", runId);
250+
return false;
251+
}
252+
catch (InvalidOperationException invalidOpEx)
253+
{
254+
_logger.LogError(invalidOpEx, "Invalid operation completing run {RunId}", runId);
255+
return false;
256+
}
230257
catch (Exception ex)
231258
{
232-
_logger.LogError(ex, "Error completing run {RunId}", runId);
259+
_logger.LogError(ex, "Unexpected error completing run {RunId}", runId);
233260
return false;
234261
}
235262
}
@@ -295,9 +322,24 @@ public async Task<bool> CompleteRunAsync(
295322
_logger.LogInformation("Run {RunId} marked as failed. Should retry: {ShouldRetry}", runId, shouldRetry);
296323
return (true, shouldRetry);
297324
}
325+
catch (Npgsql.NpgsqlException npgEx)
326+
{
327+
_logger.LogError(npgEx, "Database error processing failure for run {RunId}", runId);
328+
return (false, false);
329+
}
330+
catch (TimeoutException timeoutEx)
331+
{
332+
_logger.LogWarning(timeoutEx, "Timeout processing failure for run {RunId}", runId);
333+
return (false, false);
334+
}
335+
catch (InvalidOperationException invalidOpEx)
336+
{
337+
_logger.LogError(invalidOpEx, "Invalid operation processing failure for run {RunId}", runId);
338+
return (false, false);
339+
}
298340
catch (Exception ex)
299341
{
300-
_logger.LogError(ex, "Error processing failure for run {RunId}", runId);
342+
_logger.LogError(ex, "Unexpected error processing failure for run {RunId}", runId);
301343
return (false, false);
302344
}
303345
}

0 commit comments

Comments
 (0)