Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue RbNaCl exception for EdDSA wrong key #491

Merged
merged 4 commits into from
Jun 22, 2022

Conversation

n-studio
Copy link
Contributor

Fixes #490

Instead of raising an error when a public key is invalid, we should return false so the key finder can test the next key.

@sourcelevel-bot
Copy link

Hello, @n-studio! This is your first Pull Request that will be reviewed by SourceLevel, an automatic Code Review service. It will leave comments on this diff with potential issues and style violations found in the code as you push new commits. You can also see all the issues found on this Pull Request on its review page. Please check our documentation for more information.

ruby-jwt.gemspec Outdated Show resolved Hide resolved
ruby-jwt.gemspec Outdated
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'appraisal'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rbnacl'
Copy link
Member

@anakinj anakinj Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would rather not have the rbnacl dependency as a default dependency, as including this will require some other libs to be installed locally also.

Also including this will change the behaviour for some functionality.

The combination of different dependencies are tested using Appraisal and this set of gemfiles.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To run tests with the rbnacl dependency, you can either run everything using bundle exec appraisal or for only the rbnacl dependency:

BUNDLE_GEMFILE=gemfiles/rbnacl.gemfile  bundle install
BUNDLE_GEMFILE=gemfiles/rbnacl.gemfile bundle exec rspec

@@ -65,6 +65,24 @@
]
end

it 'EDDSA' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readme_examples_spec.rb is dedicated to tests for the examples in the readme file. I think the test (with some good context/explanation) could be in the block for rbnacl tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an addition to this keyfinder with multiple keys a more simple test could be

 context 'when wrong ED25519 key is used to verify signature' do
      it 'raises a ::JWT:DecodeError' do
        token = JWT.encode(payload, RbNaCl::Signatures::Ed25519::SigningKey.generate, 'ED25519')
        expect { JWT.decode(token,  RbNaCl::Signatures::Ed25519::SigningKey.generate.verify_key, true, algorithm: 'ED25519') }.to raise_error(::JWT::DecodeError)
      end
    end

@anakinj
Copy link
Member

anakinj commented Jun 21, 2022

This looks great, good catch. It seems that the signature verification is leaking the RbNaCl errors when the signature is invalid. Think it's a more generic issue than the key rotation issue.

Added a few comments about the changes.

@n-studio
Copy link
Contributor Author

@anakinj Thank you for the review! I updated my code accordingly.

@n-studio n-studio changed the title Fix key rotation for EDDSA Rescue RbNaCl exception for EdDSA wrong key Jun 22, 2022
@@ -218,7 +218,7 @@

it 'wrong key should raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key
JWT.decode data[alg], wrong_key, true, algorithm: alg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this was a good catch, the test has been wrong all the time, right. Like passing for the wrong reasons.

@anakinj
Copy link
Member

anakinj commented Jun 22, 2022

This is great stuff. Thank you for the fix.

@anakinj anakinj merged commit c34d7a2 into jwt:master Jun 22, 2022
@n-studio n-studio deleted the fix/ed-25519-rotation branch June 22, 2022 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rotating keys with ED25519
2 participants