-
-
Notifications
You must be signed in to change notification settings - Fork 694
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
Malformed JWTs due to different signing algorithms in headers
and algorithm
parameters
#659
Comments
I just bumped into a related (usability) issue: using a non-default algorithm with a custom header. (I was doing OIDC asymmetric signatures.) The key = ...
header = {"typ": "JWT", "alg": "PS256", "kid": "my-key-id"}
claims = {"foo": "bar"}
jws = jwt.encode(payload=claims, key=key, algorithm=header["alg"], headers=header) Which wouldn't be needed if jws = jwt.encode(payload=claims, key=key, headers=header) It's cleaner code (for the user) and saner behaviour. |
Hi, @De117 @ashutosh1206. I've sent a PR to fix this issue. Could you check it? My PR supports the case mentioned by @De117. key = ...
header = {"typ": "JWT", "alg": "PS256", "kid": "my-key-id"}
claims = {"foo": "bar"}
jws = jwt.encode(payload=claims, key=key, headers=header) On the other hand, in the following @ashutosh1206 's case, alg=none will be preferred and returns error because key is set. import jwt
payload = {"test": "payload"}
headers = {"typ": "JWT", "alg": "none"}
jwt.encode(payload = payload, key = "secret", algorithm = "HS256", headers = headers) In any case, I think the consistent rule that |
@dajiaji I had something slightly different in mind ("prefer |
Generating JWTs (
encode()
) withalg
key set inheaders
parameter along with a differentalgorithm
parameter value results in malformed JWTs.Example
headers
andalgorithm
values that will result in malformed tokens:Expected Result
A token signed with let's say HS256 algorithm should always contain
alg
value set as HS256 in the JWT header.Actual Result
Tokens can be signed using an algorithm x (eg. HS256) with token header containing a different
alg
valueReproduction Steps
Running the snippet above will give:
eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJ0ZXN0IjoicGF5bG9hZCJ9.PySJLJ2Js8Z1K0acpZrgOhzHp0sea_N5rrNX1L_FJis
. Header + payload in this case is signed using HS256, but the header value in the token is:{"typ":"JWT","alg":"none"}
Similarly,
will result in a token signed using HS256, with the token header being
{"typ":"JWT","alg":"RS256"}
System Information
Root cause
algorithm
(L#109 api_jws.py) variable is being used for signing, butheader
dict (L#93 api_jws.py) is being overwritten withheaders
method parameter in L#97 api_jws.py leading toalg
value being overwritten to the value present in the method parameterheaders
.I tried coming up with ways in which this bug could be abused by an adversary, but couldn't find any. And so, I don't think this qualifies as a security issue.
The text was updated successfully, but these errors were encountered: