From d9f3b250e0cac90529e9dd2c6806d4f9d46901fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Fri, 3 Jul 2026 13:59:02 +0200 Subject: [PATCH] require padding when decoding base64 on Python 3.15+ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restore the old behavior of `urlsafe_b64decode()` to require padding when running on Python 3.15+, in order to fix raising `BadData` in some cases of malformed input. This fixes the test failures in "bad payload" tests that incidentally surface when using zlib-ng as the compression library. Fixes #420 Signed-off-by: Michał Górny --- CHANGES.rst | 2 ++ src/itsdangerous/encoding.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 17c1a19..070ce38 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ Unreleased - Drop support for Python 3.8 and 3.9. - Remove previously deprecated code. +- Fix raising ``BadData`` on payload without padding on Python 3.15. + :issue:`420` Version 2.2.0 diff --git a/src/itsdangerous/encoding.py b/src/itsdangerous/encoding.py index f5ca80f..1136d3e 100644 --- a/src/itsdangerous/encoding.py +++ b/src/itsdangerous/encoding.py @@ -3,6 +3,7 @@ import base64 import string import struct +import sys import typing as t from .exc import BadData @@ -33,7 +34,10 @@ def base64_decode(string: str | bytes) -> bytes: string += b"=" * (-len(string) % 4) try: - return base64.urlsafe_b64decode(string) + if sys.version_info >= (3, 15): + return base64.urlsafe_b64decode(string, padded=True) + else: + return base64.urlsafe_b64decode(string) except (TypeError, ValueError) as e: raise BadData("Invalid base64-encoded data") from e