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

Make sure alg parameter value isn't added twice #297

Merged
merged 4 commits into from
Mar 15, 2019

Conversation

korstiaan
Copy link
Contributor

The alg parameter key (ALG_KEY) is a string, and added to the header hash as such ({ "alg" => "foo" }). If it's already in the header hash, but as a symbol ({ alg: "foo"}), it's appended to the this hash ({ alg: "foo", "alg" => "foo"}`) resulting in the end in a different token.

We noticed Apple's Mapkit JS complaing about this.

This PR fixes this.

@sourcelevel-bot
Copy link

Hello, @korstiaan! This is your first Pull Request that will be reviewed by Ebert, 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.

lib/jwt/encode.rb Outdated Show resolved Hide resolved
lib/jwt/encode.rb Outdated Show resolved Hide resolved
@@ -39,7 +39,7 @@ def encoded_header_and_payload
end

def encode_header
encode(@headers.merge(ALG_KEY => @algorithm))
encode(@headers.transform_keys(&:to_s).merge(ALG_KEY => @algorithm))
Copy link
Member

@anakinj anakinj Feb 11, 2019

Choose a reason for hiding this comment

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

I think Hash#transform_keys was added to Ruby 2.5.0, so this won't work on older rubies. I think something like what is done here could be applied for the headers also.

Also if a copy of the @headers hash is created the Hash#merge! method could be used to save one object allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've moved the stringify'ing (as done in the claims validator) to the constructor, and directly add ALG_KEY to the headers hash instead of .merge

Copy link
Member

Choose a reason for hiding this comment

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

Nice, looks good to me.

@excpt excpt added this to the Version 2.2.0 milestone Mar 15, 2019
@excpt excpt merged commit 1dc69d5 into jwt:master Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants