`JWTVerifier` answers 500 instead of 401 for a PS256 token when it verifies with an EC key (jwt < 3.3.0)

Offen
#659 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@atesgoral arbeitet bereits daran.

Seit 06.11.2026.

  • #666 von @atesgoral — offen

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
ruby

Rechercherichtung

Start with lib/mcp/server/oauth/jwt_verifier.rb at the algorithm configuration and verify rescue paths, then follow the middleware error handling in lib/mcp/server/oauth/middleware.rb. Reproduce with the provided EC key and PS256 token on jwt 3.2.0, then check the middleware response. Done when this case returns 401 invalid_token instead of 500; the issue requests regression coverage for key:, jwks: and jwks_uri:.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug

JWTVerifier allows PS256, PS384 and PS512 together with ES256, ES384 and ES512 by default (jwt_verifier.rb:40), whatever the key type. With an EC key, a static EC JWKS or a fetched EC JWKS, a token whose header says alg: PS256 makes jwt 3.2.0 and earlier call verify_pss on the EC key. That raises NoMethodError.

JWTVerifier#verify rescues only jwt errors (jwt_verifier.rb:92-110). So OAuth::Middleware (middleware.rb:45-51) and the Streamable HTTP transport (streamable_http_transport.rb:2413-2418) answer 500 server_error and call the exception reporter, instead of 401 invalid_token. Any client can send such a token. It needs no key.

jwt 3.3.0 fixed the root cause in jwt/ruby-jwt#722: Ps#verify now raises JWT::VerificationKeyError for a non-RSA key. That class is a JWT::DecodeError, so with jwt 3.3.0 the same token gives InvalidTokenError. But docs/installation.md asks for gem "jwt" with no version, so applications on jwt 3.2.0 and earlier, including the 2.x line, still get 500.

Repro

Run from the repository root with bundle exec ruby -I lib repro.rb:

require "mcp"
require "jwt"
require "openssl"
require "base64"

metadata = MCP::Server::OAuth::ProtectedResourceMetadata.new(
  resource: "https://mcp.example.com/mcp",
  authorization_servers: ["https://as.example.com"],
)
verifier = MCP::Server::OAuth::JWTVerifier.new(
  resource_metadata: metadata,
  key: OpenSSL::PKey::EC.generate("prime256v1"),
)
header = { alg: "PS256", typ: "JWT" }
claims = { iss: "https://as.example.com", aud: "https://mcp.example.com/mcp", exp: Time.now.to_i + 600 }
token = [header, claims].map { |part| Base64.urlsafe_encode64(JSON.generate(part), padding: false) }.join(".") + ".AAAA"

puts "jwt #{JWT::VERSION::STRING}"
begin
  verifier.verify(token)
rescue => e
  puts "#{e.class}: #{e.message}"
end

Expected (and the result with jwt 3.3.0):

jwt 3.3.0
MCP::Server::OAuth::InvalidTokenError: Invalid token

Actual with jwt 3.2.0:

jwt 3.2.0
NoMethodError: undefined method 'verify_pss' for an instance of OpenSSL::PKey::EC

Passing an explicit algorithms: ["ES256"] avoids the problem.

Suggested fix

Any of these:

  • Drop the algorithms that do not fit the configured key before calling JWT.decode: RS* and PS* for RSA keys, ES* for EC keys, EdDSA for Ed25519 keys. This works with every jwt version.
  • Require jwt 3.3.0 or later when JWTVerifier loads, with a clear error, and document the minimum version.

Add a regression test for each key source (key:, jwks:, jwks_uri:) through the middleware.

Vorherrschende Sprache
Ruby
Sterne
940
Forks
143
Ø Merge
1 T. 3 Std.
Gemergte PRs (30 T.)
47

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/ruby-sdk

Alle Issues in modelcontextprotocol/ruby-sdk

Ähnliche Issues

Weitere Issues zu Ruby

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.