Skip to content

Commit f379e13

Browse files
jj-ivxclaude
andcommitted
fix: stop leaking exception messages to clients via JSON-RPC error data
Tool handler exceptions and anything reaching JsonRpcHandler's blind rescue had their #message echoed into the error data member, exposing internals to untrusted clients (CWE-209). Capability errors keep their client-facing message by re-raising through RequestHandlerError, the one channel whose message is deliberately surfaced. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent d51d4a0 commit f379e13

5 files changed

Lines changed: 23 additions & 14 deletions

File tree

‎lib/json_rpc_handler.rb‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,12 @@ def process_request(request, id_validation_pattern:, &method_finder)
135135
success_response(id: id, result: result)
136136
rescue MCP::Server::RequestHandlerError => e
137137
handle_request_error(e, id, id_validation_pattern)
138-
rescue StandardError => e
138+
rescue StandardError
139+
# The exception message is deliberately not echoed into `data`: it can carry
140+
# internals that must not reach untrusted clients (CWE-209).
139141
error_response(id: id, id_validation_pattern: id_validation_pattern, error: {
140142
code: ErrorCode::INTERNAL_ERROR,
141143
message: "Internal error",
142-
data: e.message,
143144
})
144145
end
145146
end

‎lib/mcp/server.rb‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,14 @@ def handle_request(request, method, session: nil, related_request_id: nil)
560560
return
561561
end
562562

563-
Methods.ensure_capability!(method, capabilities)
563+
begin
564+
Methods.ensure_capability!(method, capabilities)
565+
rescue Methods::MissingRequiredCapabilityError => e
566+
# Re-raise through RequestHandlerError, the one channel whose message is
567+
# deliberately surfaced to clients: `JsonRpcHandler`'s blind `rescue StandardError`
568+
# no longer echoes exception messages into the error `data` member (CWE-209).
569+
raise RequestHandlerError.new(e.message, request, error_type: :internal_error, original_error: e)
570+
end
564571

565572
# `initialize` MUST NOT be cancelled (MCP spec 2025-11-25, cancellation item 2),
566573
# so do not track it in the in-flight registry.
@@ -864,8 +871,11 @@ def call_tool(request, session: nil, related_request_id: nil, cancellation: nil,
864871
# `JsonRpcHandler::NO_RESPONSE` per the MCP cancellation spec.
865872
raise
866873
rescue => e
874+
# `e.message` is deliberately not included: it can carry internals (class, method
875+
# and host names) that must not reach untrusted clients (CWE-209). The original
876+
# exception still reaches `configuration.exception_reporter` via `original_error`.
867877
raise RequestHandlerError.new(
868-
"Internal error calling tool #{tool_name}: #{e.message}",
878+
"Internal error calling tool #{tool_name}",
869879
request,
870880
error_type: :internal_error,
871881
original_error: e,

‎test/json_rpc_handler_test.rb‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@
310310
assert_rpc_error expected_error: {
311311
code: -32603,
312312
message: "Internal error",
313-
data: "Something bad happened",
314313
}
315314
assert_equal 1, @response[:id]
316315
assert_nil @response[:result]
@@ -452,7 +451,6 @@
452451
assert_rpc_error expected_error: {
453452
code: -32603,
454453
message: "Internal error",
455-
data: "Something bad happened",
456454
}
457455
end
458456

‎test/mcp/server_context_test.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def call(message:, server_context:)
545545
assert_nil response[:result]
546546
assert_equal(-32603, response[:error][:code])
547547
assert_equal "Internal error", response[:error][:message]
548-
assert_match(/Internal error calling tool tool_with_required_context: /, response[:error][:data])
548+
assert_equal "Internal error calling tool tool_with_required_context", response[:error][:data]
549549
end
550550

551551
test "call_tool_with_args correctly detects server_context parameter presence" do

‎test/mcp/server_test.rb‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ def call(message:, server_context: nil)
884884
assert_nil response[:result]
885885
assert_equal(-32603, response[:error][:code])
886886
assert_equal "Internal error", response[:error][:message]
887-
assert_match(/Internal error calling tool tool_that_raises: /, response[:error][:data])
887+
assert_equal "Internal error calling tool tool_that_raises", response[:error][:data]
888888
assert_instrumentation_data({ method: "tools/call", tool_name: "tool_that_raises", tool_arguments: { message: "test" }, error: :internal_error })
889889
end
890890

@@ -941,7 +941,7 @@ class Example < Tool
941941
assert_nil response[:result]
942942
assert_equal(-32603, response[:error][:code])
943943
assert_equal "Internal error", response[:error][:message]
944-
assert_match(/Internal error calling tool tool_that_raises: /, response[:error][:data])
944+
assert_equal "Internal error calling tool tool_that_raises", response[:error][:data]
945945
assert_instrumentation_data({ method: "tools/call", tool_name: "tool_that_raises", tool_arguments: { message: "test" }, error: :internal_error })
946946
end
947947

@@ -972,7 +972,7 @@ class Example < Tool
972972
assert_nil response[:result]
973973
assert_equal(-32603, response[:error][:code])
974974
assert_equal "Internal error", response[:error][:message]
975-
assert_match(/Internal error calling tool tool_with_faulty_schema: Unexpected schema error/, response[:error][:data])
975+
assert_equal "Internal error calling tool tool_with_faulty_schema", response[:error][:data]
976976
end
977977

978978
test "#handle tools/call returns JSON-RPC error for unknown tool" do
@@ -1727,7 +1727,7 @@ def read_resource_request(uri)
17271727

17281728
assert_equal(["tool failure", "around ensure boom"], reported)
17291729
assert_equal(JsonRpcHandler::ErrorCode::INTERNAL_ERROR, response[:error][:code])
1730-
assert_equal("around ensure boom", response[:error][:data])
1730+
assert_nil(response[:error][:data])
17311731
end
17321732

17331733
test "#handle reports the same exception object reused across requests on every call" do
@@ -1779,7 +1779,7 @@ def read_resource_request(uri)
17791779
response = server.handle(request)
17801780

17811781
assert_equal([frozen_error], reported)
1782-
assert_includes(response[:error][:data], "frozen failure")
1782+
assert_equal("Internal error calling tool frozen_tool", response[:error][:data])
17831783
end
17841784

17851785
test "#handle still reports via exception_reporter when around_request swallows the tool failure" do
@@ -2635,7 +2635,7 @@ def read_resource_request(uri)
26352635
assert_nil response[:result]
26362636
assert_equal(-32603, response[:error][:code])
26372637
assert_equal "Internal error", response[:error][:message]
2638-
assert_match(/Internal error calling tool invalid_structured_content_tool: Invalid result:/, response[:error][:data])
2638+
assert_equal "Internal error calling tool invalid_structured_content_tool", response[:error][:data]
26392639
end
26402640

26412641
test "tools/call returns JSON-RPC error when output schema validation is enabled and structuredContent is missing" do
@@ -2664,7 +2664,7 @@ def read_resource_request(uri)
26642664
assert_nil response[:result]
26652665
assert_equal(-32603, response[:error][:code])
26662666
assert_equal "Internal error", response[:error][:message]
2667-
assert_match(/Internal error calling tool missing_structured_content_tool: Invalid result:/, response[:error][:data])
2667+
assert_equal "Internal error calling tool missing_structured_content_tool", response[:error][:data]
26682668
end
26692669

26702670
test "tools/call skips output schema validation for error responses" do

0 commit comments

Comments
 (0)