这是indexloc提供的服务,不要输入任何密码
Skip to content

Support JWTs with no kid in the header #1072

@couling

Description

@couling

Summary.

I've hit a problem in the wild with JWTs being signed without a kid and the corresponding JWK also not containing a kid. This appears to be a valid use case:

RFC 7517 4.5

The "kid" (key ID) parameter is used to match a specific key. This
is used, for instance, to choose among a set of keys within a JWK Set
during key rollover.
...
Use of this member is OPTIONAL.

IE it's only functionally necessary to include one when there is some ambiguity around which key. Eg: during key rollover. At other times, when there is only one key, neither the JWT nor the JWK need to include one.

It appears to be valid to match a token with no kid against any signing key on a JWK. There is no specification for what to do in the case of ambiguity that I can see, but it seems reasonable to fail validation in that situation.

What you expected.

If there is not kid in the token header and there is exactly one signing key on the JWK then the following code should still work:

jwks_client = jwt.jwks_client.PyJWKClient(config.jwk_url)
signing_key = jwks_client.get_signing_key_from_jwt(token)
decoded = jwt.decode_complete(token, signing_key)

This should:

  • Fetch the JWK
  • Filter a list of signing keys (including keys with no kid)
  • If the token contains a kid it should match on the kid
  • If the token does not contain a kid it verify the list of signing keys contains just one key and verify against that.

System Information

$ python -m jwt.help
{
  "cryptography": {
    "version": "45.0.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.12.0"
  },
  "platform": {
    "release": "24.5.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "2.10.1"
  }
}

This appears to be a related to #857

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions