From 8ae206cd5d77e4c1a4bd911d67f56535c15987ee Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Tue, 30 Apr 2019 15:56:57 +0200 Subject: [PATCH] Improve validation for times.{begin,end} in notification objects fixes #6939 --- lib/icinga/notification.cpp | 29 +++++++++++++++++++++++++++++ lib/icinga/notification.hpp | 1 + 2 files changed, 30 insertions(+) diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index 998705efa..5067116a9 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -720,6 +720,35 @@ void Notification::ValidateTypes(const Lazy& lvalue, const Validatio BOOST_THROW_EXCEPTION(ValidationError(this, { "types" }, "Type filter is invalid.")); } +void Notification::ValidateTimes(const Lazy& lvalue, const ValidationUtils& utils) +{ + ObjectImpl::ValidateTimes(lvalue, utils); + + Dictionary::Ptr times = lvalue(); + + if (!times) + return; + + double begin; + double end; + + try { + begin = Convert::ToDouble(times->Get("begin")); + } catch (const std::exception&) { + BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'begin' is invalid, must be duration or number." )); + } + + try { + end = Convert::ToDouble(times->Get("end")); + } catch (const std::exception&) { + BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'end' is invalid, must be duration or number." )); + } + + /* Also solve logical errors where begin > end. */ + if (begin > 0 && end > 0 && begin > end) + BOOST_THROW_EXCEPTION(ValidationError(this, { "times" }, "'begin' must be smaller than 'end'.")); +} + Endpoint::Ptr Notification::GetCommandEndpoint() const { return Endpoint::GetByName(GetCommandEndpointRaw()); diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp index 5006e89d8..c483eaf91 100644 --- a/lib/icinga/notification.hpp +++ b/lib/icinga/notification.hpp @@ -95,6 +95,7 @@ public: void ValidateStates(const Lazy& lvalue, const ValidationUtils& utils) override; void ValidateTypes(const Lazy& lvalue, const ValidationUtils& utils) override; + void ValidateTimes(const Lazy& lvalue, const ValidationUtils& utils) override; static void EvaluateApplyRules(const intrusive_ptr& host); static void EvaluateApplyRules(const intrusive_ptr& service); -- 2.40.0