auth0 / express-jwt

connect/express middleware that validates a JsonWebToken (JWT) and set the req.user with the attributes
MIT License
4.49k stars 444 forks source link

Modify tests to actually exercise wrong signature case by removing base64 padding chars from test token #319

Closed loucadufault closed 1 year ago

loucadufault commented 1 year ago

Description

The failure tests 'should throw error when signature is wrong' test case was incorrectly building a test input in two ways:

  1. The payload content was encoded as a base64 string, which in this case added special padding chars ('='). These chars do not comply with the JWT specification, and are expected to not be present by downstreams that perform the token validation and verification. This change fixes the test token to remove the padding chars, the same way this is done in the downstream (code pointer).
  2. Also, the payload content was being built from a string literal, meant to represent a stringified POJO (aka a JSON string), which was ill-formed (missing quotes). This change fixes the payload to be well-formed.

Additionally, the assertion of the error message was incorrect, this change fixes it to assert the message as "invalid signature".

The reason why 1. was not caught is that the test token technically still failed validation, but at the earlier stage of checking format validity, and was caught as the same error (with a different message). The reason why 2. was not caught was that the token payload was never parsed, as it failed at an earlier step due to 1.

References

See also https://github.com/auth0/node-jws/issues/49 for details about forbidding '=' base64 padding characters in complying with specification

Testing

Reviewers should test that this test case actually exercises the token signature verification, by ensuring that the isValidJWS() function from the node-jws module dependency (code pointer) returns true. Namely, the regex test against var JWS_REGEX = /^[a-zA-Z0-9\-_]+?\.[a-zA-Z0-9\-_]+?\.([a-zA-Z0-9\-_]+)?$/; should pass.

Checklist

jfromaniello commented 1 year ago

Make sense. Thank you