Skip to content

Commit 1486ea0

Browse files
committed
Refuse a skill URI that is not valid UTF-8 and escape the echoed strings
## Motivation and Context Three places echoed request data without regard to where it goes. `skills/get` and `resources/read` with a `params.uri` holding bytes that are not valid UTF-8 answered -32603 instead of -32602. The JSON parser passes such bytes through inside a string, `Skill::Uri.segments` then raised from `split`, and the refusal's `error_data: { uri: }` could not be generated, which `handle_json` also turns into Internal error. `Uri.segments` now requires a validly encoded String, so such a URI is not a manifest URI, and the three Skills refusals put the URI into their error data read as UTF-8 with its invalid bytes replaced. A `params.uri` that is not a String is not echoed at all, since an Array or Hash could hold such bytes anywhere; the data then falls back to the message, as for the other refusals. The "Skill not found" and "Skill file not found" messages carried the URI verbatim to the client and to the exception reporter, so a URI holding CR, LF, or ESC could forge or recolor a reporter log line. Both messages now quote the URI with `dump`, which keeps it on one printable ASCII line, and so does the detail reported when a `skills/get` handler answers for another URI. The older Resource, Tool, and Prompt messages are left as they are: changing them is a separate decision, while the Skills messages are unreleased. `MCP::Client` warns on stderr when it excludes a tool from `tools/list` for an invalid `x-mcp-header` definition, and the warning printed the property names and header values the server chose, raw. The tool name (inspected first when it is not a String) and the reason are now quoted with `dump` as well, so bidirectional overrides and control characters cannot reorder or forge the line. ## How Has This Been Tested? test/mcp/skill_test.rb, test/mcp/server_skills_test.rb, test/mcp/skill/directory_test.rb, and test/mcp/client_test.rb. Before the fix, `skills/get` with `skill://pdf-processing\xFF/SKILL.md` answered -32603 "Internal error handling skills/get request", `resources/read` with such a URI under a skill root and `skills/get` with a `params.uri` Hash holding such bytes answered -32603 through `handle_json`, the reporter received "Skill not found: skill://x\r\nforged..." with the raw CR and LF, and the exclusion warning printed the U+202E property name and the ESC in the header value unescaped. A binary-tagged `skill://x\xC3\xA9/SKILL.md` given to `handle` came back with the message and `data.uri` as binary Strings, which json 3.1 refuses to generate (json 3.0 generated them with a deprecation warning, since the bytes are valid UTF-8). ## Breaking Changes None on the wire. The two messages that now quote the URI were added with the Skills extension, which is unreleased, and the error code and `data.uri` of a valid UTF-8 URI are unchanged. A String in another encoding given to `Server#handle` directly now has its bytes read as UTF-8 for `data.uri`, where JSON used to transcode it. The stderr warning of `MCP::Client` for an excluded tool changes its wording, since it now quotes the two strings.
1 parent 8bbe002 commit 1486ea0

7 files changed

Lines changed: 161 additions & 16 deletions

File tree

‎lib/mcp/client.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,9 @@ def exclude_invalid_x_mcp_header?(tool)
697697
scan = McpParamHeaders.scan(tool["inputSchema"])
698698
return false if scan[:valid]
699699

700-
warn("MCP::Client: excluding tool #{tool["name"].inspect} from tools/list: #{scan[:reason]}")
700+
name = tool["name"]
701+
# Both pieces are the server's: `dump` keeps control characters and bidirectional overrides out of the log line.
702+
warn("MCP::Client: excluding tool #{(name.is_a?(String) ? name : name.inspect).dump} from tools/list: #{scan[:reason].dump}")
701703
true
702704
end
703705

‎lib/mcp/server.rb‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,15 +1547,15 @@ def get_skill(request, server_context: nil)
15471547
request,
15481548
error_type: :invalid_params,
15491549
error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS,
1550-
error_data: { uri: uri },
1550+
error_data: refused_uri_data(uri),
15511551
)
15521552
end
15531553

15541554
skill = if @skills_get_handler
15551555
result = invoke_skills_handler(@skills_get_handler, request, server_context)
15561556
unless result.nil?
15571557
raise invalid_skills_result(request, "skills/get returned #{result.class}") unless result.is_a?(Skill)
1558-
raise invalid_skills_result(request, "skills/get returned #{result.uri} for #{uri}") unless result.uri == uri
1558+
raise invalid_skills_result(request, "skills/get returned #{result.uri.dump} for #{uri.dump}") unless result.uri == uri
15591559

15601560
problem = skills_problem([result])
15611561
raise invalid_skills_result(request, problem) if problem
@@ -1567,11 +1567,11 @@ def get_skill(request, server_context: nil)
15671567

15681568
if skill.nil?
15691569
raise RequestHandlerError.new(
1570-
"Skill not found: #{uri}",
1570+
"Skill not found: #{uri.dump}",
15711571
request,
15721572
error_type: :invalid_params,
15731573
error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS,
1574-
error_data: { uri: uri },
1574+
error_data: refused_uri_data(uri),
15751575
)
15761576
end
15771577

@@ -1763,11 +1763,11 @@ def read_resource(request, server_context: nil)
17631763
# is not served, and the Skills extension asks for `-32602` where the base reader keeps its `[]`.
17641764
if resource.nil? && uri.is_a?(String) && @skill_file_roots.any? { |root| Skill::Uri.under?(uri, root) }
17651765
raise RequestHandlerError.new(
1766-
"Skill file not found: #{uri}",
1766+
"Skill file not found: #{uri.dump}",
17671767
request,
17681768
error_type: :invalid_params,
17691769
error_code: JsonRpcHandler::ErrorCode::INVALID_PARAMS,
1770-
error_data: { uri: uri },
1770+
error_data: refused_uri_data(uri),
17711771
)
17721772
end
17731773

@@ -2089,5 +2089,15 @@ def normalize_completion_result(result)
20892089

20902090
{ completion: { values: values, total: total, hasMore: has_more }.compact }
20912091
end
2092+
2093+
# The error data of a Skills refusal: the request URI when it is a String, read as UTF-8 with its invalid bytes
2094+
# replaced, so that the response can be generated whatever bytes the request held; nothing for another value,
2095+
# which could hold such bytes anywhere, so the data falls back to the message. The messages quote the URI with
2096+
# `dump` for the same reason, and so that the exception reporter gets one printable line.
2097+
def refused_uri_data(uri)
2098+
return unless uri.is_a?(String)
2099+
2100+
{ uri: uri.dup.force_encoding(Encoding::UTF_8).scrub }
2101+
end
20922102
end
20932103
end

‎lib/mcp/skill.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ def resolvable?(uri)
6363
uri.bytesize <= MAX_RESOLVED_BYTES && uri.count("/") <= MAX_RESOLVED_SEGMENTS + 1
6464
end
6565

66-
# The path segments of a `skill://` URI, or nil when it is not structurally valid: a String in
67-
# an ASCII-compatible encoding with at least one segment, none empty, none `.` or `..`.
66+
# The path segments of a `skill://` URI, or nil when it is not structurally valid: a validly encoded String
67+
# in an ASCII-compatible encoding with at least one segment, none empty, none `.` or `..`.
6868
def segments(uri)
69-
return unless uri.is_a?(String) && uri.encoding.ascii_compatible? && uri.start_with?(SCHEME)
69+
return unless uri.is_a?(String) && uri.encoding.ascii_compatible? && uri.valid_encoding? && uri.start_with?(SCHEME)
7070

7171
parts = uri[SCHEME.length..-1].split("/", -1)
7272
return if parts.empty? || parts.any? { |part| part.empty? || part == "." || part == ".." }

‎test/mcp/client_test.rb‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,68 @@ def test_tools_excludes_invalid_x_mcp_header_definitions_on_a_modern_connection
425425
assert_includes(warning, "invalid_tool")
426426
end
427427

428+
def test_tools_exclusion_warning_quotes_the_strings_the_server_chose
429+
# The tool name, the property names, and the `x-mcp-header` values in the warning are the server's; control
430+
# characters and bidirectional overrides in them could forge or reorder the line, so each piece is dumped.
431+
transport = mock
432+
transport.stubs(:modern?).returns(true)
433+
mock_response = {
434+
"result" => {
435+
"tools" => [
436+
{
437+
"name" => "tool\nMCP::Client: forged",
438+
"inputSchema" => {
439+
"type" => "object",
440+
"properties" => { "\u202Eeulav" => { "type" => "string", "x-mcp-header" => "X-Bad Name\e[0m" } },
441+
},
442+
},
443+
],
444+
},
445+
}
446+
transport.expects(:send_request).returns(mock_response).once
447+
client = Client.new(transport: transport)
448+
449+
original_verbose = $VERBOSE
450+
$VERBOSE = false
451+
tools = nil
452+
_out, warning = capture_io { tools = client.tools }
453+
$VERBOSE = original_verbose
454+
455+
assert_empty(tools)
456+
assert_equal(
457+
"MCP::Client: excluding tool \"tool\\nMCP::Client: forged\" from tools/list: " \
458+
"\"\\u202Eeulav: x-mcp-header `X-Bad Name\\e[0m` is not a valid RFC 9110 token\"\n",
459+
warning,
460+
)
461+
end
462+
463+
def test_tools_exclusion_warning_dumps_a_tool_name_that_is_not_a_string
464+
transport = mock
465+
transport.stubs(:modern?).returns(true)
466+
mock_response = {
467+
"result" => {
468+
"tools" => [
469+
{
470+
"name" => { "x" => "\u202E" },
471+
"inputSchema" => { "type" => "object", "properties" => { "v" => { "type" => "string", "x-mcp-header" => "bad name" } } },
472+
},
473+
],
474+
},
475+
}
476+
transport.expects(:send_request).returns(mock_response).once
477+
client = Client.new(transport: transport)
478+
479+
original_verbose = $VERBOSE
480+
$VERBOSE = false
481+
_out, warning = capture_io { client.tools }
482+
$VERBOSE = original_verbose
483+
484+
# `inspect` leaves a printable character such as U+202E inside a nested String as it is, so the whole is dumped.
485+
assert_equal(1, warning.lines.size)
486+
refute_match(/[^\x00-\x7F]/, warning)
487+
assert_includes(warning, "\\u202E")
488+
end
489+
428490
def test_tools_keeps_invalid_x_mcp_header_definitions_on_a_legacy_connection
429491
transport = mock
430492
transport.stubs(:modern?).returns(false)

‎test/mcp/server_skills_test.rb‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def initialize_capabilities(server)
6767
assert_equal({ skills: [] }, server.handle(request("skills/list"))[:result])
6868
response = server.handle(request("skills/get", { uri: "skill://x/SKILL.md" }))
6969
assert_equal(-32602, response[:error][:code])
70-
assert_equal "Skill not found: skill://x/SKILL.md", response[:error][:message]
70+
assert_equal 'Skill not found: "skill://x/SKILL.md"', response[:error][:message]
7171
assert_equal({ uri: "skill://x/SKILL.md" }, response[:error][:data])
7272
end
7373

@@ -139,12 +139,65 @@ def initialize_capabilities(server)
139139
test "skills/get refuses a URI that does not name a SKILL.md before any lookup" do
140140
server = Server.new(name: "catalog", skills: [pdf_skill])
141141

142-
["skill://pdf-processing/references/FORMS.md", "skill://pdf-processing", "http://x/SKILL.md", nil, 42].each do |uri|
142+
["skill://pdf-processing/references/FORMS.md", "skill://pdf-processing", "http://x/SKILL.md"].each do |uri|
143143
response = server.handle(request("skills/get", { uri: uri }))
144-
assert_equal(-32602, response[:error][:code], uri.inspect)
144+
assert_equal(-32602, response[:error][:code], uri)
145145
assert_equal "Invalid params: uri is not a skill SKILL.md URI", response[:error][:message]
146146
assert_equal({ uri: uri }, response[:error][:data])
147147
end
148+
149+
# A value that is not a String is not echoed, since it could hold bytes JSON cannot generate anywhere.
150+
[nil, 42, { "k" => "\xFF" }, ["\xFF"]].each do |uri|
151+
response = server.handle(request("skills/get", { uri: uri }))
152+
assert_equal(-32602, response[:error][:code], uri.inspect)
153+
assert_equal "Invalid params: uri is not a skill SKILL.md URI", response[:error][:message]
154+
assert_equal response[:error][:message], response[:error][:data], uri.inspect
155+
end
156+
json = server.handle_json(%({"jsonrpc":"2.0","id":1,"method":"skills/get","params":{"uri":{"k":"\xFF"}}}))
157+
assert_equal(-32602, JSON.parse(json, symbolize_names: true)[:error][:code])
158+
end
159+
160+
test "skills/get refuses a URI that is not valid UTF-8 as invalid params, with an answer JSON can generate" do
161+
server = Server.new(name: "catalog", skills: [pdf_skill])
162+
uri = "skill://pdf-processing\xFF/SKILL.md"
163+
164+
response = server.handle(request("skills/get", { uri: uri }))
165+
assert_equal(-32602, response[:error][:code])
166+
assert_equal "Invalid params: uri is not a skill SKILL.md URI", response[:error][:message]
167+
assert_equal "skill://pdf-processing\uFFFD/SKILL.md", response[:error][:data][:uri]
168+
169+
# The parser passes the bytes through inside a string, and the refusal has to be generated around them.
170+
json = server.handle_json(%({"jsonrpc":"2.0","id":1,"method":"skills/get","params":{"uri":"#{uri}"}}))
171+
response = JSON.parse(json, symbolize_names: true)
172+
assert_equal(-32602, response[:error][:code])
173+
assert_equal "skill://pdf-processing\uFFFD/SKILL.md", response[:error][:data][:uri]
174+
end
175+
176+
test "the Skill not found message quotes the URI, so the exception reporter gets one printable line" do
177+
reported = []
178+
configuration = Configuration.new(exception_reporter: ->(exception, _context) { reported << exception })
179+
server = Server.new(name: "catalog", skills: [pdf_skill], configuration: configuration)
180+
uri = "skill://x\r\nforged: line\e[0m/SKILL.md"
181+
182+
response = server.handle(request("skills/get", { uri: uri }))
183+
assert_equal(-32602, response[:error][:code])
184+
assert_equal 'Skill not found: "skill://x\r\nforged: line\e[0m/SKILL.md"', response[:error][:message]
185+
assert_equal({ uri: uri }, response[:error][:data])
186+
assert_equal [response[:error][:message]], reported.map(&:message)
187+
end
188+
189+
test "a URI given to handle as a binary String is read as UTF-8 for the error data" do
190+
server = Server.new(name: "catalog", skills: [pdf_skill])
191+
192+
# Validly encoded, so a manifest URI that names no skill. json 3.1 refuses to generate a binary String holding
193+
# high bytes (json 3.0 generates this one with a deprecation warning, since its bytes are valid UTF-8), so the
194+
# error data carries the same bytes read as UTF-8.
195+
response = server.handle(request("skills/get", { uri: "skill://x\xC3\xA9/SKILL.md".b }))
196+
assert_equal(-32602, response[:error][:code])
197+
assert_equal 'Skill not found: "skill://x\xC3\xA9/SKILL.md"', response[:error][:message]
198+
assert_equal "skill://x\xC3\xA9/SKILL.md", response[:error][:data][:uri]
199+
assert_equal Encoding::UTF_8, response[:error][:data][:uri].encoding
200+
assert_equal "skill://x\xC3\xA9/SKILL.md", JSON.parse(JSON.generate(response), symbolize_names: true)[:error][:data][:uri]
148201
end
149202

150203
test "refuses a registry with duplicate URIs and accepts an enclosing skill that lists a nested skill's files" do
@@ -187,7 +240,7 @@ def initialize_capabilities(server)
187240
assert_equal pdf_skill.to_h, server.handle(request("skills/get", { uri: "skill://pdf-processing/SKILL.md" })).dig(:result, :skill)
188241
response = server.handle(request("skills/get", { uri: "skill://git/SKILL.md" }))
189242
assert_equal(-32602, response[:error][:code])
190-
assert_equal "Skill not found: skill://git/SKILL.md", response[:error][:message]
243+
assert_equal 'Skill not found: "skill://git/SKILL.md"', response[:error][:message]
191244
assert_equal [], server.skills
192245
end
193246

