diff --git a/src/condition/scalar_condition.cpp b/src/condition/scalar_condition.cpp index 6d07adc2..1856e399 100644 --- a/src/condition/scalar_condition.cpp +++ b/src/condition/scalar_condition.cpp @@ -95,7 +95,7 @@ ResultType eval_target(Iterator &it, std::string_view address, bool ephemeral, throw ddwaf::timeout_exception(); } - if (it.type() != matcher.supported_type()) { + if (!matcher.is_supported_type(it.type())) { continue; } diff --git a/src/matcher/base.hpp b/src/matcher/base.hpp index 38f7e2f4..0b448ce8 100644 --- a/src/matcher/base.hpp +++ b/src/matcher/base.hpp @@ -33,7 +33,7 @@ class base { [[nodiscard]] virtual std::string_view to_string() const = 0; // Scalar matcher methods - [[nodiscard]] virtual DDWAF_OBJ_TYPE supported_type() const = 0; + [[nodiscard]] virtual bool is_supported_type(DDWAF_OBJ_TYPE type) const = 0; [[nodiscard]] virtual std::pair match(const ddwaf_object &obj) const = 0; }; @@ -54,9 +54,9 @@ template class base_impl : public base { return static_cast(this)->to_string_impl(); } - [[nodiscard]] DDWAF_OBJ_TYPE supported_type() const override + [[nodiscard]] bool is_supported_type(DDWAF_OBJ_TYPE type) const override { - return T::supported_type_impl(); + return T::is_supported_type_impl(type); } // Helper used for testing purposes @@ -68,31 +68,31 @@ template class base_impl : public base { [[nodiscard]] std::pair match(const ddwaf_object &obj) const override { const auto *ptr = static_cast(this); - if constexpr (T::supported_type_impl() == DDWAF_OBJ_STRING) { + if constexpr (T::is_supported_type_impl(DDWAF_OBJ_STRING)) { if (obj.type == DDWAF_OBJ_STRING && obj.stringValue != nullptr) { return ptr->match_impl({obj.stringValue, static_cast(obj.nbEntries)}); } } - if constexpr (T::supported_type_impl() == DDWAF_OBJ_SIGNED) { + if constexpr (T::is_supported_type_impl(DDWAF_OBJ_SIGNED)) { if (obj.type == DDWAF_OBJ_SIGNED) { return ptr->match_impl(obj.intValue); } } - if constexpr (T::supported_type_impl() == DDWAF_OBJ_UNSIGNED) { + if constexpr (T::is_supported_type_impl(DDWAF_OBJ_UNSIGNED)) { if (obj.type == DDWAF_OBJ_UNSIGNED) { return ptr->match_impl(obj.uintValue); } } - if constexpr (T::supported_type_impl() == DDWAF_OBJ_BOOL) { + if constexpr (T::is_supported_type_impl(DDWAF_OBJ_BOOL)) { if (obj.type == DDWAF_OBJ_BOOL) { return ptr->match_impl(obj.boolean); } } - if constexpr (T::supported_type_impl() == DDWAF_OBJ_FLOAT) { + if constexpr (T::is_supported_type_impl(DDWAF_OBJ_FLOAT)) { if (obj.type == DDWAF_OBJ_FLOAT) { return ptr->match_impl(obj.f64); } diff --git a/src/matcher/equals.hpp b/src/matcher/equals.hpp index 79597cc3..04ed8671 100644 --- a/src/matcher/equals.hpp +++ b/src/matcher/equals.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "matcher/base.hpp" #include "utils.hpp" @@ -30,29 +31,30 @@ template class equals : public base_impl> { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "equals"; } - - static constexpr DDWAF_OBJ_TYPE supported_type_impl() + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) { - if constexpr (std::is_same_v) { - return DDWAF_OBJ_SIGNED; - } - if constexpr (std::is_same_v) { - return DDWAF_OBJ_UNSIGNED; + if constexpr (std::is_same_v || std::is_same_v) { + return type == DDWAF_OBJ_SIGNED || type == DDWAF_OBJ_UNSIGNED; } if constexpr (std::is_same_v) { - return DDWAF_OBJ_BOOL; + return type == DDWAF_OBJ_BOOL; } if constexpr (std::is_same_v) { - return DDWAF_OBJ_STRING; + return type == DDWAF_OBJ_STRING; } } - [[nodiscard]] std::pair match_impl(const T &obtained) const + template + [[nodiscard]] std::pair match_impl(const U &obtained) const requires(!std::is_same_v) { - return {expected_ == obtained, {}}; + if constexpr (std::is_same_v || std::is_same_v) { + return {std::cmp_equal(expected_, obtained), {}}; + } else { + return {expected_ == obtained, {}}; + } } [[nodiscard]] std::pair match_impl(std::string_view obtained) const @@ -79,7 +81,10 @@ template <> class equals : public base_impl> { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "equals"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_FLOAT; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_FLOAT; + } [[nodiscard]] std::pair match_impl(double obtained) const { diff --git a/src/matcher/exact_match.hpp b/src/matcher/exact_match.hpp index 469357ef..3de97e71 100644 --- a/src/matcher/exact_match.hpp +++ b/src/matcher/exact_match.hpp @@ -30,7 +30,10 @@ class exact_match : public base_impl { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "exact_match"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } [[nodiscard]] std::pair match_impl(std::string_view str) const; diff --git a/src/matcher/greater_than.hpp b/src/matcher/greater_than.hpp index 0949c503..1ccc2fcc 100644 --- a/src/matcher/greater_than.hpp +++ b/src/matcher/greater_than.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "ddwaf.h" #include "matcher/base.hpp" @@ -30,25 +31,22 @@ class greater_than : public base_impl> { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "greater_than"; } - - static constexpr DDWAF_OBJ_TYPE supported_type_impl() + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) { - if constexpr (std::is_same_v) { - return DDWAF_OBJ_SIGNED; - } - if constexpr (std::is_same_v) { - return DDWAF_OBJ_UNSIGNED; - } - if constexpr (std::is_same_v) { - return DDWAF_OBJ_FLOAT; - } + return type == DDWAF_OBJ_SIGNED || type == DDWAF_OBJ_UNSIGNED || type == DDWAF_OBJ_FLOAT; } - [[nodiscard]] std::pair match_impl(const T &obtained) const + template + [[nodiscard]] std::pair match_impl(const U &obtained) const + requires(!std::is_floating_point_v) { - return {minimum_ < obtained, {}}; + return {std::cmp_greater(obtained, minimum_), {}}; } + [[nodiscard]] std::pair match_impl(double obtained) const + { + return {obtained > minimum_, {}}; + } T minimum_; friend class base_impl>; diff --git a/src/matcher/ip_match.hpp b/src/matcher/ip_match.hpp index e3fb84b2..83e9bf01 100644 --- a/src/matcher/ip_match.hpp +++ b/src/matcher/ip_match.hpp @@ -44,7 +44,10 @@ class ip_match : public base_impl { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "ip_match"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } [[nodiscard]] std::pair match_impl(std::string_view str) const; diff --git a/src/matcher/is_sqli.hpp b/src/matcher/is_sqli.hpp index 1f933312..3455f922 100644 --- a/src/matcher/is_sqli.hpp +++ b/src/matcher/is_sqli.hpp @@ -26,7 +26,10 @@ class is_sqli : public base_impl { static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "is_sqli"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } static std::pair match_impl(std::string_view pattern); diff --git a/src/matcher/is_xss.hpp b/src/matcher/is_xss.hpp index 625aaf5f..4f579ced 100644 --- a/src/matcher/is_xss.hpp +++ b/src/matcher/is_xss.hpp @@ -24,7 +24,10 @@ class is_xss : public base_impl { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "is_xss"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } static std::pair match_impl(std::string_view pattern); diff --git a/src/matcher/lower_than.hpp b/src/matcher/lower_than.hpp index 03746173..62a248ea 100644 --- a/src/matcher/lower_than.hpp +++ b/src/matcher/lower_than.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "ddwaf.h" #include "matcher/base.hpp" @@ -30,23 +31,21 @@ class lower_than : public base_impl> { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "lower_than"; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_SIGNED || type == DDWAF_OBJ_UNSIGNED || type == DDWAF_OBJ_FLOAT; + } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() + template + [[nodiscard]] std::pair match_impl(const U &obtained) const + requires(!std::is_floating_point_v) { - if constexpr (std::is_same_v) { - return DDWAF_OBJ_SIGNED; - } - if constexpr (std::is_same_v) { - return DDWAF_OBJ_UNSIGNED; - } - if constexpr (std::is_same_v) { - return DDWAF_OBJ_FLOAT; - } + return {std::cmp_less(obtained, maximum_), {}}; } - [[nodiscard]] std::pair match_impl(const T &obtained) const + [[nodiscard]] std::pair match_impl(double obtained) const { - return {maximum_ > obtained, {}}; + return {obtained < maximum_, {}}; } T maximum_; diff --git a/src/matcher/phrase_match.hpp b/src/matcher/phrase_match.hpp index f2b094dd..54c5c5ab 100644 --- a/src/matcher/phrase_match.hpp +++ b/src/matcher/phrase_match.hpp @@ -8,6 +8,7 @@ #include #include +#include #include "matcher/base.hpp" @@ -26,7 +27,10 @@ class phrase_match : public base_impl { protected: static constexpr std::string_view to_string_impl() { return ""; } static constexpr std::string_view name_impl() { return "phrase_match"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } [[nodiscard]] std::pair match_impl(std::string_view pattern) const; diff --git a/src/matcher/regex_match.hpp b/src/matcher/regex_match.hpp index ec13b359..13388a85 100644 --- a/src/matcher/regex_match.hpp +++ b/src/matcher/regex_match.hpp @@ -26,7 +26,10 @@ class regex_match : public base_impl { protected: [[nodiscard]] std::string_view to_string_impl() const { return regex->pattern(); } static constexpr std::string_view name_impl() { return "match_regex"; } - static constexpr DDWAF_OBJ_TYPE supported_type_impl() { return DDWAF_OBJ_STRING; } + static constexpr bool is_supported_type_impl(DDWAF_OBJ_TYPE type) + { + return type == DDWAF_OBJ_STRING; + } [[nodiscard]] std::pair match_impl(std::string_view pattern) const;