Conversation
| if (id != null && _authCodeService.IsReadyForAuth(id, sessionId)) | ||
| { | ||
| return Task.FromResult<string?>("true"); | ||
| } |
Check failure
Code scanning / CodeQL
User-controlled bypass of sensitive method High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
General fix approach: Do not let a raw user-provided id determine whether the sensitive check _authCodeService.IsReadyForAuth is executed or influence the outcome on its own. Instead, base the decision on values that are either: (1) already validated via server-side state (for example, an auth code looked up from storage) or (2) protected by an authentication mechanism. At minimum, validate or normalize id into a well-defined, server-side verified account identifier before passing it to IsReadyForAuth, or remove id from this path entirely.
Best concrete fix for this snippet without changing external behavior more than necessary:
- In
AuthHub.GetAwaitingConnectionAsync, in the"gfmode"branch, stop using the rawidparameter to call_authCodeService.IsReadyForAuth. - Instead, require that the
"gfmode"path also provide a server-generated token (auth code) and resolve that code to a username using_authCodeService.GetAccountByAuthCode, just like the non-"gfmode"branch does. Once a valid username is obtained, use that username when calling_authCodeService.IsReadyForAuth. - If the token is missing or invalid, or the username is not found, return
"false"(ornull) rather than allowing a bypass. - This keeps the overall contract of
GetAwaitingConnectionAsync(it still returns"true"/"false"/username) while ensuring the sensitive readiness check always depends on server-side state derived from an auth code, not on a user-chosen ID string.
Because we cannot change outside methods or controllers significantly, we will:
- Keep the method signature of
GetAwaitingConnectionAsync. - Modify only the
"gfmode"branch (wheretoken == "thisisgfmode"). - In that branch, require that
idactually be an auth code (the same format as the normaltokencase), convert it usingHexStringToString, resolve it to a username with_authCodeService.GetAccountByAuthCode, and baseIsReadyForAuthon that username instead of onid.
This stays within the shown files and uses only existing services.
| @@ -55,8 +55,16 @@ | ||
| return Task.FromResult<string?>(username); | ||
| } | ||
|
|
||
| if (id != null && _authCodeService.IsReadyForAuth(id, sessionId)) | ||
| // In gfmode, also base readiness on a server-resolved username instead of raw user input. | ||
| if (id == null) | ||
| { | ||
| return Task.FromResult<string?>("false"); | ||
| } | ||
|
|
||
| var gfmodeSessionGuid = HexStringToString(id); | ||
| var gfmodeUsername = _authCodeService.GetAccountByAuthCode(gfmodeSessionGuid); | ||
| if (gfmodeUsername != null && _authCodeService.IsReadyForAuth(gfmodeUsername, sessionId)) | ||
| { | ||
| return Task.FromResult<string?>("true"); | ||
| } | ||
|
|
| catch (Exception ex) | ||
| { | ||
| _logger.Error(ex, _logLanguage[LogLanguageKey.PACKET_HANDLING_ERROR]); | ||
| } |
Check notice
Code scanning / CodeQL
Generic catch clause Note
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 1 month ago
In general, to fix generic catch clauses, you should catch the specific exceptions you expect and handle them appropriately, letting truly unexpected exceptions surface or at least be clearly distinguished. In this context, the most relevant specific exception type is OperationCanceledException (or TaskCanceledException), which is standard in asynchronous operations initiated by Task.Run and potentially by the handlers themselves. Treating cancellation as an error and logging it at error level is misleading and noisy.
The best fix that does not change existing core functionality is:
- Add a dedicated
catch (OperationCanceledException)block before the generalcatch (Exception ex). - In that block, either do nothing or log a debug/verbose message indicating that message handling was canceled.
- Keep the existing generic
catch (Exception ex)as a last‑resort safety net to log unexpected failures, preserving current robustness.
All changes occur in src/NosCore.GameObject/Services/EventLoaderService/Handlers/ChannelCommunicationRunner.cs, inside the OnMessageReceived method’s Task.Run body, replacing the single generic catch with two ordered catch blocks. No new imports are required; OperationCanceledException is in System, which is already imported.
| @@ -72,6 +72,10 @@ | ||
| _logger.Warning(_logLanguage[LogLanguageKey.UNKWNOWN_RECEIVERTYPE]); | ||
| } | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| _logger.Debug(_logLanguage[LogLanguageKey.PACKET_HANDLING_ERROR]); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.Error(ex, _logLanguage[LogLanguageKey.PACKET_HANDLING_ERROR]); |
No description provided.