From 88cdb524662d3a139340b32ec185a0728ba2d227 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 29 Nov 2026 00:15:10 +0900 Subject: [PATCH] Sign the forged token of the key confusion test without `JWT.encode` ## Motivation and Context `test_rejects_hs256_key_confusion` presents the verifier with a token signed with HS256, using the RSA public key as the HMAC secret, and expects it to be refused. It built that token with `JWT.encode`. jwt 3.3.2 refuses to sign with a public key or a certificate as an HMAC secret, so the test now errors with `JWT::EncodeError` before it reaches the verifier, and CI fails on every Ruby version that resolves jwt 3.3.2. The test now computes the HMAC itself, so it keeps checking the verifier with every jwt version. The verifier is unchanged. ## How Has This Been Tested? `test/mcp/server/oauth/jwt_verifier_test.rb`, run with jwt 3.3.0 and with jwt 3.3.2. Before the change it errors with jwt 3.3.2. ## Breaking Changes None. --- test/mcp/server/oauth/jwt_verifier_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/mcp/server/oauth/jwt_verifier_test.rb b/test/mcp/server/oauth/jwt_verifier_test.rb index 96ed575c..14eaac17 100644 --- a/test/mcp/server/oauth/jwt_verifier_test.rb +++ b/test/mcp/server/oauth/jwt_verifier_test.rb @@ -100,7 +100,11 @@ def test_rejects_alg_none def test_rejects_hs256_key_confusion stub_jwks - forged = JWT.encode(claims, @rsa_key.public_key.to_pem, "HS256") + # Signed by hand, since jwt 3.3.2 and later refuse to sign with a public key as the HMAC secret. + header = { alg: "HS256", typ: "JWT", kid: @jwk[:kid] } + signing_input = [header, claims].map { |part| urlsafe(JSON.generate(part)) }.join(".") + signature = OpenSSL::HMAC.digest("SHA256", @rsa_key.public_key.to_pem, signing_input) + forged = "#{signing_input}.#{urlsafe(signature)}" error = assert_raises(InvalidTokenError) { jwks_verifier.verify(forged) }