🔒 [Security Fix] Command Injection in Appium Server Capabilities Parsing on Windows - #237
Conversation
- 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.
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
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.ParseCapabilitiesIfWindowswithJsonSerializer.Serializeand 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
Invokeinstead 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.
| // 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}\""; |
| 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\\\"")); | ||
| } |
| // 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}"); | ||
|
|
🎯 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.
🛡️ 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:
PR created automatically by Jules for task 14054609694786984673 started by @Dor-bl