From 80eaf304d1c555be6e7d658f5221b85b84f78b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 27 Aug 2025 12:35:31 +0200 Subject: [PATCH] Remove padding in base64 converter --- trytond/trytond/tests/test_wsgi.py | 27 ++++++++++++++++++++++++++- trytond/trytond/wsgi.py | 7 ++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/trytond/trytond/tests/test_wsgi.py b/trytond/trytond/tests/test_wsgi.py index 254f526664..b1c6fb2fcc 100644 --- a/trytond/trytond/tests/test_wsgi.py +++ b/trytond/trytond/tests/test_wsgi.py @@ -3,10 +3,12 @@ from unittest.mock import Mock, sentinel +from werkzeug.routing import Map, Rule + from trytond.exceptions import TrytonException from trytond.protocols.wrappers import Response from trytond.tests.test_tryton import Client, TestCase -from trytond.wsgi import TrytondWSGI +from trytond.wsgi import Base64Converter, TrytondWSGI class WSGIAppTestCase(TestCase): @@ -15,6 +17,29 @@ class WSGIAppTestCase(TestCase): class TestException(TrytonException): pass + def test_base64_converter(self): + "Test Base64 converter" + m = Map([ + Rule('/a/', endpoint='a'), + ], converters={ + 'base64': Base64Converter, + }) + a = m.bind('example.org', '/') + + self.assertEqual(a.match('/a/dGVzdA=='), ('a', {'a': 'test'})) + self.assertEqual(a.match('/a/dGVzdA'), ('a', {'a': 'test'})) + + def test_base64_converter_build(self): + "Test Base64 converter build" + m = Map([ + Rule('/a/', endpoint='a'), + ], converters={ + 'base64': Base64Converter, + }) + a = m.bind('example.org', '/') + + self.assertEqual(a.build('a', {'a': 'test'}), '/a/dGVzdA') + def test_one_error_handler_called(self): """Test an error handler is called with the app, the request and the exception.""" diff --git a/trytond/trytond/wsgi.py b/trytond/trytond/wsgi.py index 53e23128af..78bb547719 100644 --- a/trytond/trytond/wsgi.py +++ b/trytond/trytond/wsgi.py @@ -44,10 +44,15 @@ logger = logging.getLogger(__name__) class Base64Converter(BaseConverter): def to_python(self, value): + if missing_padding := len(value) % 4: + value += '=' * (4 - missing_padding) return base64.urlsafe_b64decode(value).decode('utf-8') def to_url(self, value): - return base64.urlsafe_b64encode(value.encode('utf-8')).decode('ascii') + return ( + base64.urlsafe_b64encode(value.encode('utf-8')) + .decode('ascii') + .rstrip('=')) class TrytondWSGI(object):