Skip to content

Commit

Permalink
adapter.http: fix base64 decoding without padding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkhsjdhjs committed Mar 15, 2024
1 parent 107f158 commit a9975f7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion basyx/aas/adapter/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a9975f7

Please sign in to comment.