From 945a22747312050f6cbe557485dfb61637d77536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieliski?= Date: Tue, 16 Jul 2024 13:23:49 +0200 Subject: [PATCH] checking base64 format fix --- WebAPI/Controllers/DataInboxController.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/WebAPI/Controllers/DataInboxController.cs b/WebAPI/Controllers/DataInboxController.cs index 64560d0..a036493 100644 --- a/WebAPI/Controllers/DataInboxController.cs +++ b/WebAPI/Controllers/DataInboxController.cs @@ -137,7 +137,26 @@ public class DataInboxController : Controller // helpers private bool IsBase64String(string data) { - var bytes = new Span(new byte[256]); - return Convert.TryFromBase64String(data, bytes, out _); + if (string.IsNullOrEmpty(data)) + { + return false; + } + try + { + var base64Bytes = Convert.FromBase64String(data); + + var utf8String = Encoding.UTF8.GetString(base64Bytes); + + var reEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(utf8String)); + return data.TrimEnd('=') == reEncoded.TrimEnd('='); + } + catch (FormatException) + { + return false; + } + catch (DecoderFallbackException) + { + return false; + } } } \ No newline at end of file