Skip to content

🔒 [Security Fix] Command Injection in Appium Server Capabilities Parsing on Windows - #237

Open
Dor-bl wants to merge 1 commit into
mainfrom
security-fix-command-injection-windows-capabilities-14054609694786984673
Open

🔒 [Security Fix] Command Injection in Appium Server Capabilities Parsing on Windows#237
Dor-bl wants to merge 1 commit into
mainfrom
security-fix-command-injection-windows-capabilities-14054609694786984673

Conversation

@Dor-bl

@Dor-bl Dor-bl commented Aug 5, 2026

Copy link
Copy Markdown
Owner

🎯 What: This PR fixes a command injection vulnerability in the 'OptionCollector.ParseCapabilitiesIfWindows' method. The original code used manual string concatenation to build a JSON-like string for the '--default-capabilities' flag, which failed to properly escape user-provided quotes.

⚠️ Risk: An attacker could provide a capability value containing double quotes (e.g., 'value" : "injected') to break out of the JSON string and inject arbitrary arguments into the local Appium server launch command. This could lead to unauthorized code execution or privilege escalation depending on the environment and the arguments injected.

🛡️ Solution: The manual string building logic was replaced with 'JsonSerializer.Serialize' from the standard library. This ensures that all special characters, including quotes, are correctly escaped according to JSON standards. The resulting JSON string is then properly escaped for use as a shell argument by replacing all double quotes with backslash-escaped quotes ('"') and wrapping the final result in double quotes. This fix aligns the Windows-specific parsing logic with the secure approach already used for UNIX-like platforms while preserving the necessary path formatting for Windows-only capabilities.

Verification:

  • Added unit tests in 'test/integration/Options/OptionCollectorTests.cs' that use reflection to test the private 'ParseCapabilitiesIfWindows' method.
  • Verified that malicious payloads containing quotes are now safely escaped (e.g., as '"' or '\u0022') and do not break the JSON structure.
  • Verified that Windows file path conversion (backslashes to forward slashes) is maintained for specific capability keys.
  • The fix was reviewed and rated as correct.

PR created automatically by Jules for task 14054609694786984673 started by @Dor-bl

- Replace manual JSON string construction with 'JsonSerializer.Serialize' in 'ParseCapabilitiesIfWindows'.
- Properly escape double quotes for shell arguments, ensuring consistency with 'ParseCapabilitiesIfUNIX'.
- Maintain platform-specific backslash-to-forward-slash conversion for file path capabilities.
- Add reproduction tests in 'OptionCollectorTests.cs' using reflection to verify the fix and prevent regressions.
Copilot AI lite review requested due to automatic review settings August 5, 2026 21:21
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens how Appium server --default-capabilities are constructed on Windows by switching from manual string concatenation to System.Text.Json serialization, with accompanying integration tests to cover escaping and Windows path normalization.

Changes:

  • Replaced manual JSON-like string building in OptionCollector.ParseCapabilitiesIfWindows with JsonSerializer.Serialize and argument escaping.
  • Added integration tests (via reflection) to exercise Windows-specific capability parsing and file path normalization.
  • Aligned UNIX and Windows capability serialization flow to use the same serializer-based approach.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Appium.Net/Appium/Service/Options/OptionCollector.cs Updates Windows capabilities argument construction to use JSON serialization + escaping.
test/integration/Options/OptionCollectorTests.cs Adds tests targeting quote escaping and Windows path conversion behavior in Windows parsing logic.
Suppressed comments (1)

test/integration/Options/OptionCollectorTests.cs:58

  • This test also uses reflection without asserting the method exists; a missing method will fail with a NullReferenceException at Invoke instead of a clear assertion message.
            var collector = new OptionCollector();
            var method = typeof(OptionCollector).GetMethod("ParseCapabilitiesIfWindows", BindingFlags.NonPublic | BindingFlags.Instance);

            var capabilities = new Dictionary<string, object>
            {
                { MobileCapabilityType.App, @"C:\path\to\app.apk" }
            };

            // Act
            var result = (string)method.Invoke(collector, new object[] { capabilities });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +91 to +95
// Serialize to JSON and escape double quotes so they survive argument parsing
var json = JsonSerializer.Serialize(processedCapabilities);
// Escape double quotes with backslash for shell argument
var escaped = json.Replace("\"", "\\\"");
return $"\"{escaped}\"";
Comment on lines +33 to +43
Assert.That(result, Does.StartWith("\"{"));
Assert.That(result, Does.EndWith("}\""));

// With JsonSerializer.Serialize, internal quotes are escaped: \"
// Then we replace " with \": \\\"
// So "anotherKey": "value\" : \"injected" becomes something like:
// \\\"anotherKey\\\":\\\"value\\\\\\\" : \\\\\\\"injected\\\"

Assert.That(result, Does.Contain("\\\"normalKey\\\":\\\"normalValue\\\""));
Assert.That(result, Does.Contain("\\\"anotherKey\\\":\\\"value\\\\\\\" : \\\\\\\"injected\\\""));
}
Comment on lines +16 to +32
// Arrange
var collector = new OptionCollector();
var method = typeof(OptionCollector).GetMethod("ParseCapabilitiesIfWindows", BindingFlags.NonPublic | BindingFlags.Instance);

var capabilities = new Dictionary<string, object>
{
{ "normalKey", "normalValue" },
{ "maliciousKey\" : \"injected\"", "maliciousValue" },
{ "anotherKey", "value\" : \"injected" }
};

// Act
var result = (string)method.Invoke(collector, new object[] { capabilities });

// Assert
Console.WriteLine($"Result: {result}");

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