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

Handle empty string as token value #640

Merged
merged 1 commit into from
Dec 13, 2024
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 @@
- 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
Loading