From aadb160bb3431e56fabf68375d50578189eea877 Mon Sep 17 00:00:00 2001 From: Aisvarya Sampath Kumar Date: Tue, 4 Aug 2026 19:30:32 -0500 Subject: [PATCH] gh-155193: Add missing canonical parameter to urlsafe_b64decode() The what's new entry for 3.15 documents canonical=False being added to b32decode(), b32hexdecode(), b64decode(), urlsafe_b64decode(), a85decode(), b85decode(), and z85decode() (gh-146311), but urlsafe_b64decode() did not actually receive the parameter. Add canonical=False to urlsafe_b64decode()'s signature and pass it through to binascii.a2b_base64(), consistent with b64decode(). The default preserves existing behavior. --- Lib/base64.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/base64.py b/Lib/base64.py index fa562f74a810345..af4c1846234f8e6 100644 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -164,7 +164,7 @@ def urlsafe_b64encode(s, *, padded=True): return binascii.b2a_base64(s, padded=padded, newline=False, alphabet=binascii.URLSAFE_BASE64_ALPHABET) -def urlsafe_b64decode(s, *, padded=False): +def urlsafe_b64decode(s, *, padded=False, canonical=False): """Decode bytes using the URL- and filesystem-safe Base64 alphabet. Argument s is a bytes-like object or ASCII string to decode. The result @@ -175,6 +175,9 @@ def urlsafe_b64decode(s, *, padded=False): If padded is false, padding in input is not required. + If canonical is true, non-canonical encodings (non-zero padding bits or + other non-canonical forms) are rejected. + The alphabet uses '-' instead of '+' and '_' instead of '/'. """ s = _bytes_from_decode_data(s) @@ -184,7 +187,8 @@ def urlsafe_b64decode(s, *, padded=False): badchar = b break s = s.translate(_urlsafe_decode_translation) - result = binascii.a2b_base64(s, strict_mode=False, padded=padded) + result = binascii.a2b_base64(s, strict_mode=False, padded=padded, + canonical=canonical) if badchar is not None: import warnings warnings.warn(f'invalid character {chr(badchar)!a} in URL-safe Base64 data '