-
Notifications
You must be signed in to change notification settings - Fork 0
CodeQL 6: refactor: catch specific exceptions across CTS/RAPS/shared #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||||||||||||||
| using Microsoft.AspNetCore.Mvc; | ||||||||||||||||||
| using Microsoft.Data.SqlClient; | ||||||||||||||||||
| using Microsoft.EntityFrameworkCore; | ||||||||||||||||||
| using Viper.Classes; | ||||||||||||||||||
| using Viper.Classes.SQLContext; | ||||||||||||||||||
|
|
@@ -105,7 +106,7 @@ public async Task<ActionResult<Epa>> GetEpa(int epaId) | |||||||||||||||||
| context.Entry(epa).State = EntityState.Deleted; | ||||||||||||||||||
| await context.SaveChangesAsync(); | ||||||||||||||||||
| } | ||||||||||||||||||
| catch (Exception ex) | ||||||||||||||||||
| catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException) | ||||||||||||||||||
| { | ||||||||||||||||||
| return BadRequest(ex.Message); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+109
to
112
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not expose raw exception messages in API responses.
Suggested fix- return BadRequest(ex.Message);
+ return BadRequest("Could not delete EPA. It may be linked to other objects.");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,7 @@ static private bool IsValidEmail(string email) | |
| var addr = new MailAddress(email); | ||
| return addr.Address == trimmed; | ||
| } | ||
| catch | ||
| catch (Exception ex) when (ex is FormatException or ArgumentException) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Use explicit exception catches for email validation. Line 98 should be split into As per coding guidelines, " 🤖 Prompt for AI Agents |
||
| { | ||
| return false; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |||||||||||||||||
| using Viper.Classes.Utilities; | ||||||||||||||||||
| using Viper.Models.RAPS; | ||||||||||||||||||
| using Web.Authorization; | ||||||||||||||||||
| using Microsoft.Data.SqlClient; | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace Viper.Areas.RAPS.Controllers | ||||||||||||||||||
| { | ||||||||||||||||||
|
|
@@ -113,7 +114,7 @@ public async Task<ActionResult<OuGroup>> CreateGroup(GroupAddEdit group) | |||||||||||||||||
| OuGroup newOuGroup = await _ouGroupService.CreateRapsGroup(group.Name, group.Description); | ||||||||||||||||||
| return CreatedAtAction("CreateGroup", new { id = newOuGroup.OugroupId }, newOuGroup); | ||||||||||||||||||
| } | ||||||||||||||||||
| catch (Exception ex) | ||||||||||||||||||
| catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException) | ||||||||||||||||||
| { | ||||||||||||||||||
| //Exception message could be indication user is trying to create a group that exists or is invalid. | ||||||||||||||||||
| return ValidationProblem(ex.Message); | ||||||||||||||||||
|
Comment on lines
+117
to
120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not return raw exception messages to clients.
Suggested fix catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
- //Exception message could be indication user is trying to create a group that exists or is invalid.
- return ValidationProblem(ex.Message);
+ // Log details server-side; return a safe client message.
+ return ValidationProblem("Unable to create group. Verify the request and try again.");
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||
| using Viper.Classes; | ||||||||||||||||
| using Viper.Classes.SQLContext; | ||||||||||||||||
| using Viper.Models.RAPS; | ||||||||||||||||
| using Microsoft.Data.SqlClient; | ||||||||||||||||
|
|
||||||||||||||||
| namespace Viper.Areas.RAPS.Controllers | ||||||||||||||||
| { | ||||||||||||||||
|
|
@@ -222,7 +223,7 @@ public async Task<ActionResult<TblRole>> PostTblRole(string instance, RoleCreate | |||||||||||||||
| { | ||||||||||||||||
| return Problem("The record was not updated because it was locked. " + ex.InnerException?.Message); | ||||||||||||||||
| } | ||||||||||||||||
| catch (Exception ex) | ||||||||||||||||
| catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException) | ||||||||||||||||
| { | ||||||||||||||||
| return Problem("There was a problem updating the database. " + ex.InnerException?.Message); | ||||||||||||||||
|
Comment on lines
+226
to
228
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove Appending Suggested fix catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException)
{
- return Problem("There was a problem updating the database. " + ex.InnerException?.Message);
+ return Problem("There was a problem updating the database.");
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| using Viper.Classes.Utilities; | ||
| using Viper.Models.RAPS; | ||
| using static Viper.Areas.RAPS.Services.RAPSAuditService; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Viper.Areas.RAPS.Services | ||
| { | ||
|
|
@@ -45,7 +46,7 @@ public async Task<List<Group>> GetAllGroups(string? search) | |
| { | ||
| ActiveDirectoryService.GetGroups(); | ||
| } | ||
| catch (Exception ex) | ||
| catch (Exception ex) when (ex is DbUpdateException or SqlException or InvalidOperationException or OperationCanceledException) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win Replace filtered Line 49 still uses a broad As per coding guidelines, " 🤖 Prompt for AI Agents |
||
| { | ||
| Logger logger = LogManager.GetCurrentClassLogger(); | ||
| logger.Error(ex); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix incorrect entity name in delete error message.
The delete-competency path returns
"Could not remove domain...", which is misleading for clients and logs.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents