Skip to content

Reject invalid runId in JSON-RPC requests with InvalidParams error - #8665

Merged
Amaury Levé (Evangelink) merged 2 commits into
mainfrom
dev/amauryleve/issue-8423
May 29, 2026
Merged

Amaury Levé (Evangelink) merged 2 commits into
mainfrom
dev/amauryleve/issue-8423

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Fixes #8423.

Problem

When a client sends a JSON-RPC request (testing/discoverTests or testing/runTests) with a malformed runId value (e.g., a non-GUID string), the deserializer silently coerced it to Guid.Empty via Guid.TryParse. This hid the protocol violation from the client and could cause hard-to-diagnose downstream behavior.

Fix

  • DiscoverRequestArgs and RunRequestArgs deserializers in both Json.cs (System.Text.Json) and SerializerUtilities.cs (Jsonite) now throw MessageFormatException when runId cannot be parsed as a Guid.
  • The RpcMessage deserializer now parses id first, then wraps the per-method params deserialization in a try/catch. On failure, it embeds an InvalidRequestParamsArgs(ErrorCode, ErrorMessage) sentinel in Params so the message can flow through to the request handler without crashing the server's message-handling loop.
  • ServerTestHost.HandleRequestCoreAsync detects the sentinel and throws a new JsonRpcException carrying the proper error code; HandleRequestAsync catches it and sends a JSON-RPC InvalidParams (-32602) error response to the client.

Tests

  • Added FormatterUtilitiesTests coverage for invalid runId on both args types (per-args deserializer throws; full RpcMessage deserializer captures sentinel).
  • Added a ServerTests integration test that sends a malformed request, asserts an InvalidParams error is returned, and verifies the server cleanly handles the subsequent exit notification (i.e., the loop is still alive).
  • Fixed pre-existing DiscoveryRequestCanBeCanceled test that used an invalid runId of Run1 (now uses a valid GUID).

Fixes #8423.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 28, 2026 15:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 fixes a JSON-RPC protocol bug where invalid runId values in testing/discoverTests / testing/runTests requests were silently coerced to Guid.Empty. The deserializers now throw MessageFormatException for unparseable run IDs, and the RPC message deserializer wraps per-method param deserialization in a try/catch that captures failures in a new InvalidRequestParamsArgs sentinel so the request can still flow to the handler and produce a proper JSON-RPC InvalidParams (-32602) error response.

Changes:

  • Both serializer paths (System.Text.Json Json.cs and Jsonite SerializerUtilities.cs) now reject invalid runId and route param-deserialization failures through a new sentinel type.
  • ServerTestHost translates the sentinel into a new JsonRpcException, then into a properly coded JSON-RPC error response.
  • Tests added for direct deserializer behavior, full RpcMessage sentinel capture, and an end-to-end server test; pre-existing DiscoveryRequestCanBeCanceled test updated to use a valid GUID.
Show a summary per file
File Description
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.cs STJ path: throws MessageFormatException on invalid runId; wraps params parse in try/catch and reads id first to build sentinel for requests.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs Jsonite path: mirrors the same invalid-runId throw and sentinel-on-failure behavior.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/RpcMessages.cs Adds InvalidRequestParamsArgs sentinel record.
src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/JsonRpcException.cs New internal exception carrying an ErrorCode to bridge handler to error response.
src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs Handles sentinel by throwing JsonRpcException; sends InvalidParams error response and completes request state.
test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/FormatterUtilitiesTests.cs Tests for per-args throw and full RpcMessage sentinel capture across both formatters.
test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs Fixes prior invalid runId value and adds end-to-end test that the server returns InvalidParams and stays alive.

Copilot's findings

  • Files reviewed: 7/7 changed files
  • Comments generated: 0

@Evangelink Amaury Levé (Evangelink) left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Review Summary

The core design is sound: the sentinel + JsonRpcException plumbing correctly keeps the message loop alive and sends the right -32602 InvalidParams error code for the targeted runId validation case. The cleanup of the "Run1" test fixture is a necessary correctness fix.

However, the exception guard that was added to route deserialization failures through the sentinel path is incomplete: it only catches MessageFormatException, but two adjacent code paths throw different exception types that escape the guard:

  1. SerializerUtilities.cs — wrong-typed fields ("runId": 42): GetRequiredPropertyFromJson<string> does an unsafe (T) unboxing cast → InvalidCastException, not caught → client receives error code 0 with e.ToString() (stack trace leak) instead of -32602.
  2. Json.cs — missing field (runId absent entirely): Bind<string> calls element.GetProperty(property) which throws KeyNotFoundException, not MessageFormatException, same outcome.

Both scenarios involve untrusted client input arriving at the same trust boundary the PR is hardening, so they are in scope to fix here.

The two MAJOR inline comments include concrete reproduction payloads and suggested fixes. The remaining comments are a NIT (duplicate string) and a MODERATE (missing runTests integration test).

Generated by Expert Code Review (on open) for issue #8665 · sonnet46 6.6M

- Bind<T> (Json.cs) throws MessageFormatException on missing property instead of KeyNotFoundException.
- GetRequiredPropertyFromJson<T> (SerializerUtilities.cs) throws MessageFormatException on wrong-typed property instead of InvalidCastException.
- Broadened RpcMessage param-deserialization catch in both parsers to also handle InvalidOperationException/JsonException (Json.cs) and InvalidCastException (SerializerUtilities.cs), so any untrusted-input deserialization failure routes through the InvalidRequestParamsArgs sentinel and produces a JSON-RPC -32602 InvalidParams response.
- Extracted shared InvalidRunIdErrorMessage constant on JsonRpcStrings.
- TrySetException for JsonRpcException in ServerTestHost for defensive consistency.
- Parameterized ServerTests integration test (DiscoverTests + RunTests) and added RpcMessage-level coverage for wrong-typed and missing runId.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Evangelink
Amaury Levé (Evangelink) merged commit ae78aa8 into main May 29, 2026
59 of 60 checks passed
@Evangelink
Amaury Levé (Evangelink) deleted the dev/amauryleve/issue-8423 branch May 29, 2026 07:41
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.

[MTP Core Analysis] JSON-RPC request deserialization coerces invalid runIds to Guid.Empty

2 participants