Skip to content

Conversation

@Kenny-Rogers
Copy link

Fixes https://github.com/hubtel/programmable-services-sdk-dotnet/security/code-scanning/2

To fix the problem, we need to sanitize the SessionId before logging it. Since the log entries are plain text, we should remove any new line characters from the SessionId to prevent log forging. This can be done using the String.Replace method to replace new line characters with an empty string.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
//this action will be called anytime a user wants to interact with your application
_logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", request.Mobile, request.SessionId,
var sanitizedSessionId = request.SessionId?.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
_logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", request.Mobile, sanitizedSessionId,

Check failure

Code scanning / CodeQL

Log entries created from user input

This log entry depends on a [user-provided value](1).

Copilot Autofix

AI about 1 year ago

To fix the problem, we need to sanitize the request.Mobile value before logging it. Since the log entries are likely plain text, we should remove any new line characters from the user input. This can be done using the String.Replace method to ensure that no line endings are present in the user input.

Suggested changeset 1
examples/WebApplication1/Controllers/RequestController.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/WebApplication1/Controllers/RequestController.cs b/examples/WebApplication1/Controllers/RequestController.cs
--- a/examples/WebApplication1/Controllers/RequestController.cs
+++ b/examples/WebApplication1/Controllers/RequestController.cs
@@ -44,3 +44,4 @@
             var sanitizedSessionId = request.SessionId?.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
-            _logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", request.Mobile, sanitizedSessionId,
+            var sanitizedMobile = request.Mobile?.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
+            _logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", sanitizedMobile, sanitizedSessionId,
                 JsonConvert.SerializeObject(request));
EOF
@@ -44,3 +44,4 @@
var sanitizedSessionId = request.SessionId?.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
_logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", request.Mobile, sanitizedSessionId,
var sanitizedMobile = request.Mobile?.Replace(Environment.NewLine, "").Replace("\n", "").Replace("\r", "");
_logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", sanitizedMobile, sanitizedSessionId,
JsonConvert.SerializeObject(request));
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants