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

Add support for RFC7797 unencoded payloads #561

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Metrics/AbcSize:
Max: 25

Metrics/ClassLength:
Max: 121
Max: 140

Metrics/ModuleLength:
Max: 100
Expand Down
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,72 @@ jwk_hash = jwk.export
thumbprint_as_the_kid = jwk_hash[:kid]
```

### Unencoded and Detached Payloads

#### Unencoded Payloads

To generate a JWT with an unencoded payload, you may use the `b64` header set to false as described by RFC 7797. When you do this, the `crit` header will be added if it doesn't already exist, and the `b64` value will be appended to it.

```ruby
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
public_key = private_key.verify_key
token = JWT.encode payload, private_key, 'ED25519', { b64: false }

# eyJiNjQiOmZhbHNlLCJhbGciOiJFRDI1NTE5IiwiY3JpdCI6WyJiNjQiXX0.{\"data\":\"test\"}.RL6jDz7h_fbQQds1x_ABOVE_dp646ZIbzvBB_DlixrTTMAiG7k0q4wH8dpcQ7KUeGgqI0tqj7B4JG_jTwM6fCg
puts token

decoded_token = JWT.decode token, public_key, true, { algorithm: 'ED25519' }
# Array
# [
# {"data"=>"test"}, # payload
# {"b64"=>false, "alg"=>"ED25519", "crit"=>["b64"]} # header
# ]
```

It is extremely important that one take great care when using unencoded payloads, as the payload must be url safe if it is intended to be transmitted, etc. Also, because `.` is used to delineate between JWT segments, the payload must not have any `.` characters. If the paylod contains `.` then an `InvalidUnencodedPayload` error is raised.

For the above reasons, detached payloads are often used in combination with unencoded payloads.

#### Detached Payloads

To generate a JWT with a detached payload, you must call `encode_detached` instead of `encode`. Then, when decoding and verifying the token, you must pass the `payload` option with the value of the detached payload before encoding.

```ruby
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
public_key = private_key.verify_key
token = JWT.encode_detached payload, private_key, 'ED25519'

# eyJhbGciOiJFRDI1NTE5In0..6xIztXyOupskddGA_RvKU76V9b2dCQUYhoZEVFnRimJoPYIzZ2Fm47CWw8k2NTCNpgfAuxg9OXjaiVK7MvrbCQ
puts token

decoded_token = JWT.decode token, public_key, true, { algorithm: 'ED25519', payload: payload }
# Array
# [
# {"test"=>"data"}, # payload
# {"alg"=>"ED25519"} # header
# ]
```

#### Combining Unencoded and Detached Payload Support

Unencoded and detached payloads are often used hand in hand, such as in proof signatures of Verifiable Credentials. You may use the `b64` header in combination with `encode_detached` to combine both features.

```ruby
private_key = RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF')
Copy link
Member

Choose a reason for hiding this comment

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

The example could be a more simple one than one that required the rbnacl dependency.

public_key = private_key.verify_key
token = JWT.encode_detached payload, private_key, 'ED25519', { b64: false }

# eyJiNjQiOmZhbHNlLCJhbGciOiJFRDI1NTE5IiwiY3JpdCI6WyJiNjQiXX0..RL6jDz7h_fbQQds1x_ABOVE_dp646ZIbzvBB_DlixrTTMAiG7k0q4wH8dpcQ7KUeGgqI0tqj7B4JG_jTwM6fCg
puts token

