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

Fixed decoding of token when validation has multiple families of algorithms #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Wassasin
Copy link

@Wassasin Wassasin commented Nov 8, 2022

Currently if you have

        let mut validation = Validation::default();
        validation.algorithms = vec![
            Algorithm::HS256,
            Algorithm::HS384,
            Algorithm::HS512,
            Algorithm::RS256,
            Algorithm::RS384,
            Algorithm::RS512,
        ];

and try to validate a RS256 signed token, you will get a Error::InvalidAlgorithm due to the section:

        for alg in &validation.algorithms {
            if key.family != alg.family() {
                return Err(new_error(ErrorKind::InvalidAlgorithm));
            }
        }

This section should check whether the key is one of the accepted families. Currently it checks if an algorithm is accepted with a different family, and if so rejects the key.

@Keats
Copy link
Owner

Keats commented Nov 10, 2022

The intent there was to allow only 1 family for the validation but the types were not good enough. In the future, better types for each alg family will be used and the validation fn will require a specific family

@lmm-git
Copy link

lmm-git commented Mar 24, 2024

May I ask why the intent is to allow only one family? The docs state

    /// The validation will check that the `alg` of the header is contained
    /// in the ones provided and will error otherwise. Will error if it is empty.
    ///
    /// Defaults to `vec![Algorithm::HS256]`.

So I would expect that if I allow multiple algorithms (even from different families) this should be fine. But it fails during runtime with a generic error.

Therefore, I would really appreciate to either update the docs or merge this MR.

@Keats
Copy link
Owner

Keats commented Mar 25, 2024

We can update the docs to say it should only contain alg from the same family.

@lmm-git
Copy link

lmm-git commented Mar 25, 2024

But just to be sure: is there any reason on why allowing only one family makes sense?

For example, I have a use case where the library should verify tokens for their expiration and if they are signed off by a specific IdP. In this use-case it is not relevant (for me) which algorithm family the signing algorithm is belonging to, but I would like to allow some "secure enough" algorithms (which might be from different families. Actually, the algorithm depends on other services the tokens are getting used on later.

@Keats
Copy link
Owner

Keats commented Mar 25, 2024

You need to pass the https://docs.rs/jsonwebtoken/latest/jsonwebtoken/struct.DecodingKey.html to decode a token and to do that you need to know the family anyway. Also https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/ could be a concern maybe if we allow multiple families

@lmm-git
Copy link

lmm-git commented Mar 25, 2024

Fair point, but as the available algorithm family depends on the DecodingKey, wouldn't it make sense to select the algorithm according to the key? In most JWK the algorithm is specified anyways.

Another option would be to allow to pass JWKs directly to the validator and the validator can accept algorithms allowed in the single JWKs?

I am aware that these changes are breaking, but imho could improve the usability of this API and overall security, as for example I would assume that having projects to handle their JWKs manually like right now is much more error prone.

Apart from that a documentation change would be nice, this probably would have fixed the issue for me in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants