Fix JSONDecodeError due to Improper Handling of Nested JSON Strings in JWT Payloads #93
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses an issue in the
decode_base64
function where nested JSON strings within JWT tokens were being corrupted due to incorrect decoding of base64 strings that are not URL-safe. This corruption occurred because the original decoding was not handling certain characters properly, leading to JSON decoding errors when attempting to parse these strings back into JSON objects.Changes
base64.b64decode
withbase64.urlsafe_b64decode
to correctly handle base64 strings that include URL-safe characters.Previous Behavior
Previously, when JWT tokens contained nested JSON strings encoded in base64, the
decode_base64
function would sometimes corrupt these strings. This was particularly apparent when characters like '+' and '/' were included in the base64 string, which were not correctly handled by the standardbase64.b64decode
. The JSON parser would then fail to parse the string due to misplaced or altered characters.For example, decoding a JWT payload with nested JSON would lead to a
JSONDecodeError
:New Behavior
With the new changes, the decode_base64 function correctly decodes the base64 string without corrupting the JSON structure:
This fix ensures that JWT tokens with nested JSON can be handled without errors, improving the robustness of the authentication handling in applications using httpx-auth.
Additional Notes
This update is crucial for applications that depend on precise and error-free handling of JWT tokens, especially in scenarios involving complex data structures within the token payloads.
Closes #92