Skip to content

Commit 894f836

Browse files
fix(security): replace 2 generic catches in NodeRegistrationService
Added HttpRequestException, TaskCanceledException, JsonException handlers Refs: CodeQL cs/catch-of-all-exceptions
1 parent f71c0cc commit 894f836

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/Node.Runtime/Services/NodeRegistrationService.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,24 @@ public async Task<bool> RegisterNodeAsync(CancellationToken cancellationToken =
7878
_options.NodeId, response.StatusCode, errorContent);
7979
return false;
8080
}
81+
catch (HttpRequestException httpEx)
82+
{
83+
_logger.LogError(httpEx, "HTTP error registering node {NodeId}: {Message}", _options.NodeId, httpEx.Message);
84+
return false;
85+
}
86+
catch (TaskCanceledException)
87+
{
88+
_logger.LogWarning("Registration request timeout for node {NodeId}", _options.NodeId);
89+
return false;
90+
}
91+
catch (System.Text.Json.JsonException jsonEx)
92+
{
93+
_logger.LogError(jsonEx, "JSON serialization error registering node {NodeId}", _options.NodeId);
94+
return false;
95+
}
8196
catch (Exception ex)
8297
{
83-
_logger.LogError(ex, "Exception occurred while registering node {NodeId}", _options.NodeId);
98+
_logger.LogError(ex, "Unexpected error registering node {NodeId}", _options.NodeId);
8499
return false;
85100
}
86101
}
@@ -115,9 +130,24 @@ public async Task<bool> SendHeartbeatAsync(int activeRuns, int availableSlots, C
115130
_options.NodeId, response.StatusCode, errorContent);
116131
return false;
117132
}
133+
catch (HttpRequestException httpEx)
134+
{
135+
_logger.LogWarning(httpEx, "HTTP error sending heartbeat for node {NodeId}: {Message}", _options.NodeId, httpEx.Message);
136+
return false;
137+
}
138+
catch (TaskCanceledException)
139+
{
140+
_logger.LogDebug("Heartbeat request timeout for node {NodeId}", _options.NodeId);
141+
return false;
142+
}
143+
catch (System.Text.Json.JsonException jsonEx)
144+
{
145+
_logger.LogWarning(jsonEx, "JSON serialization error sending heartbeat for node {NodeId}", _options.NodeId);
146+
return false;
147+
}
118148
catch (Exception ex)
119149
{
120-
_logger.LogWarning(ex, "Exception occurred while sending heartbeat for node {NodeId}", _options.NodeId);
150+
_logger.LogWarning(ex, "Unexpected error sending heartbeat for node {NodeId}", _options.NodeId);
121151
return false;
122152
}
123153
}

0 commit comments

Comments
 (0)