From 47434cd62d27cde6f9e060e1d6f441f173fb0b38 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 8 Jul 2022 22:45:31 +0200 Subject: [PATCH] stages/prompt: try to base64 decode file, fallback to keeping value as-is Signed-off-by: Jens Langhammer --- authentik/stages/prompt/models.py | 7 ++++++- authentik/stages/prompt/tests.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/authentik/stages/prompt/models.py b/authentik/stages/prompt/models.py index 60da2bd88..67d56c03a 100644 --- a/authentik/stages/prompt/models.py +++ b/authentik/stages/prompt/models.py @@ -1,5 +1,6 @@ """prompt models""" from base64 import b64decode +from binascii import Error from typing import Any, Optional from urllib.parse import urlparse from uuid import uuid4 @@ -90,7 +91,11 @@ class InlineFileField(CharField): _mime, _, enc = header.partition(";") if enc != "base64": raise ValidationError("Invalid encoding") - data = b64decode(encoded.encode()).decode() + try: + data = b64decode(encoded.encode()).decode() + except (UnicodeDecodeError, UnicodeEncodeError, ValueError, Error): + LOGGER.info("failed to decode base64 of file field, keeping base64") + data = encoded return super().to_internal_value(data) diff --git a/authentik/stages/prompt/tests.py b/authentik/stages/prompt/tests.py index 8a3931548..5258b8975 100644 --- a/authentik/stages/prompt/tests.py +++ b/authentik/stages/prompt/tests.py @@ -120,6 +120,10 @@ class TestPromptStage(FlowTestCase): InlineFileField().to_internal_value("data:mine/type;base64,Zm9v"), "foo", ) + self.assertEqual( + InlineFileField().to_internal_value("data:mine/type;base64,Zm9vqwer"), + "Zm9vqwer", + ) def test_render(self): """Test render of form, check if all prompts are rendered correctly"""