changeset 1988:e29aea2349b9

test validity of base64 strings
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 28 Apr 2016 17:45:03 +0200
parents ce90d109bb64
children f3339c4f8bf4
files Core/Toolbox.cpp UnitTestsSources/UnitTestsMain.cpp
diffstat 2 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Tue Apr 26 17:40:55 2016 +0200
+++ b/Core/Toolbox.cpp	Thu Apr 28 17:45:03 2016 +0200
@@ -582,6 +582,18 @@
   void Toolbox::DecodeBase64(std::string& result, 
                              const std::string& data)
   {
+    for (size_t i = 0; i < data.length(); i++)
+    {
+      if (!isalnum(data[i]) &&
+          data[i] != '+' &&
+          data[i] != '/' &&
+          data[i] != '=')
+      {
+        // This is not a valid character for a Base64 string
+        throw OrthancException(ErrorCode_BadFileFormat);
+      }
+    }
+
     result = base64_decode(data);
   }
 
--- a/UnitTestsSources/UnitTestsMain.cpp	Tue Apr 26 17:40:55 2016 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Thu Apr 28 17:45:03 2016 +0200
@@ -364,6 +364,12 @@
   std::string decoded;
   Toolbox::DecodeBase64(decoded, hello);
   ASSERT_EQ("Hello world", decoded);
+
+  // Invalid character
+  ASSERT_THROW(Toolbox::DecodeBase64(decoded, "?"), OrthancException);
+
+  // All the allowed characters
+  Toolbox::DecodeBase64(decoded, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=");
 }
 
 TEST(Toolbox, PathToExecutable)