From a9975f7945211abd5411e6b3061443a8f6b43236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Fri, 15 Mar 2024 15:43:24 +0100 Subject: [PATCH] adapter.http: fix base64 decoding without padding Many online tools omit the padding when encoding base64url. Thus, requesters may omit the padding as well, which would cause the decoding to fail. Thus, we simply always append two padding characters, because python doesn't complain about too much padding. --- basyx/aas/adapter/http.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/basyx/aas/adapter/http.py b/basyx/aas/adapter/http.py index e55fddff3..73c0307d8 100644 --- a/basyx/aas/adapter/http.py +++ b/basyx/aas/adapter/http.py @@ -376,7 +376,11 @@ def to_url(self, value: model.Identifier) -> str: def to_python(self, value: str) -> model.Identifier: value = super().to_python(value) try: - decoded = base64.urlsafe_b64decode(super().to_python(value)).decode(self.encoding) + # If the requester omits the base64 padding, an exception will be raised. + # However, Python doesn't complain about too much padding, + # thus we simply always append two padding characters (==). + # See also: https://stackoverflow.com/a/49459036/4780052 + decoded = base64.urlsafe_b64decode(super().to_python(value) + "==").decode(self.encoding) except binascii.Error: raise BadRequest(f"Encoded identifier {value} is invalid base64url!") except UnicodeDecodeError: