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

Fail on encode and decode of bad JWS header values #174

Merged
merged 2 commits into from
Jul 23, 2015

Conversation

gabrielg
Copy link
Contributor

The JWS spec states that if kid is present then it MUST be a string. Currently, the library allows silent creation of invalid JWS (and thus, JWT), as it allows any type for kid.

Allowing arbitrary types is both against the spec, and has some potential security ramifications that I've seen crop up. Since header values can be used pre-verification (and in the case of kid, might be required to verify), some more strictness around them and ensuring they meet the spec makes sense.

The JWS spec:

https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#section-4.1.4

States that if `kid` is present then it **MUST** be a string.
Currently, the library allows silent creation of invalid JWS (and
thus, JWT), as it allows any type for `kid`. This commit adds checks
to help ensure output meets the spec.

* Add jwt.api_jws.PyJWS._validate_headers for validating JWS headers
  on encode and decode
* Add tests
@jpadilla
Copy link
Owner

@gabrielg good catch. @mark-adams this looks good to me.


def _validate_kid(self, kid):
if not isinstance(kid, string_types):
raise TypeError('Key ID header parameter must be a string')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should probably use InvalidTokenError similar to other validation errors.

@mark-adams
Copy link
Contributor

👍 Once you changeover that exception to be a InvalidTokenError, I'm ready to merge this.

The only reason we didn't have similar validation in here for that already was that we are preparing full JWK support as part of 2.0, but this seems like something reasonable to add now.

Thanks for contributing!

@gabrielg
Copy link
Contributor Author

Cool, changed TypeError over to InvalidTokenError. Thank you!

mark-adams added a commit that referenced this pull request Jul 23, 2015
Fail on encode and decode of bad JWS header values
@mark-adams mark-adams merged commit c5ee34e into jpadilla:master Jul 23, 2015
@jpadilla
Copy link
Owner

@mark-adams should we release this already?

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.

3 participants