| 1 |
From: arpitjain099 <[email protected]> |
| 2 |
Date: Thu, 4 Jun 2026 07:36:22 +0900 |
| 3 |
Subject: Normalize base64 alphabet in base64url_decode to avoid Py3.15 |
| 4 |
FutureWarning |
| 5 |
|
| 6 |
base64url_decode passed input straight to base64.urlsafe_b64decode after |
| 7 |
padding. When the input carried standard-alphabet characters (+ or /), |
| 8 |
Python 3.15 emits FutureWarning about invalid characters in URL-safe |
| 9 |
Base64 data, and CPython has announced it will eventually discard those |
| 10 |
characters (silently corrupting the decoded bytes). |
| 11 |
|
| 12 |
Translate + and / to - and _ before decoding so valid URL-safe input |
| 13 |
never warns and historical standard-alphabet input keeps decoding to the |
| 14 |
same bytes deterministically. The test_compressed_jwt fixture also |
| 15 |
carried a standard-Base64 payload segment (a literal /) rather than the |
| 16 |
URL-safe form an RFC 7515 JWT requires; rebuild it (and its HMAC |
| 17 |
signature) on the URL-safe alphabet. |
| 18 |
|
| 19 |
Fixes #1167 |
| 20 |
|
| 21 |
Signed-off-by: arpitjain099 <[email protected]> |
| 22 |
|
| 23 |
Forwarded: https://github.com/jpadilla/pyjwt/pull/1181 |
| 24 |
--- |
| 25 |
jwt/utils.py | 11 ++++++++++- |
| 26 |
tests/test_compressed_jwt.py | 6 +++--- |
| 27 |
tests/test_utils.py | 38 +++++++++++++++++++++++++++++++++++++- |
| 28 |
3 files changed, 50 insertions(+), 5 deletions(-) |
| 29 |
|
| 30 |
diff --git a/jwt/utils.py b/jwt/utils.py |
| 31 |
index 914cb50..3d91f72 100644 |
| 32 |
--- a/jwt/utils.py |
| 33 |
+++ b/jwt/utils.py |
| 34 |
@@ -21,9 +21,18 @@ def force_bytes(value: Union[bytes, str]) -> bytes: |
| 35 |
else: |
| 36 |
raise TypeError("Expected a string value") |
| 37 |
|
| 38 |
+# Translate the standard Base64 alphabet to the URL-safe alphabet so that |
| 39 |
+# callers passing standard-alphabet data (``+`` / ``/``) are normalised before |
| 40 |
+# the decode. From Python 3.15 ``base64.urlsafe_b64decode`` emits a |
| 41 |
+# ``FutureWarning`` for ``+`` / ``/`` and has announced it will eventually |
| 42 |
+# discard those characters, which would silently corrupt the decoded bytes. |
| 43 |
+# Normalising up front keeps valid URL-safe input warning-free and keeps the |
| 44 |
+# historical lenient handling of standard-alphabet input deterministic. |
| 45 |
+_STD_TO_URLSAFE = bytes.maketrans(b"+/", b"-_") |
| 46 |
+ |
| 47 |
|
| 48 |
def base64url_decode(input: Union[bytes, str]) -> bytes: |
| 49 |
- input_bytes = force_bytes(input) |
| 50 |
+ input_bytes = force_bytes(input).translate(_STD_TO_URLSAFE) |
| 51 |
|
| 52 |
rem = len(input_bytes) % 4 |
| 53 |
|
| 54 |
diff --git a/tests/test_compressed_jwt.py b/tests/test_compressed_jwt.py |
| 55 |
index 51c61a8..d80f49e 100644 |
| 56 |
--- a/tests/test_compressed_jwt.py |
| 57 |
+++ b/tests/test_compressed_jwt.py |
| 58 |
@@ -19,7 +19,7 @@ def test_decodes_complete_valid_jwt_with_compressed_payload() -> None: |
| 59 |
example_payload = {"hello": "world"} |
| 60 |
example_secret = "secret" |
| 61 |
# payload made with the pako (https://nodeca.github.io/pako/) library in Javascript: |
| 62 |
- # Buffer.from(pako.deflateRaw('{"hello": "world"}')).toString('base64') |
| 63 |
+ # Buffer.from(pako.deflateRaw('{"hello": "world"}')).toString('base64url') |
| 64 |
example_jwt = ( |
| 65 |
b"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9" |
| 66 |
b".q1bKSM3JyVeyUlAqzy_KSVGqBQA" |
| 67 |
@@ -33,7 +33,7 @@ def test_decodes_complete_valid_jwt_with_compressed_payload() -> None: |
| 68 |
"header": {"alg": "HS256", "typ": "JWT"}, |
| 69 |
"payload": example_payload, |
| 70 |
"signature": ( |
| 71 |
- b"\x00\t\xf5zP\x89\x0b\x93\x10\t\x81pO\x06\xb6\xb68" |
| 72 |
- b"\xed\xc8\xb8*-ET\xf9\x8do\x14\x1b\x14\xf2:" |
| 73 |
+ b"\x00\t\xf5zP\x89\x0b\x93\x10\t\x81pO\x06\xb6\xb6" |
| 74 |
+ b"8\xed\xc8\xb8*-ET\xf9\x8do\x14\x1b\x14\xf2:" |
| 75 |
), |
| 76 |
} |
| 77 |
diff --git a/tests/test_utils.py b/tests/test_utils.py |
| 78 |
index 2ad2314..8249d11 100644 |
| 79 |
--- a/tests/test_utils.py |
| 80 |
+++ b/tests/test_utils.py |
| 81 |
@@ -2,9 +2,17 @@ from contextlib import nullcontext |
| 82 |
|
| 83 |
import pytest |
| 84 |
|
| 85 |
+import base64 |
| 86 |
+import warnings |
| 87 |
from contextlib import AbstractContextManager |
| 88 |
|
| 89 |
-from jwt.utils import force_bytes, from_base64url_uint, is_ssh_key, to_base64url_uint |
| 90 |
+from jwt.utils import ( |
| 91 |
+ base64url_decode, |
| 92 |
+ force_bytes, |
| 93 |
+ from_base64url_uint, |
| 94 |
+ is_ssh_key, |
| 95 |
+ to_base64url_uint, |
| 96 |
+) |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.parametrize( |
| 100 |
@@ -41,6 +49,34 @@ def test_from_base64url_uint(inputval: bytes, expected: int) -> None: |
| 101 |
assert actual == expected |
| 102 |
|
| 103 |
|
| 104 |
+def test_base64url_decode_handles_standard_alphabet() -> None: |
| 105 |
+ # The same bytes encoded with the standard ("+/") and the URL-safe ("-_") |
| 106 |
+ # alphabets must decode to the same value. ``base64url_decode`` normalises |
| 107 |
+ # the standard alphabet so historical callers keep working. |
| 108 |
+ raw = bytes(range(256)) |
| 109 |
+ standard = base64.b64encode(raw) |
| 110 |
+ urlsafe = base64.urlsafe_b64encode(raw).rstrip(b"=") |
| 111 |
+ |
| 112 |
+ assert b"+" in standard or b"/" in standard |
| 113 |
+ assert base64url_decode(standard) == raw |
| 114 |
+ assert base64url_decode(urlsafe) == raw |
| 115 |
+ |
| 116 |
+ |
| 117 |
+def test_base64url_decode_does_not_warn_on_urlsafe_input() -> None: |
| 118 |
+ # Valid URL-safe input must never trigger the Python 3.15+ FutureWarning |
| 119 |
+ # about "+"/"/" in URL-safe Base64 data (jpadilla/pyjwt#1167). |
| 120 |
+ raw = bytes(range(256)) |
| 121 |
+ standard = base64.b64encode(raw) |
| 122 |
+ urlsafe = base64.urlsafe_b64encode(raw).rstrip(b"=") |
| 123 |
+ |
| 124 |
+ with warnings.catch_warnings(): |
| 125 |
+ warnings.simplefilter("error", FutureWarning) |
| 126 |
+ assert base64url_decode(urlsafe) == raw |
| 127 |
+ # A standard-alphabet input is normalised first, so it must also be |
| 128 |
+ # decoded without emitting the FutureWarning. |
| 129 |
+ assert base64url_decode(standard) == raw |
| 130 |
+ |
| 131 |
+ |
| 132 |
def test_force_bytes_raises_error_on_invalid_object() -> None: |
| 133 |
with pytest.raises(TypeError): |
| 134 |
force_bytes({}) # type: ignore[arg-type] |