From 723bc1cabf3c45d9fd8b742f2c4c679847299aaa Mon Sep 17 00:00:00 2001 From: Leonardo Di Donato Date: Thu, 1 Aug 2019 18:59:26 +0000 Subject: [PATCH] fix(userspace): accessing a (json) object can throw exceptions because of wrong types Signed-off-by: Leonardo Di Donato --- userspace/engine/falco_engine.cpp | 56 ++++++++++++++++++------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/userspace/engine/falco_engine.cpp b/userspace/engine/falco_engine.cpp index 9b47f363..e3e8327b 100644 --- a/userspace/engine/falco_engine.cpp +++ b/userspace/engine/falco_engine.cpp @@ -365,46 +365,54 @@ unique_ptr falco_engine::process_k8s_audit_event(json bool falco_engine::parse_k8s_audit_json(nlohmann::json &j, std::list &evts) { - // If the Kind is EventList, split it into individual events. - if(j.value("kind", "") == "EventList") + // Note that nlohmann::basic_json::value can throw nlohmann::basic_json::type_error (302, 306) + try { - for(auto &je : j["items"]) + // If the kind is EventList, split it into individual events + if(j.value("kind", "") == "EventList") + { + for(auto &je : j["items"]) + { + evts.emplace_back(); + je["kind"] = "Event"; + + uint64_t ns = 0; + if(!sinsp_utils::parse_iso_8601_utc_string(je.value(k8s_audit_time, ""), ns)) + { + return false; + } + + std::string tmp; + sinsp_utils::ts_to_string(ns, &tmp, false, true); + + evts.back().set_jevt(je, ns); + } + + return true; + } + else if(j.value("kind", "") == "Event") { evts.emplace_back(); - je["kind"] = "Event"; - uint64_t ns = 0; - if(!sinsp_utils::parse_iso_8601_utc_string(je.value(k8s_audit_time, ""), ns)) + if(!sinsp_utils::parse_iso_8601_utc_string(j.value(k8s_audit_time, ""), ns)) { return false; } - std::string tmp; - sinsp_utils::ts_to_string(ns, &tmp, false, true); - - evts.back().set_jevt(je, ns); + evts.back().set_jevt(j, ns); + return true; } - - return true; - } - else if(j.value("kind", "") == "Event") - { - evts.emplace_back(); - uint64_t ns = 0; - if(!sinsp_utils::parse_iso_8601_utc_string(j.value(k8s_audit_time, ""), ns)) + else { return false; } - - - evts.back().set_jevt(j, ns); - return true; } - else + catch(exception &e) { + // Propagate the exception + rethrow_exception(current_exception()); return false; } - } unique_ptr falco_engine::process_k8s_audit_event(json_event *ev)