Skip to content

Commit b006395

Browse files
ragalieanakinj
authored andcommitted
Handle empty string as token value
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.
1 parent cc0a876 commit b006395

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Deprecation warnings for deprecated methods and classes [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
1818
- Improved documentation for public apis [#629](https://github.com/jwt/ruby-jwt/pull/629) ([@anakinj](https://github.com/anakinj))
1919
- Use correct methods when raising error during signing/verification with EdDSA [#633](https://github.com/jwt/ruby-jwt/pull/633)
20+
- Fix JWT::EncodedToken behavior with empty string as token [#640](https://github.com/jwt/ruby-jwt/pull/640) ([@ragalie](https://github.com/ragalie))
2021
- Your contribution here
2122

2223
## [v2.9.3](https://github.com/jwt/ruby-jwt/tree/v2.9.3) (2024-10-03)

lib/jwt/encoded_token.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def unencoded_payload?
124124
end
125125

126126
def parse_and_decode(segment)
127-
parse(::JWT::Base64.url_decode(segment))
127+
parse(::JWT::Base64.url_decode(segment || ''))
128128
end
129129

130130
def parse_unencoded(segment)

spec/jwt/encoded_token_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,26 @@
4040
expect(token.payload).to eq({ 'foo' => 'bar' })
4141
end
4242
end
43+
44+
context 'when token is the empty string' do
45+
let(:encoded_token) { '' }
46+
47+
it 'raises decode error' do
48+
expect { token.payload }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
49+
end
50+
end
4351
end
4452

4553
describe '#header' do
4654
it { expect(token.header).to eq({ 'alg' => 'HS256' }) }
55+
56+
context 'when token is the empty string' do
57+
let(:encoded_token) { '' }
58+
59+
it 'raises decode error' do
60+
expect { token.header }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
61+
end
62+
end
4763
end
4864

4965
describe '#signature' do

0 commit comments

Comments
 (0)