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

JSONDecodeError due to Improper Handling of Nested JSON Strings in JWT Payloads #92

Open
pythrick opened this issue Apr 17, 2024 · 0 comments · May be fixed by #93
Open

JSONDecodeError due to Improper Handling of Nested JSON Strings in JWT Payloads #92

pythrick opened this issue Apr 17, 2024 · 0 comments · May be fixed by #93

Comments

@pythrick
Copy link

Description

There is an issue in the httpx-auth library where the decoding of base64-encoded JSON within JWT tokens corrupts JSON strings that contain nested JSON. This happens because the double quotes inside the nested JSON string are not correctly handled during the decoding process, leading to a failure when attempting to load the string back into a JSON object.

Steps to Reproduce

The issue can be reproduced with the following test case:

import jwt
import json
from httpx_auth._oauth2.tokens import decode_base64

def test_decode_base64_with_nested_json_string():
    # Encode a JSON inside the JWT
    dummy_token = jwt.encode({"data": json.dumps({"something": ["else"]})}, key="")
    header, body, signature = dummy_token.split(".")
    
    # Decode the body
    decoded_bytes = decode_base64(body)
    
    # Attempt to load JSON
    result = json.loads(decoded_bytes)
    assert result == {"data": '{"something": ["else"]}'}

Running this test results in a json.decoder.JSONDecodeError due to incorrect handling of the nested JSON string.

Expected Behavior

The decoded JSON string should be handled correctly, allowing for proper loading into a Python dictionary without JSON parsing errors.

Actual Behavior

The test raises the following error due to malformed JSON:

json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 12 (char 11)

This error is caused by the way double quotes inside the nested JSON are handled, which corrupts the JSON string during the base64 decoding step.

Environment

Python Version: 3.10.11
httpx-auth version: 0.22.0 (2024-03-02)

Additional Context

This issue impacts scenarios where JWT tokens contain nested JSON strings as part of their payload. A fix would likely involve adjusting the base64 decoding function to correctly handle nested JSON strings without corrupting them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant