Skip to content

Commit

Permalink
Handle empty string as token value
Browse files Browse the repository at this point in the history
If the token is the empty string we try to pass `nil` to `Base64.url_decode`,
which always expects a string.

This ensures we always pass a string to avoid an unexpected error.
  • Loading branch information
ragalie authored and anakinj committed Dec 13, 2024
1 parent cc0a876 commit b006395
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Deprecation warnings for deprecated methods and classes [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
- Improved documentation for public apis [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
- Use correct methods when raising error during signing/verification with EdDSA [#633](https://github.com/jwt/ruby-jwt/pull/633)
- Fix JWT::EncodedToken behavior with empty string as token [#640](https://github.com/jwt/ruby-jwt/pull/640) ([@ragalie](https://github.com/ragalie))
- Your contribution here

## [v2.9.3](https://github.com/jwt/ruby-jwt/tree/v2.9.3) (2024-10-03)
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt/encoded_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def unencoded_payload?
end

def parse_and_decode(segment)
parse(::JWT::Base64.url_decode(segment))
parse(::JWT::Base64.url_decode(segment || ''))
end

def parse_unencoded(segment)
Expand Down
16 changes: 16 additions & 0 deletions spec/jwt/encoded_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,26 @@
expect(token.payload).to eq({ 'foo' => 'bar' })
end
end

context 'when token is the empty string' do
let(:encoded_token) { '' }

it 'raises decode error' do
expect { token.payload }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
end
end
end

describe '#header' do
it { expect(token.header).to eq({ 'alg' => 'HS256' }) }

context 'when token is the empty string' do
let(:encoded_token) { '' }

it 'raises decode error' do
expect { token.header }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
end
end
end

describe '#signature' do
Expand Down

0 comments on commit b006395

Please sign in to comment.