Skip to content

Commit 97af16e

Browse files
authored
Merge pull request #4127 from eyupcanakman/fix/uri-invalid-scheme-crash
Don't crash on document URIs with an invalid scheme
2 parents c5ebf5a + 45788c4 commit 97af16e

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

lib/ruby_lsp/base_server.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def start
7474
)
7575
end
7676
end
77+
rescue URI::Error
78+
# A client can send a document URI with a scheme Ruby's URI parser rejects, so we don't want to fail
7779
rescue Store::NonExistingDocumentError
7880
# If we receive a request for a file that no longer exists, we don't want to fail
7981
end

test/server_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,36 @@ def test_unrecognized_request_returns_method_not_found
17491749
assert_equal("Method not found: #{non_existent_method}", error.message)
17501750
end
17511751

1752+
def test_does_not_crash_when_a_document_uri_cannot_be_parsed
1753+
# A document URI with a scheme Ruby's URI parser rejects must not crash the server
1754+
body = {
1755+
id: 1,
1756+
method: "textDocument/hover",
1757+
params: {
1758+
textDocument: { uri: "_diff_view:/path/to/file.rb" },
1759+
position: { line: 0, character: 0 },
1760+
},
1761+
}.to_json
1762+
server = RubyLsp::Server.new(
1763+
test_mode: true,
1764+
reader: StringIO.new("Content-Length: #{body.bytesize}\r\n\r\n#{body}"),
1765+
writer: StringIO.new,
1766+
)
1767+
1768+
error = nil #: StandardError?
1769+
capture_subprocess_io do
1770+
server.start
1771+
rescue StandardError => e
1772+
error = e
1773+
end
1774+
1775+
assert_nil(error, "server crashed on an unparseable document URI: #{error&.message}")
1776+
# The request still gets an error response instead of hanging
1777+
assert_instance_of(RubyLsp::Error, server.pop_response)
1778+
ensure
1779+
server&.run_shutdown
1780+
end
1781+
17521782
private
17531783

17541784
def run_initialize_server_with_setup_error(error)

0 commit comments

Comments
 (0)