]> granicus.if.org Git - icinga2/commitdiff
Auto-sanitize data before en-/decoding JSON
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Fri, 15 Mar 2019 08:30:22 +0000 (09:30 +0100)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Mon, 18 Mar 2019 14:07:57 +0000 (15:07 +0100)
lib/base/json.cpp

index b2dc5adb3f278c4aa8b3a33e2b9ee7aae7cc17c9..0f2a8de91a667ac8f130052cb6b83f94246a89e8 100644 (file)
@@ -7,6 +7,7 @@
 #include "base/array.hpp"
 #include "base/objectlock.hpp"
 #include "base/convert.hpp"
+#include "base/utility.hpp"
 #include <bitset>
 #include <boost/exception_ptr.hpp>
 #include <cstdint>
@@ -104,7 +105,7 @@ void EncodeNamespace(JsonEncoder<prettyPrint>& stateMachine, const Namespace::Pt
 
        ObjectLock olock(ns);
        for (const Namespace::Pair& kv : ns) {
-               stateMachine.Key(kv.first);
+               stateMachine.Key(Utility::ValidateUTF8(kv.first));
                Encode(stateMachine, kv.second->Get());
        }
 
@@ -119,7 +120,7 @@ void EncodeDictionary(JsonEncoder<prettyPrint>& stateMachine, const Dictionary::
 
        ObjectLock olock(dict);
        for (const Dictionary::Pair& kv : dict) {
-               stateMachine.Key(kv.first);
+               stateMachine.Key(Utility::ValidateUTF8(kv.first));
                Encode(stateMachine, kv.second);
        }
 
@@ -153,7 +154,7 @@ void Encode(JsonEncoder<prettyPrint>& stateMachine, const Value& value)
                        break;
 
                case ValueString:
-                       stateMachine.Strng(value.Get<String>());
+                       stateMachine.Strng(Utility::ValidateUTF8(value.Get<String>()));
                        break;
 
                case ValueObject:
@@ -215,9 +216,11 @@ String icinga::JsonEncode(const Value& value, bool pretty_print)
 
 Value icinga::JsonDecode(const String& data)
 {
+       String sanitized (Utility::ValidateUTF8(data));
+
        JsonSax stateMachine;
 
-       nlohmann::json::sax_parse(data.Begin(), data.End(), &stateMachine);
+       nlohmann::json::sax_parse(sanitized.Begin(), sanitized.End(), &stateMachine);
 
        return stateMachine.GetResult();
 }