Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
Different servers generate different JWT tokens using the same parameters

I'm using python-jose's JWT implementation to generate JWT tokens for authentication purposes.

We're running our backend in a Docker container on Kubernetes and sometimes, when we have multiple pods, we get different tokens for the same claims, secret and algorithm. I've also had this happen on a single container on my development environment when touching my index.wsgi script.

Pod 1:

>>> jwt.encode({'key': 'value'}, 'secret', algorithm='HS256')
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWx1ZSJ9.FG-8UppwHaFp1LgRYQQeS6EDQF7_6-bMFegNucHjmWg'

Pod 2:

>>> jwt.encode({'key': 'value'}, 'secret', algorithm='HS256')
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkiOiJ2YWx1ZSJ9.JPIDicqvQ6GAh14yE2yZ3wnZQ0LiLNTTRDtJgLZcn98'

I took a deep dive into the code to see what could be causing this and didn't find anything incriminating. In a nutshell, here's what the code does:

  1. Do a json.dumps of the algorithm header ({'typ': 'JWT', 'alg': 'HS256'}) and encode it as Base64, removing any ='s
  2. Do a json.dumps of the payload ({'key': 'value'}) and encode it as Base64, removing any ='s
  3. Sign encoded_header.encoded_payload using HMAC256 with the secret key and encode it as Base64, again removing any ='s
  4. Concatenate the signature to the previous string, resulting in encoded_header.encoded_payload.encoded_signature

At this point, I have no idea what is causing this. I'm suspecting a bug of some sort in the HMAC or SHA256 implementation of Python, but that seems rather unlikely... Any clues?

Note: we've successfully reproduced the bug with pyjwt, which was the base for python-jose.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This occurs because Python dictionaries are unordered. If you decode the two JWTs, you will see that the header portion is ordered differently for each token.

{
    "typ": "JWT",
    "alg": "HS256"
}

and

{
    "alg": "HS256",
    "typ": "JWT"
}

This causes the base64 encoded header to be different, which will in turn cause the signature to be different.

That said, both of those are valid tokens for the exact same claim set, and both should verify successfully. There is nothing in the JWT spec that dictates that an equivalent claim set should result in equivalent JWT output.

Note: I am the author of the python-jose library.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda