Repository navigation
Conversation
62bbb58 to
952d95f
Compare
|
Thanks for the addition @ydah, looks interesting. I need to take a look at this with a fresh mind. But its on my todo list. |
029b46f to
3850bd0
Compare
| # encoded_token.verify_signature!(algorithm: 'HS256', key: 'secret') | ||
| # encoded_token.payload # => {'pay' => 'load'} | ||
| class EncodedToken | ||
| class EncodedToken # rubocop:disable Metrics/ClassLength |
There was a problem hiding this comment.
Would be super nice if we could avoid polluting the EncodedToken with the nested token logic. Could we have a pair: NestedToken and EncodedNestedToken
There was a problem hiding this comment.
I vibed together a rough sketch of what I'm visualizing: #721
|
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. |
…2, 7.1, 7.2, and Appendix A.2. Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2. A Nested JWT is a JWT used as the payload of another JWT, allowing multiple layers of signing with different keys/algorithms.
|
@anakinj Thanks for putting together #721. I’ve updated my branch to follow the split you suggested: One difference from your sketch is that I kept |
anakinj
left a comment
There was a problem hiding this comment.
Thanks for reworking this along the lines of #721, @ydah. The core holds up well:
- each layer is verified with its own algorithm and key;
- stripping the outer layer is caught, because the number of layers has to match the key configurations;
noneis rejected on every layer;- claims are checked on the innermost token;
- the existing APIs don't change, and
JWT.decodeon a nested token still raisesMalformedTokenError.
A few things to change before it goes in.
1. Verify each layer before decoding the next one (lib/jwt/encoded_nested_token.rb:49-63)
verify_signatures! calls unwrap_all first. That decodes up to 10 layers, and counts them using cty headers nobody has verified, before any signature is checked. RFC 7519 section 7.2 verifies the JWS first and only then looks at cty. For a forged token, the error then depends on what the sender put inside it, and the count error shows how many key configurations the server expects:
outer signed with the wrong key, malformed inner layer: JWT::MalformedTokenError: Token header not a JSON object
outer signed with the wrong key, one key config given: JWT::DecodeError: Expected 2 key configurations, got 1
Looping over the key configurations would fix this:
- verify the current layer;
- decode the next layer only if this one's
ctysays so; - raise if the layers and the configurations run out at different times.
That also limits the depth to the number of configurations. On your question about verify!(keys: [...]): one entry per layer fits this well.
2. Use the current error classes (lines 27, 52, 70, 86)
These raise the bare JWT::DecodeError, which is deprecated. Nothing on main raises it any more, and an application that rescues JWT::TokenError won't catch it. Suggested classes:
MalformedTokenErrorfor the depth, empty payload and layer count errors;TokenErrorfor "verify signatures first", as inEncodedToken#payload.
3. Make NestedToken behave like Token (lib/jwt/nested_token.rb)
#jwtbeforesign!returns the inner token, whereToken#jwtraisesToken is not signed. An issuer who forgetssign!sends out the inner token unwrapped.- Calling
sign!twice adds another layer, whereTokenraisesToken already signed. - It repeats the header and signing code from
Token#sign!, and has noheader,signatureorencoded_*readers.
A subclass of Token, as in #721, would get the guards and readers for free. It would set the cty header and override encoded_payload, so the inner token is base64url-encoded without JSON encoding.
4. Treat cty: application/jwt as nested (line 81)
RFC 7515 section 4.1.10 says a recipient treats a cty without a / as if application/ were in front of it, so application/jwt and JWT mean the same. Today application/jwt isn't seen as nested. It fails safely, but tokens from issuers that send it won't verify.
5. Drop the b64: false branch (line 88)
RFC 7797 doesn't allow a . in an unencoded payload in the compact form, and a JWT always contains one, so a valid token can't reach this branch. It also skips the crit: ["b64"] check that EncodedToken does. Always base64url-decoding the payload, or rejecting b64: false on a cty: JWT layer, would be simpler.
6. Check the per-layer options (lines 54-59)
- Unknown keys are ignored. A
claims:in a layer's hash is accepted and does nothing, so a caller can think they checked claims on that layer. - A typo like
algorithms:givesNo algorithm provided. - A Hash instead of an Array raises a plain
TypeError.
Passing the hash on with token.verify_signature!(**key_config) would make unknown keys raise, and keys could be checked to be an Array.
7. Tests
- No spec passes
claims:. With claim verification removed fromverify!, all the new specs still pass. The spec atencoded_nested_token_spec.rb:70says it checks claims, but its payload has noexp. integration/nested_jwt_spec.rb:45builds the outer token withJWT::Token.new(payload: inner_jwt). That JSON-encodes the inner token, so the result isn't a valid nested token. The spec only passes because it checksnested?.- Cases worth adding:
- an expired inner token, with
claims:as an Array and as a Hash; noneon a layer;- the outer layer stripped off;
- the depth limit through
verify!; application/jwt;- a bad outer signature around a malformed inner token.
- an expired inner token, with
8. CHANGELOG
release-please writes the CHANGELOG from the PR title (see CONTRIBUTING.md), so the edit can go.
Smaller things:
- "Expected 1 key configurations, got 2" reads the wrong way round: the first number is the token's layer count.
max_depthcan be given tounwrap_allbut not toverify!.MAX_DEPTHandCTY_JWTcould beprivate_constant, as inEncodedToken.- The README could say that
inner_tokenandunwrap_allreturn unverified tokens, and that only JWS layers are supported. - The "(NEST-01)" tags in the spec names look like leftovers.
Posted by Claude on behalf of @anakinj.
Description
Implements Nested JWT functionality as defined in RFC 7519 Section 5.2, 7.1, 7.2, and Appendix A.2.
A Nested JWT is a JWT used as the payload of another JWT, allowing multiple layers of signing with different keys/algorithms.
see: https://datatracker.ietf.org/doc/html/rfc7519#section-5.2
see: https://datatracker.ietf.org/doc/html/rfc7519#section-7.1
see: https://datatracker.ietf.org/doc/html/rfc7519#section-7.2
Checklist
Before the PR can be merged be sure the following are checked: