Remove padding in base64 converter

This commit is contained in:
Cédric Krier
2025-08-27 12:35:31 +02:00
parent 705baad2b1
commit 80eaf304d1
2 changed files with 32 additions and 2 deletions
+26 -1
View File
@@ -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/<base64: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/<base64: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."""
+6 -1
View File
@@ -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):