decoded_token = JWT.decode token, public_key, true, { algorithm: 'ED25519', payload: payload }
# Array
# [
# {"data"=>"test"}, # payload
# {"b64"=>false, "alg"=>"ED25519", "crit"=>["b64"]} # header
# ]
```

# Development and Tests

We depend on [Bundler](http://rubygems.org/gems/bundler) for defining gemspec and performing releases to rubygems.org, which can be done with
Expand Down
11 changes: 10 additions & 1 deletion lib/jwt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ def encode(payload, key, algorithm = 'HS256', header_fields = {})
Encode.new(payload: payload,
key: key,
algorithm: algorithm,
headers: header_fields).segments
headers: header_fields,
detached: false).segments

Choose a reason for hiding this comment

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

Maybe we can set detached as default false

end

def encode_detached(payload, key, algorithm = 'HS256', header_fields = {})
Encode.new(payload: payload,
key: key,
algorithm: algorithm,
headers: header_fields,
detached: true).segments
end

def decode(jwt, key = nil, verify = true, options = {}, &keyfinder) # rubocop:disable Style/OptionalBooleanParameter
Expand Down
29 changes: 25 additions & 4 deletions lib/jwt/decode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(jwt, key, verify, options, &keyfinder)

@jwt = jwt
@key = key
@detached_payload = options[:payload]
@options = options
@segments = jwt.split('.')
@verify = verify
Expand Down Expand Up @@ -152,15 +153,35 @@ def header
end

def payload
@payload ||= parse_and_decode @segments[1]
@payload ||= parse_and_decode(encoded_payload, decode: decode_payload?)
end

def encoded_payload
payload = encoded_detached_payload if !@detached_payload.nil? && @segments[1].empty?
Copy link
Member

Choose a reason for hiding this comment

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

I think it would make sense to raise an error if a payload is given and the token also contains a payload, instead of just ignoring the given payload.

payload ||= @segments[1]
payload
end

def encoded_detached_payload
payload ||= ::JWT::Base64.url_encode(JWT::JSON.generate(@detached_payload)) if decode_payload?
payload ||= @detached_payload.to_json
payload
end

def decode_payload?
header['b64'].nil? || !!header['b64']
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this is a bit problematic to change behavior because of user-input. Would it be too much of an inconvenience to have the caller decide to decode or not?

end

def signing_input
@segments.first(2).join('.')
[@segments[0], encoded_payload].join('.')
end

def parse_and_decode(segment)
JWT::JSON.parse(::JWT::Base64.url_decode(segment))
def parse_and_decode(segment, decode: true)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to avoid the parameter if the caller would just call parse and decode in separate?

if decode
JWT::JSON.parse(::JWT::Base64.url_decode(segment))
else

Choose a reason for hiding this comment

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

We can avoid else statement with a guard clause.

JWT::JSON.parse(segment)
end
rescue ::JSON::ParserError
raise JWT::DecodeError, 'Invalid segment encoding'
end
Expand Down
31 changes: 29 additions & 2 deletions lib/jwt/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,24 @@ def initialize(options)
@algorithm = resolve_algorithm(options[:algorithm])
@headers = options[:headers].transform_keys(&:to_s)
@headers[ALG_KEY] = @algorithm.alg
@detached = options[:detached]

Choose a reason for hiding this comment

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

Here we can set as default false

@detached = options[:detached] || false


# add b64 claim to crit as per RFC7797 proposed standard
unless encode_payload?
@headers['crit'] ||= []
@headers['crit'] << 'b64' unless @headers['crit'].include?('b64')
end
end

def segments
validate_claims!
combine(encoded_header_and_payload, encoded_signature)

parts = []
parts << encoded_header
parts << (@detached ? '' : encoded_payload)
parts << encoded_signature

combine(*parts)
end

private
Expand Down Expand Up @@ -51,7 +64,21 @@ def encode_header
end

def encode_payload
encode_data(@payload)
# if b64 header is present and false, do not encode payload as per RFC7797 proposed standard
Copy link
Member

@anakinj anakinj Jun 16, 2024

Choose a reason for hiding this comment

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

Would it make sense to not rely on header values but instead have an explicit parameter that controls the behavior of both header values and payload encoding?

encode_payload? ? encode_data(@payload) : prepare_unencoded_payload
end

def encode_payload?
# if b64 header is left out, default to true as per RFC7797 proposed standard
@headers['b64'].nil? || !!@headers['b64']
end

def prepare_unencoded_payload
json = @payload.to_json

raise(JWT::InvalidUnencodedPayload, 'An unencoded payload cannot contain period/dot characters (i.e. ".").') if json.include?('.')

json
end

def signature
Expand Down
1 change: 1 addition & 0 deletions lib/jwt/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class EncodeError < StandardError; end
class DecodeError < StandardError; end
class RequiredDependencyError < StandardError; end

class InvalidUnencodedPayload < EncodeError; end
class VerificationError < DecodeError; end
class ExpiredSignature < DecodeError; end
class IncorrectAlgorithm < DecodeError; end
Expand Down
56 changes: 56 additions & 0 deletions spec/jwt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

RSpec.describe JWT do
let(:payload) { { 'user_id' => '[email protected]' } }
let(:unencoded_payload) { { 'user_id' => 'safe_value' } }
let(:unencoded_payload_unsafe) { { 'user_id' => 'unsafe.value' } }

let :data do
data = {
Expand All @@ -24,6 +26,9 @@
'ES256K_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256k-public.pem'))),
'NONE' => 'eyJhbGciOiJub25lIn0.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.',
'HS256' => 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.kWOVtIOpWcG7JnyJG0qOkTDbOy636XrrQhMm_8JrRQ8',
'HS256_unencoded_payload' => 'eyJiNjQiOmZhbHNlLCJhbGciOiJIUzI1NiIsImNyaXQiOlsiYjY0Il19.{"user_id":"safe_value"}.eW4NSHANyJpL6ivfFut7a5CM5lpaif8vEQYr-CRzrUc',
'HS256_detached_payload' => 'eyJhbGciOiJIUzI1NiJ9..oDy7wJe4wR3YcvGx5EmVm42H68g3L8nFfKnx3yeH25o',
'HS256_unencoded_detached_payload' => 'eyJiNjQiOmZhbHNlLCJhbGciOiJIUzI1NiIsImNyaXQiOlsiYjY0Il19..eW4NSHANyJpL6ivfFut7a5CM5lpaif8vEQYr-CRzrUc',
'HS512256' => 'eyJhbGciOiJIUzUxMjI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.Ds_4ibvf7z4QOBoKntEjDfthy3WJ-3rKMspTEcHE2bA',
'HS384' => 'eyJhbGciOiJIUzM4NCJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.VuV4j4A1HKhWxCNzEcwc9qVF3frrEu-BRLzvYPkbWO0LENRGy5dOiBQ34remM3XH',
'HS512' => 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.8zNtCBTJIZTHpZ-BkhR-6sZY1K85Nm5YCKqV3AxRdsBJDt_RR-REH2db4T3Y0uQwNknhrCnZGvhNHrvhDwV1kA',
Expand Down Expand Up @@ -933,4 +938,55 @@ def valid_alg?(alg)
end
end
end

context 'when payload is not encoded' do
it 'should generate a valid token' do
token = JWT.encode unencoded_payload, data[:secret], 'HS256', { b64: false }

expect(token).to eq data['HS256_unencoded_payload']
end

it 'should decode a valid token' do
jwt_payload, header = JWT.decode data['HS256_unencoded_payload'], data[:secret], true, algorithm: 'HS256'

expect(header['alg']).to eq 'HS256'
expect(jwt_payload).to eq unencoded_payload
end

it 'should raise error when payload is unsafe for decoding' do
expect do
JWT.encode unencoded_payload_unsafe, 'secret', 'HS256', { b64: false }
end.to raise_error JWT::InvalidUnencodedPayload
end
end

context 'when payload is detached' do
it 'should generate a valid token' do
token = JWT.encode_detached unencoded_payload, data[:secret], 'HS256'

expect(token).to eq data['HS256_detached_payload']
end

it 'should decode a valid token' do
jwt_payload, header = JWT.decode data['HS256_detached_payload'], data[:secret], true, algorithm: 'HS256', payload: unencoded_payload

expect(header['alg']).to eq 'HS256'
expect(jwt_payload).to eq unencoded_payload
end
end

context 'when payload is unencoded and detached' do
it 'should generate a valid token' do
token = JWT.encode_detached unencoded_payload, data[:secret], 'HS256', { b64: false }

expect(token).to eq data['HS256_unencoded_detached_payload']
end

it 'should decode a valid token' do
jwt_payload, header = JWT.decode data['HS256_unencoded_detached_payload'], data[:secret], true, algorithm: 'HS256', payload: unencoded_payload

expect(header['alg']).to eq 'HS256'
expect(jwt_payload).to eq unencoded_payload
end
end
end