Skip to content

On Keys and Algorithms #478

@bellebaum

Description

@bellebaum

This is a sort of "Lessons Learned" combined with a few suggestions this project might want to consider.

Story time

I have a server that has to verify and decode JWTs from several sources using several algorithms.
Looking at the README for this gem, it strongly suggests hard-coding a parameter called algorithm for the decode function. Since I do not know the algorithm in advance, I took it from the JWT, and ran some tests on it to ensure that only an expected subset of algorithms are considered valid.

But herein lies the problem: What I should have done is take a look at the code and find that algorithm behaves more like allowed_algorithms and I can specify several of those in an array.
I am not an expert in JWTs. There is no way for me to be sure that I have covered all possible attack vectors related to alg.

Suggestion I

Document the possibility of specifying several algorithms in the README in "Algorithms and Usage", possibly with an example.
Ideally, change the name of the variable algorithm to allowed_algorithms.
You can keep the old variable as an alias to maintain backwards compatibility.

Suggestion II

The article linked in the README also suggests the following:

Most (hopefully all?) implementations now have a basic check to prevent this attack: if a secret key was provided, then token verification will fail for tokens using the none algorithm. This is a good idea, but it doesn't solve the underlying problem: attackers control the choice of algorithm. Let's keep digging.

They also follow this up with an example of how one key can be interpreted as either RSA or HMAC relying on the fact that both were encoded as a string.

Since this library does not have such problems (An RSA key is of type OpenSSL::PKEY, while an HMAC shared secret is not, see also #184 ), we could even go one step further:

One could check the provided key against the expected algorithms. I agree that this is not enough in many situations, but I also agree with the article that it does not hurt.

#400 goes one step further by not requiring algorithm at all if the type of key can be resolved. To me it seems like that person has fallen for the same misunderstanding I have, that one cannot specify multiple algorithms.

Conclusion

There is nothing wrong with this library per se, but a few tweaks could nudge people to intuitively use it more secure way.
Let's have a discussion :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions