From fabeb16bb032f616043d99c1ca17441c1db8cb45 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 11 Nov 2026 03:14:02 +0900 Subject: [PATCH] Keep the private key and the headers out of `inspect` ## Motivation and Context `inspect` is what error reporters, consoles and log lines print an object with, and the default one walks every instance variable. On `MCP::Client::HTTP` that printed the values of the static `headers:`, often an API key, the userinfo and query of the URL, and the session id. On `ClientCredentialsProvider` it printed a `private_key` given as a PEM String in full. On `MCP::Client::Stdio` it printed the `env:` and the arguments given to the server process, where its API keys usually go, and on the server side `IntrospectionVerifier`, `JWTVerifier` and `RequestStateSecurity` printed their client secret or key. `MCP::Client#inspect` prints its transport, so it showed the same. `InMemoryStorage` already keeps the tokens and the client secret out of its `inspect` for this reason. `MCP::Client::HTTP#inspect` now shows the header names, the URL without its userinfo and query, and whether a session is open, and `ClientCredentialsProvider#inspect` shows whether a key is held. Each names the class of the provider or storage it holds and prints nothing of it, since what a custom one prints is its own to decide. `MCP::Client::Stdio#inspect` shows the command, the number of arguments and the names of the variables, and the three server classes show whether they hold a secret or a key. ## How Has This Been Tested? `test/mcp/client/http_test.rb`, `test/mcp/client/oauth/client_credentials_provider_test.rb`, `test/mcp/client/stdio_test.rb`, `test/mcp/server/oauth/introspection_verifier_test.rb`, `test/mcp/server/oauth/jwt_verifier_test.rb` and `test/mcp/server/request_state_security_test.rb`. Against the previous library each new test finds the secret in the output. ## Breaking Changes None, unless a caller parsed the default `inspect` output of these classes. --- lib/mcp/client/http.rb | 15 +++++++++++ .../oauth/client_credentials_provider.rb | 7 ++++++ lib/mcp/client/stdio.rb | 8 ++++++ .../server/oauth/introspection_verifier.rb | 6 +++++ lib/mcp/server/oauth/jwt_verifier.rb | 6 +++++ lib/mcp/server/request_state_security.rb | 5 ++++ test/mcp/client/http_test.rb | 25 +++++++++++++++++++ .../oauth/client_credentials_provider_test.rb | 13 ++++++++++ test/mcp/client/stdio_test.rb | 9 +++++++ .../oauth/introspection_verifier_test.rb | 5 ++++ test/mcp/server/oauth/jwt_verifier_test.rb | 8 ++++++ .../mcp/server/request_state_security_test.rb | 4 +++ 12 files changed, 111 insertions(+) diff --git a/lib/mcp/client/http.rb b/lib/mcp/client/http.rb index 6131d99e..a00f36e3 100644 --- a/lib/mcp/client/http.rb +++ b/lib/mcp/client/http.rb @@ -602,6 +602,21 @@ def close end end + # `inspect` is what error reporters and consoles print an object with, and the default one walks every instance variable: + # the static `headers:` (often an API key), the Faraday connection that holds them as well, and the session id. + # It shows the header names only, the URL without its userinfo and query, whether a session is open, and + # the class of the `oauth:` provider: what a provider or its storage prints is theirs to decide, and may hold a credential. + def inspect + safe_url = begin + MCP::Client::OAuth::Discovery.canonicalize_origin_and_path(@url) + rescue StandardError + "[unparseable]" + end + + "#<#{self.class.name} url=#{safe_url.inspect} headers=#{(@headers || {}).keys.inspect} " \ + "session_id=#{@session_id.nil? ? "nil" : "[present]"} oauth=#{@oauth.nil? ? "nil" : @oauth.class.name}>" + end + private attr_reader :headers diff --git a/lib/mcp/client/oauth/client_credentials_provider.rb b/lib/mcp/client/oauth/client_credentials_provider.rb index a0f1eb08..a602742d 100644 --- a/lib/mcp/client/oauth/client_credentials_provider.rb +++ b/lib/mcp/client/oauth/client_credentials_provider.rb @@ -154,6 +154,13 @@ def client_assertion(audience:) ) end + # `inspect` is what error reporters and consoles print an object with, and the default one would print + # the `private_key` given as a PEM String in full, so it shows only whether a key is held, and the class of + # the storage: what a custom storage prints is its own to decide. + def inspect + "#<#{self.class.name} client_id=#{@client_id.inspect} private_key=#{@private_key.nil? ? "nil" : "[present]"} storage=#{@storage.class.name}>" + end + private def validate_private_key_jwt_arguments!(client_secret:, private_key:, signing_algorithm:) diff --git a/lib/mcp/client/stdio.rb b/lib/mcp/client/stdio.rb index 84c5cd61..ffdf260b 100644 --- a/lib/mcp/client/stdio.rb +++ b/lib/mcp/client/stdio.rb @@ -77,6 +77,14 @@ def initialize(command:, args: [], env: nil, inherit_env: false, read_timeout: n @write_mutex = Mutex.new end + # The default `inspect` would print the `env:` given to the server process, which is where its API keys usually go, + # and the arguments, which can hold one as well. It shows the command, the number of arguments and the names + # of the variables only. + def inspect + "#<#{self.class.name} command=#{@command.inspect} args=#{Array(@args).size} env=#{(@env || {}).keys.inspect} " \ + "started=#{@started}>" + end + # Performs the MCP `initialize` handshake: sends an `initialize` request # followed by the required `notifications/initialized` notification. The # server's `InitializeResult` (protocol version, capabilities, server diff --git a/lib/mcp/server/oauth/introspection_verifier.rb b/lib/mcp/server/oauth/introspection_verifier.rb index 87cc50eb..7393a192 100644 --- a/lib/mcp/server/oauth/introspection_verifier.rb +++ b/lib/mcp/server/oauth/introspection_verifier.rb @@ -87,6 +87,12 @@ def verify(token) access_token end + # The default `inspect` would print the `client_secret` this verifier authenticates to the introspection endpoint with. + def inspect + "#<#{self.class.name} introspection_endpoint=#{@introspection_endpoint.inspect} client_id=#{@client_id.inspect} " \ + "client_secret=#{@client_secret.nil? ? "nil" : "[present]"} client_auth_method=#{@client_auth_method.inspect}>" + end + private def introspect(token) diff --git a/lib/mcp/server/oauth/jwt_verifier.rb b/lib/mcp/server/oauth/jwt_verifier.rb index 8512835d..fff9ba63 100644 --- a/lib/mcp/server/oauth/jwt_verifier.rb +++ b/lib/mcp/server/oauth/jwt_verifier.rb @@ -118,6 +118,12 @@ def verify(token) raise InvalidTokenError, "Invalid token" end + # The default `inspect` would print the `key:`, which for HMAC algorithms is the shared secret itself. + def inspect + "#<#{self.class.name} issuer=#{@issuer.inspect} audience=#{@audience.inspect} jwks_uri=#{@jwks_uri.inspect} " \ + "key=#{@key.nil? ? "nil" : "[present]"} algorithms=#{@algorithms.inspect}>" + end + private def decode(token) diff --git a/lib/mcp/server/request_state_security.rb b/lib/mcp/server/request_state_security.rb index 92922655..dd9cbe29 100644 --- a/lib/mcp/server/request_state_security.rb +++ b/lib/mcp/server/request_state_security.rb @@ -47,6 +47,11 @@ def initialize(key:, ttl: DEFAULT_TTL, audience: nil) @audience = audience end + # The default `inspect` would print the `key:`, with which anyone can seal a state this server accepts. + def inspect + "#<#{self.class.name} ttl=#{@ttl.inspect} audience=#{@audience.inspect}>" + end + # Seals a plaintext state into an opaque token bound to the originating request. def seal(state, method:, target:, arguments_digest:) claims = { diff --git a/test/mcp/client/http_test.rb b/test/mcp/client/http_test.rb index 36093d5c..b4034401 100644 --- a/test/mcp/client/http_test.rb +++ b/test/mcp/client/http_test.rb @@ -48,6 +48,31 @@ def test_raises_load_error_when_event_stream_parser_not_available assert_includes(error.message, "Add it to your Gemfile: gem 'event_stream_parser', '>= 1.0'") end + def test_inspect_shows_no_header_value_userinfo_query_or_session_id + transport = HTTP.new(url: "https://user:pass@example.com/mcp?key=query-secret", headers: { "X-Api-Key" => "header-secret" }) + transport.instance_variable_set(:@session_id, "session-secret") + + # The connection holds the headers too, and the default `inspect` would walk into it. + transport.send(:client) + + [transport.inspect, MCP::Client.new(transport: transport).inspect].each do |text| + ["header-secret", "user:pass", "query-secret", "session-secret"].each { |secret| refute_includes(text, secret) } + end + assert_equal('#', transport.inspect) + end + + def test_inspect_shows_the_class_of_the_oauth_provider_and_nothing_it_holds + storage = Object.new + storage.define_singleton_method(:inspect) { "storage-secret" } + [:tokens, :client_information].each { |name| storage.define_singleton_method(name) { nil } } + [:save_tokens, :save_client_information].each { |name| storage.define_singleton_method(name) { |_value| nil } } + provider = MCP::Client::OAuth::ClientCredentialsProvider.new(client_id: "cc-client", client_secret: "s3cret", storage: storage) + transport = HTTP.new(url: "https://example.com/mcp", oauth: provider) + + assert_includes(transport.inspect, "oauth=MCP::Client::OAuth::ClientCredentialsProvider>") + [transport.inspect, provider.inspect].each { |text| refute_includes(text, "storage-secret") } + end + def test_headers_are_added_to_the_request headers = { "Authorization" => "Bearer token" } client = HTTP.new(url: url, headers: headers) diff --git a/test/mcp/client/oauth/client_credentials_provider_test.rb b/test/mcp/client/oauth/client_credentials_provider_test.rb index 708c1bad..e90e07ee 100644 --- a/test/mcp/client/oauth/client_credentials_provider_test.rb +++ b/test/mcp/client/oauth/client_credentials_provider_test.rb @@ -176,6 +176,19 @@ def private_key_jwt_provider(key: generate_es256_key) ) end + def test_inspect_does_not_print_the_private_key + pem = generate_es256_key.to_pem + provider = ClientCredentialsProvider.new( + client_id: "cc-client", + token_endpoint_auth_method: "private_key_jwt", + private_key: pem, + signing_algorithm: "ES256", + ) + + refute_includes(provider.inspect, "PRIVATE KEY") + assert_includes(provider.inspect, 'client_id="cc-client" private_key=[present]') + end + def test_initialize_accepts_private_key_jwt provider = private_key_jwt_provider diff --git a/test/mcp/client/stdio_test.rb b/test/mcp/client/stdio_test.rb index 7f08e6b1..1288d075 100644 --- a/test/mcp/client/stdio_test.rb +++ b/test/mcp/client/stdio_test.rb @@ -23,6 +23,15 @@ def test_send_request_raises_when_connect_not_called assert_equal("MCP::Client#connect must be called before sending requests.", error.message) end + def test_inspect_shows_no_argument_or_environment_value + transport = Stdio.new(command: "ruby", args: ["server.rb", "--token=arg-secret"], env: { "API_KEY" => "env-secret" }) + + [transport.inspect, MCP::Client.new(transport: transport).inspect].each do |text| + ["arg-secret", "env-secret"].each { |secret| refute_includes(text, secret) } + end + assert_equal('#', transport.inspect) + end + def test_send_request_starts_process_and_returns_response stdin_read, stdin_write = IO.pipe stdout_read, stdout_write = IO.pipe diff --git a/test/mcp/server/oauth/introspection_verifier_test.rb b/test/mcp/server/oauth/introspection_verifier_test.rb index 5d377fc8..32a1a8f2 100644 --- a/test/mcp/server/oauth/introspection_verifier_test.rb +++ b/test/mcp/server/oauth/introspection_verifier_test.rb @@ -286,6 +286,11 @@ def test_requires_client_id_unless_none assert_includes(error.message, "client_id is required") end + def test_inspect_does_not_print_the_client_secret + refute_includes(verifier.inspect, "rs-secret") + assert_includes(verifier.inspect, 'client_id="rs-client" client_secret=[present]') + end + private def verifier(client_auth_method: :client_secret_basic, client_id: "rs-client", client_secret: "rs-secret", resource_metadata: METADATA) diff --git a/test/mcp/server/oauth/jwt_verifier_test.rb b/test/mcp/server/oauth/jwt_verifier_test.rb index 14eaac17..1d138675 100644 --- a/test/mcp/server/oauth/jwt_verifier_test.rb +++ b/test/mcp/server/oauth/jwt_verifier_test.rb @@ -542,6 +542,14 @@ def test_stale_jwks_is_not_served_beyond_the_stale_bound assert_equal("JWKS endpoint responded with status 500", error.message) end + def test_inspect_does_not_print_an_hmac_key + secret = "hmac-shared-secret-of-at-least-32-bytes" + verifier = JWTVerifier.new(resource_metadata: METADATA, key: secret, algorithms: ["HS256"]) + + refute_includes(verifier.inspect, secret) + assert_includes(verifier.inspect, 'key=[present] algorithms=["HS256"]') + end + private def claims diff --git a/test/mcp/server/request_state_security_test.rb b/test/mcp/server/request_state_security_test.rb index 045eaff2..c56ae8fb 100644 --- a/test/mcp/server/request_state_security_test.rb +++ b/test/mcp/server/request_state_security_test.rb @@ -14,6 +14,10 @@ class RequestStateSecurityTest < ActiveSupport::TestCase assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: 0) } end + test "does not print its key in inspect" do + refute_includes(RequestStateSecurity.new(key: KEY).inspect, KEY) + end + test "rejects a ttl that is not finite" do assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::INFINITY) } assert_raises(ArgumentError) { RequestStateSecurity.new(key: KEY, ttl: Float::NAN) }