#include <memory>
#include <optional>
#include <set>
#include <stdexcept>
#include <string>
#include <utility>
#include "clock.hpp"
#include "exclusion/input_filter.hpp"
#include "exclusion/object_filter.hpp"
#include "expression.hpp"
#include "log.hpp"
#include "matcher/base.hpp"
#include "object_store.hpp"
#include "rule.hpp"
namespace ddwaf {
using excluded_set = input_filter::excluded_set;
input_filter::input_filter(std::string id, std::shared_ptr<expression> expr,
std::set<const core_rule *> rule_targets, std::shared_ptr<object_filter> filter)
: id_(std::move(id)), expr_(std::move(expr)), rule_targets_(std::move(rule_targets)),
filter_(std::move(filter))
{
if (!expr_) {
throw std::invalid_argument("rule filter constructed with null expression");
}
}
std::optional<excluded_set> input_filter::match(const object_store &store, cache_type &cache,
const matcher_mapper &dynamic_matchers, timer &deadline) const
{
DDWAF_DEBUG("Evaluating input filter '{}'", id_);
if (!expr_->eval(cache.expr_cache, store, {}, dynamic_matchers, deadline)) {
return std::nullopt;
}
auto objects = filter_->match(store, cache.object_filter_cache, deadline);
if (objects.empty()) {
return std::nullopt;
}
return {{.rules = rule_targets_, .objects = std::move(objects)}};
}
}