@@ -276,6 +329,7 @@ def initialize_capabilities(server)
276329
assert_equal(-32603, server.handle(request("skills/get", { uri: "skill://pdf-processing/SKILL.md" }))[:error][:code])
277330
results[:get] = git_skill
278331
assert_equal(-32603, server.handle(request("skills/get", { uri: "skill://pdf-processing/SKILL.md" }))[:error][:code])
332+
assert_equal 'skills/get returned "skill://git/SKILL.md" for "skill://pdf-processing/SKILL.md"', reported.last.message
279333
assert_equal 5, reported.size
280334
assert reported.all? { |exception| exception.is_a?(StandardError) && !exception.is_a?(Server::RequestHandlerError) }
281335

‎test/mcp/skill/directory_test.rb‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def read_request(uri)
8989
assert_equal "n\n", server.handle(read_request("skill://acme/outer/inner/notes.txt")).dig(:result, :contents, 0, :text)
9090
response = server.handle(read_request("skill://acme/outer/missing.md"))
9191
assert_equal(-32602, response[:error][:code])
92-
assert_equal "Skill file not found: skill://acme/outer/missing.md", response[:error][:message]
92+
assert_equal 'Skill file not found: "skill://acme/outer/missing.md"', response[:error][:message]
9393
assert_equal [], server.handle(read_request("skill://elsewhere/x"))[:result][:contents]
9494

9595
listed = server.handle({ jsonrpc: "2.0", id: 1, method: "resources/list" })[:result][:resources]
@@ -281,11 +281,25 @@ def read_request(uri)
281281
["skill://pdf-processing/.env", "skill://pdf-processing/../pdf-processing/SKILL.md", "skill://pdf-processing/references/FORMS.md/"].each do |uri|
282282
response = server.handle(read_request(uri))
283283
assert_equal(-32602, response[:error][:code], uri)
284-
assert_equal "Skill file not found: #{uri}", response[:error][:message]
284+
assert_equal "Skill file not found: \"#{uri}\"", response[:error][:message]
285285
end
286286
end
287287
end
288288

289+
test "a URI under a skill root that is not valid UTF-8 is refused with an answer JSON can generate" do
290+
with_root do |root|
291+
pdf_root(root)
292+
server = Server.new(name: "catalog", skills: Skill.from_directory(root))
293+
uri = "skill://pdf-processing/\xFF.md"
294+
295+
json = server.handle_json(%({"jsonrpc":"2.0","id":1,"method":"resources/read","params":{"uri":"#{uri}"}}))
296+
response = JSON.parse(json, symbolize_names: true)
297+
assert_equal(-32602, response[:error][:code])
298+
assert_equal 'Skill file not found: "skill://pdf-processing/\xFF.md"', response[:error][:message]
299+
assert_equal "skill://pdf-processing/\uFFFD.md", response[:error][:data][:uri]
300+
end
301+
end
302+
289303
test "a file that changed since registration is refused with one generic error and the detail goes to the reporter" do
290304
with_root do |root|
291305
pdf_root(root)

‎test/mcp/skill_test.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,9 @@ def build(uri: "skill://pdf-processing/SKILL.md", frontmatter: { name: "pdf-proc
355355
assert uri.structural?("skill://a/b")
356356
refute uri.structural?("skill://a//b")
357357
refute uri.structural?("skill://")
358+
refute uri.structural?("skill://a\xFF/b"), "a String that is not validly encoded has no segments"
359+
assert uri.structural?("skill://a/b".b)
360+
assert_empty uri.candidate_skill_uris("skill://a\xFF/b/c")
358361
assert uri.manifest_uri?("skill://a/b/SKILL.md")
359362
refute uri.manifest_uri?("skill://SKILL.md")
360363
refute uri.manifest_uri?("skill://a/b/skill.md")

0 commit comments

Comments
 (0)