Repository navigation
Conversation
0838041 to
bf095f9
Compare
|
Overall I support this feature and think it should be the default behavior. My biggest concern is adding a lot of logic to this now it will stay and live in the gem in the future. Would almost just like to require |
1acb9e3 to
11450ac
Compare
| os: | ||
| - ubuntu-latest | ||
| ruby: | ||
| - "2.5" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Would like to support as many versions as possible, what about dynamically figure out if its possible to enable this feature or not.
There was a problem hiding this comment.
Indeed, I've restored support for 2.5 and 2.6.
|
I updated this PR. WDYT? |
905c978 to
ad53ab7
Compare
|
Sorry for the back and forth but im a bit starting to regret my suggestions to only allowing enabling it per instance. I wonder if it would still be easier for most users to enable this globally for the gem. Also setting the flag to true/false would allow disabling of the strictness when JSON 3.0 is out with the new default. What do you think: Also as mentioned earlier maybe do the refactorings in separate to keep the scope of this change on the new feature? |
| **Breaking changes:** | ||
| - Drop support for Ruby 2.6 and older [#713](https://github.com/jwt/ruby-jwt/pull/713) ([@ydah](https://github.com/ydah)) | ||
| - Bump minimum json gem version to 2.6 [#713](https://github.com/jwt/ruby-jwt/pull/713) ([@ydah](https://github.com/ydah)) | ||
|
|
There was a problem hiding this comment.
| **Breaking changes:** | |
| - Drop support for Ruby 2.6 and older [#713](https://github.com/jwt/ruby-jwt/pull/713) ([@ydah](https://github.com/ydah)) | |
| - Bump minimum json gem version to 2.6 [#713](https://github.com/jwt/ruby-jwt/pull/713) ([@ydah](https://github.com/ydah)) |
No breaking changes anymore, right?
|
This pull request has had no activity for 60 days, so it has been marked as stale. It will be closed in 14 days unless something happens. If you are still working on it, push a commit or leave a comment and it will stay open. |
Implements duplicate claim name detection as specified in RFC 7519 Section 4, which states: > The Claim Names within a JWT Claims Set MUST be unique; JWT parsers MUST either reject JWTs with duplicate Claim Names or use a JSON parser that returns only the lexically last duplicate member name. This feature allows users to reject JWTs that contain duplicate keys in the header or payload, which is recommended for security-sensitive applications to prevent claim confusion attacks.
…on a runtime feature check instead of a hard dependency on json >= 2.13.0
… single call with conditional options
…licate_keys! method
… SignatureVerifier classes to reduce class complexity
ad53ab7 to
63b4bcf
Compare
|
@anakinj Thanks, the three-state global setting makes sense. I’ll update I’ll also move the I’ve rebased onto the latest main and removed the outdated breaking-change entries from the changelog. |
anakinj
left a comment
There was a problem hiding this comment.
Thanks @ydah, the three-state setting and moving the refactor to its own PR sound good. Wrapping json's own allow_duplicate_key option is the right approach. A few things to keep in mind for the update, because the current head changes the default on json 3.x.
1. Don't pass the option unless the setting is set (lib/jwt/json.rb:13-17)
json 3.x rejects duplicate keys by default. So on main with json 3.2.0, JWT.decode and EncodedToken already reject a token that repeats a claim or header parameter. This PR always passes allow_duplicate_key: true on json 2.13 and later, so those tokens are accepted again:
json 3.2.0, main: repeated sub in the payload -> JWT::MalformedTokenError: Invalid segment encoding
json 3.2.0, PR: repeated sub in the payload -> accepted, the last value wins
On json 2.13 to 2.x it also hides json's warning that duplicate keys will raise in json 3.0. b80f390 passed the option only for false, and 63b4bcf changed that back. With the new setting, nil should leave the option out entirely.
2. Require a fixed json for false (lib/jwt/json.rb:27)
allow_duplicate_key: false is affected by CVE-2026-33210 (GHSA-3m6g-2423-7cp3) in json 2.14.0 to 2.19.1. It's fixed in 2.19.2, with backports in 2.17.1.2 and 2.15.2.1. The header is parsed before the signature is checked, so with strict mode on, anyone who can send a token reaches that code. When the setting is false, the version check should require a fixed release, not just 2.13.
3. Remove raise_on_duplicate_keys! (lib/jwt/encoded_token.rb:60-66)
The global setting replaces it, which also fixes a problem with it. It only resets the parser. If the header or payload has already been read, for example to look up kid, or the signature was verified first, the memoized values stay and duplicates get through. With the setting read in JWT::JSON.parse, both EncodedToken and JWT.decode follow it, and EncodedToken itself doesn't need to change. enforce_hmac_key_length in DecodeConfiguration shows how such a setting looks.
4. Error classes (lib/jwt/error.rb:84-87, lib/jwt/json.rb:19)
DuplicateKeyErrorshould be a subclass ofMalformedTokenError, which is what main raises for these tokens with json 3.x today.DecodeErroris deprecated.UnsupportedErroris a setup problem, not a bad token, so it belongs underJWT::Error. A more specific name would help too.e.message.include?('duplicate key')also matches other parse errors. On json 2.9.1, a malformed segment that contains the text "duplicate key" becomes aDuplicateKeyError, and the segment is echoed in the message.start_with?('duplicate key')and a fixed message avoid that.
5. Specs
spec/jwt/claims/duplicate_key_spec.rb:33, :61 and spec/jwt/json_spec.rb:12 expect the last value to win by default. That isn't true on main with json 3.x, so they should follow the json version once nil keeps json's default.
Cases worth adding:
- each value of the setting through
JWT.decodeandEncodedToken; b64: falseand detached payloads;- a json version without the option.
The spec tests EncodedToken, so spec/jwt/ fits better than spec/jwt/claims/.
Smaller things:
- CONTRIBUTING.md asks not to edit CHANGELOG.md.
- README.md says ruby-jwt uses the last value, which isn't true with json 3.x.
Posted by Claude on behalf of @anakinj.
Description
Implements duplicate claim name detection as specified in RFC 7519 Section 4, which states:
see: https://datatracker.ietf.org/doc/html/rfc7519#section-4
This feature allows users to reject JWTs that contain duplicate keys in the header or payload, which is recommended for security-sensitive applications to prevent claim confusion attacks.
Checklist
Before the PR can be merged be sure the following are checked: