-
Notifications
You must be signed in to change notification settings - Fork 372
Add duplicate claim name detection per RFC 7519 Section 4 #713
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
Open
ydah
wants to merge
3
commits into
jwt:main
Choose a base branch
from
ydah:duplicate_claim_detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| AllCops: | ||
| TargetRubyVersion: 2.5 | ||
| TargetRubyVersion: 2.7 | ||
| NewCops: enable | ||
| SuggestExtensions: false | ||
| Exclude: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe 'Duplicate Claim Name Detection' do | ||
| let(:secret) { 'test_secret' } | ||
| let(:algorithm) { 'HS256' } | ||
|
|
||
| def sign_jwt(signing_input, secret) | ||
| signature = OpenSSL::HMAC.digest('SHA256', secret, signing_input) | ||
| JWT::Base64.url_encode(signature) | ||
| end | ||
|
|
||
| def build_jwt_with_duplicate_payload(duplicate_payload_json) | ||
| header = JWT::Base64.url_encode('{"alg":"HS256"}') | ||
| payload = JWT::Base64.url_encode(duplicate_payload_json) | ||
| signing_input = "#{header}.#{payload}" | ||
| signature = sign_jwt(signing_input, secret) | ||
| "#{signing_input}.#{signature}" | ||
| end | ||
|
|
||
| def build_jwt_with_duplicate_header(duplicate_header_json, payload_json = '{"sub":"user"}') | ||
| header = JWT::Base64.url_encode(duplicate_header_json) | ||
| payload = JWT::Base64.url_encode(payload_json) | ||
| signing_input = "#{header}.#{payload}" | ||
| signature = sign_jwt(signing_input, secret) | ||
| "#{signing_input}.#{signature}" | ||
| end | ||
|
|
||
| describe 'using EncodedToken API' do | ||
| describe 'payload with duplicate keys' do | ||
| let(:duplicate_payload_jwt) { build_jwt_with_duplicate_payload('{"sub":"user","sub":"admin"}') } | ||
|
|
||
| context 'with default behavior' do | ||
| it 'uses the last value (allows duplicates)' do | ||
| token = JWT::EncodedToken.new(duplicate_payload_jwt) | ||
| expect(token.unverified_payload['sub']).to eq('admin') | ||
| end | ||
| end | ||
|
|
||
| context 'with raise_on_duplicate_keys!' do | ||
| it 'raises DuplicateKeyError' do | ||
| token = JWT::EncodedToken.new(duplicate_payload_jwt) | ||
| token.raise_on_duplicate_keys! | ||
| expect do | ||
| token.unverified_payload | ||
| end.to raise_error(JWT::DuplicateKeyError, /duplicate key/) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'header with duplicate keys' do | ||
| let(:duplicate_header_jwt) { build_jwt_with_duplicate_header('{"alg":"HS256","alg":"none"}') } | ||
|
|
||
| context 'with default behavior' do | ||
| it 'uses the last value (allows duplicates)' do | ||
| token = JWT::EncodedToken.new(duplicate_header_jwt) | ||
| expect(token.header['alg']).to eq('none') | ||
| end | ||
| end | ||
|
|
||
| context 'with raise_on_duplicate_keys!' do | ||
| it 'raises DuplicateKeyError for header' do | ||
| token = JWT::EncodedToken.new(duplicate_header_jwt) | ||
| token.raise_on_duplicate_keys! | ||
| expect do | ||
| token.header | ||
| end.to raise_error(JWT::DuplicateKeyError, /duplicate key/) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'chaining' do | ||
| let(:valid_jwt) { build_jwt_with_duplicate_payload('{"sub":"user"}') } | ||
|
|
||
| it 'returns self for method chaining' do | ||
| token = JWT::EncodedToken.new(valid_jwt) | ||
| expect(token.raise_on_duplicate_keys!).to eq(token) | ||
| end | ||
| end | ||
|
|
||
| describe 'valid tokens' do | ||
| let(:valid_jwt) { build_jwt_with_duplicate_payload('{"sub":"user","name":"John"}') } | ||
|
|
||
| it 'parses valid JSON without duplicates' do | ||
| token = JWT::EncodedToken.new(valid_jwt) | ||
| token.raise_on_duplicate_keys! | ||
| expect(token.unverified_payload).to eq({ 'sub' => 'user', 'name' => 'John' }) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'multiple duplicate keys' do | ||
| let(:multiple_duplicates_jwt) { build_jwt_with_duplicate_payload('{"a":1,"b":2,"a":3,"b":4}') } | ||
|
|
||
| context 'with raise_on_duplicate_keys!' do | ||
| it 'raises DuplicateKeyError for the first duplicate found' do | ||
| token = JWT::EncodedToken.new(multiple_duplicates_jwt) | ||
| token.raise_on_duplicate_keys! | ||
| expect do | ||
| token.unverified_payload | ||
| end.to raise_error(JWT::DuplicateKeyError, /duplicate key/) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe JWT::JSON do | ||
| describe '.generate' do | ||
| it 'generates JSON from a hash' do | ||
| expect(described_class.generate({ 'a' => 1 })).to eq('{"a":1}') | ||
| end | ||
| end | ||
|
|
||
| describe '.parse' do | ||
| context 'with allow_duplicate_keys: true (default)' do | ||
| it 'uses the last value for duplicate keys' do | ||
| result = described_class.parse('{"a":1,"a":2}') | ||
| expect(result['a']).to eq(2) | ||
| end | ||
|
|
||
| it 'parses valid JSON without duplicates' do | ||
| result = described_class.parse('{"a":1,"b":2}') | ||
| expect(result).to eq({ 'a' => 1, 'b' => 2 }) | ||
| end | ||
| end | ||
|
|
||
| context 'with allow_duplicate_keys: false' do | ||
| it 'raises DuplicateKeyError for duplicate keys' do | ||
| expect do | ||
| described_class.parse('{"a":1,"a":2}', allow_duplicate_keys: false) | ||
| end.to raise_error(JWT::DuplicateKeyError, /duplicate key/) | ||
| end | ||
|
|
||
| it 'parses valid JSON without duplicates' do | ||
| result = described_class.parse('{"a":1,"b":2}', allow_duplicate_keys: false) | ||
| expect(result).to eq({ 'a' => 1, 'b' => 2 }) | ||
| end | ||
|
|
||
| it 'detects duplicates in nested objects' do | ||
| json = '{"outer":{"inner":1,"inner":2}}' | ||
| expect do | ||
| described_class.parse(json, allow_duplicate_keys: false) | ||
| end.to raise_error(JWT::DuplicateKeyError, /duplicate key/) | ||
| end | ||
|
|
||
| it 'allows same key in different objects' do | ||
| json = '{"obj1":{"a":1},"obj2":{"a":2}}' | ||
| result = described_class.parse(json, allow_duplicate_keys: false) | ||
| expect(result['obj1']['a']).to eq(1) | ||
| expect(result['obj2']['a']).to eq(2) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MEMO: We added json gem >= 2.13.0 to the dependencies, but this version requires Ruby 2.7+. Since ruby-jwt has required_ruby_version = ‘>= 2.5’, dependency resolution fails during bundle install in a Ruby 2.5 environment.