You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before filing an issue, please read the contents of CONTRIBUTING.md, and follow its instructions.
Describe the bug
When attempting to process a JWT in JWS Compact format that does not have the "typ" header set and contains trailing data, jwt.Parse attempts to decode the token as uncompacted JSON, and produces an error about invalid initial character that misrepresents the actual issue.
2022/11/22 13:36:37 failed to parse token: invalid character 'e' looking for beginning of value
The real problem is not with the initial 'e' in the payload, but with the trailing ".trailing-rubbish" after the signature. The reason this occurs is that parse() inside jwt.go
Initially calls jwx.GuessFormat() which returns jwx.UnknownFormat because the input (a) doesn't start with a '{', and (b) doesn't have either two or four '.'-separated parts. This makes sense. However,
It then calls verifyJWS() assuming that the unknown format is in fact a JWS, and discards the returned base64-decoded JSON, only checking the error. The error is nil as the supplied data is in fact a valid JWS (with additional trailing data, which verifyJWS() ignores). Contrast this to the jwx.JWS, rather than jwx.UnknownFormat path, where verifyJWS() is also called, but the returned based64-decode JSON is reassigned as payload.
Finally, json.Unmarshal is called. Since the output of verifyJWS has been discarded, and the payload is still base64-encoded, and this fails with the error reported above.
Expected Behavior
Since the supplied data has trailing garbage, and is not as a whole, a valid JWT, it makes sense that an attempt to parse it produces an error. However, the error itself is misleading, and can result in time being wasted in looking for the problem in the wrong place. As such any of the following alternative would be preferable:
Do not attempt to verify the unknown format and immediately report that as the error, without attempting to parse it.
Detect possible trailing data (possibly inside `jwx.GuessFormat(), or elsewhere) report that fact as an error (more work than the above, but results in a more informative error).
(less preferable) Update the unknown format branch inside parse() to behave analogously to the JWS branch and update the payload with the decode version if the call to verifyJWS succeeds. docs for jwt.Parse should mention this handling of trailing data.
Additional context
N/A
The text was updated successfully, but these errors were encountered:
Cool. I merged it, but please be aware that I won't be making a release for a few days at least, in order to make sure that nothing else broke or I missed anything. Please use the unreleased version until then. Thanks for the heads up
Contribution Guidelines
Before filing an issue, please read the contents of CONTRIBUTING.md, and follow its instructions.
Describe the bug
When attempting to process a JWT in JWS Compact format that does not have the "typ" header set and contains trailing data,
jwt.Parse
attempts to decode the token as uncompacted JSON, and produces an error about invalid initial character that misrepresents the actual issue.Please attach the output of
go version
To Reproduce / Expected behavior
For example:
Results in
The real problem is not with the initial
'e'
in the payload, but with the trailing".trailing-rubbish"
after the signature. The reason this occurs is thatparse()
insidejwt.go
jwx.GuessFormat()
which returnsjwx.UnknownFormat
because the input (a) doesn't start with a'{'
, and (b) doesn't have either two or four'.'
-separated parts. This makes sense. However,verifyJWS()
assuming that the unknown format is in fact a JWS, and discards the returned base64-decoded JSON, only checking the error. The error isnil
as the supplied data is in fact a valid JWS (with additional trailing data, whichverifyJWS()
ignores). Contrast this to thejwx.JWS
, rather thanjwx.UnknownFormat
path, whereverifyJWS()
is also called, but the returned based64-decode JSON is reassigned as payload.json.Unmarshal
is called. Since the output ofverifyJWS
has been discarded, and the payload is still base64-encoded, and this fails with the error reported above.Expected Behavior
Since the supplied data has trailing garbage, and is not as a whole, a valid JWT, it makes sense that an attempt to parse it produces an error. However, the error itself is misleading, and can result in time being wasted in looking for the problem in the wrong place. As such any of the following alternative would be preferable:
parse()
to behave analogously to the JWS branch and update the payload with the decode version if the call toverifyJWS
succeeds. docs forjwt.Parse
should mention this handling of trailing data.Additional context
N/A
The text was updated successfully, but these errors were encountered: