Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Raise `JWT::MalformedTokenError` for a token header that is not a JSON object, including a string header in `JWT.decode` without verification [#783](https://github.com/jwt/ruby-jwt/pull/783) ([@anakinj](https://github.com/anakinj))
- Do not call the `key_finder` of `JWT::EncodedToken` when the token's algorithm is not allowed, so a mismatched `alg` header no longer verifies with a key finder [#784](https://github.com/jwt/ruby-jwt/pull/784) ([@anakinj](https://github.com/anakinj))
- Accept the RFC 8812 curve name `secp256k1` when importing EC JWKs, alongside `P-256K` [#787](https://github.com/jwt/ruby-jwt/pull/787) ([@anakinj](https://github.com/anakinj))
- Raise `JWT::JWKError` instead of `OpenSSL::PKey::EC::Point::Error` for EC JWK coordinates that are not a point on the given curve [#789](https://github.com/jwt/ruby-jwt/pull/789) ([@anakinj](https://github.com/anakinj))
- Your contribution here

## [v3.3.0](https://github.com/jwt/ruby-jwt/tree/v3.3.0) (2026-09-11)
Expand Down
2 changes: 2 additions & 0 deletions lib/jwt/jwk/ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def create_point(jwk_crv, jwk_x, jwk_y)
OpenSSL::PKey::EC::Group.new(curve),
OpenSSL::BN.new([0x04, x_octets, y_octets].pack('Ca*a*'), 2)
)
rescue OpenSSL::PKey::EC::Point::Error
raise JWT::JWKError, "Invalid EC point for curve #{jwk_crv}"
end

if ::JWT.openssl_3?
Expand Down
33 changes: 33 additions & 0 deletions spec/jwt/jwk/ec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,38 @@
expect(payload).to eq('sub' => '1')
end
end

context 'when the coordinates are not a point on the given curve' do
let(:ec_key) { OpenSSL::PKey::EC.generate('prime256v1') }
let(:keypair) { ec_key }
let(:public_params) { exported_key }

{
'the curve does not match the coordinates' => ->(params) { params.merge(crv: 'P-384') },
'the point is not on the curve' => ->(params) { params.merge(y: params[:x]) }
}.each do |description, invalidate|
context "when #{description}" do
let(:params) { invalidate.call(public_params) }

it 'raises a JWT::JWKError when the key is used' do
jwk = subject
%i[verify_key private? export].each do |method_name|
expect { jwk.public_send(method_name) }.to raise_error(JWT::JWKError, /Invalid EC point for curve/) do |error|
expect(error.cause).to be_a(OpenSSL::PKey::EC::Point::Error)
end
end
end

it 'raises a JWT::JWKError when decoding with a JWKS' do
token = JWT.encode({ 'sub' => '1' }, ec_key, 'ES256', kid: 'ec-key')
jwks = { keys: [params.merge(kid: 'ec-key')] }

expect do
JWT.decode(token, nil, true, algorithms: ['ES256'], jwks: jwks)
end.to raise_error(JWT::JWKError, /Invalid EC point for curve/)
end
end
end
end
end
end
Loading