Skip to content

Add Playwright functions#387

Open
MikeAlhayek wants to merge 4 commits intomainfrom
ma/add-playwright
Open

Add Playwright functions#387
MikeAlhayek wants to merge 4 commits intomainfrom
ma/add-playwright

Conversation

@MikeAlhayek
Copy link
Copy Markdown
Member

No description provided.


if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Auto-starting browser automation session for '{StartUrl}' because no active session was found for the default alias.", bootstrapUrl);

Check failure

Code scanning / CodeQL

Log entries created from user input High

This log entry depends on a
user-provided value
.
This log entry depends on a
user-provided value
.

Copilot Autofix

AI 10 days ago

To fix the problem, we should ensure that user-controlled data (bootstrapUrl) is sanitized before being written to the log. Since the log message appears to be plain text and we do not have context that it will be safely HTML-encoded later, the conservative approach is to remove newline characters and other problematic control characters from the string before logging it. This mitigates log forging by preventing attackers from injecting extra log lines or confusing content.

The best minimally invasive fix is to introduce a small, private helper method inside BrowserAutomationService that “log-sanitizes” strings: replacing \r and \n with empty strings (and optionally trimming). Then, at the log site at line 378, call this helper on bootstrapUrl and log the sanitized value instead of the raw one. This keeps existing functionality intact (the URL content is otherwise unchanged) while addressing all CodeQL variants that flag this logging statement.

Concretely:

  • In src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.cs, add a new private static method, e.g. SanitizeForLog(string value), near the other private static helpers (TryGetAbsoluteHttpUrl is a good reference point).
  • Implement SanitizeForLog using string.Replace to strip \r and \n (and possibly \t) characters, and return the result (or empty string if null).
  • Modify the logging call at line 378 to use SanitizeForLog(bootstrapUrl) instead of bootstrapUrl. No new external dependencies or using directives are required.
Suggested changeset 1
src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.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/src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.cs b/src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.cs
--- a/src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.cs
+++ b/src/Modules/CrestApps.OrchardCore.AI.Agent/Services/BrowserAutomationService.cs
@@ -375,7 +375,8 @@
 
         if (_logger.IsEnabled(LogLevel.Information))
         {
-            _logger.LogInformation("Auto-starting browser automation session for '{StartUrl}' because no active session was found for the default alias.", bootstrapUrl);
+            var sanitizedBootstrapUrl = SanitizeForLog(bootstrapUrl);
+            _logger.LogInformation("Auto-starting browser automation session for '{StartUrl}' because no active session was found for the default alias.", sanitizedBootstrapUrl);
         }
 
         var snapshot = await CreateSessionAsync(
@@ -473,6 +474,19 @@
         await context.AddCookiesAsync(cookies);
     }
 
+    private static string SanitizeForLog(string value)
+    {
+        if (string.IsNullOrEmpty(value))
+        {
+            return string.Empty;
+        }
+
+        // Remove newline characters to prevent log forging.
+        return value
+            .Replace("\r", string.Empty, StringComparison.Ordinal)
+            .Replace("\n", string.Empty, StringComparison.Ordinal);
+    }
+
     private static bool TryGetAbsoluteHttpUrl(string value, out string normalizedUrl)
     {
         normalizedUrl = null;
EOF
@@ -375,7 +375,8 @@

if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Auto-starting browser automation session for '{StartUrl}' because no active session was found for the default alias.", bootstrapUrl);
var sanitizedBootstrapUrl = SanitizeForLog(bootstrapUrl);
_logger.LogInformation("Auto-starting browser automation session for '{StartUrl}' because no active session was found for the default alias.", sanitizedBootstrapUrl);
}

var snapshot = await CreateSessionAsync(
@@ -473,6 +474,19 @@
await context.AddCookiesAsync(cookies);
}

private static string SanitizeForLog(string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}

// Remove newline characters to prevent log forging.
return value
.Replace("\r", string.Empty, StringComparison.Ordinal)
.Replace("\n", string.Empty, StringComparison.Ordinal);
}

private static bool TryGetAbsoluteHttpUrl(string value, out string normalizedUrl)
{
normalizedUrl = null;
Copilot is powered by AI and may make mistakes. Always verify output.
@github-actions
Copy link
Copy Markdown

This pull request has merge conflicts. Please resolve those before requesting a review.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

This pull request has merge conflicts. Please resolve those before requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant