Skip to content

Commit

Permalink
Add logging and remove dead code (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 authored May 10, 2023
1 parent 40d4be9 commit ce0dd1e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ std::optional<event> match_rule(rule *rule, const object_store &store,
}

if (!rule->is_enabled()) {
DDWAF_DEBUG("Rule %s is disabled", id.c_str());
return std::nullopt;
}

Expand Down
10 changes: 0 additions & 10 deletions src/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ namespace ddwaf {

namespace {

char *to_cstr(std::string_view input)
{
const std::size_t size = input.size();
// NOLINTNEXTLINE
char *str = static_cast<char *>(malloc(size + 1));
memcpy(str, input.data(), size);
str[size] = '\0';
return str;
}

bool redact_match(const ddwaf::obfuscator &obfuscator, const event::match &match)
{
for (const auto &key : match.key_path) {
Expand Down
16 changes: 10 additions & 6 deletions src/parser/parser_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,20 @@ rule_spec_container parse_rules(parameter::vector &rule_array, base_section_info
try {
id = at<std::string>(rule_map, "id");
if (rules.find(id) != rules.end()) {
DDWAF_WARN("duplicate rule %s", id.c_str());
DDWAF_WARN("Duplicate rule %s", id.c_str());
info.add_failed(id, "duplicate rule");
continue;
}

auto rule = parse_rule(rule_map, target_manifest, rule_data_ids, limits, source);
DDWAF_DEBUG("Parsed rule %s", id.c_str());
info.add_loaded(id);
rules.emplace(std::move(id), std::move(rule));
} catch (const std::exception &e) {
if (id.empty()) {
id = index_to_id(i);
}
DDWAF_WARN("failed to parse rule '%s': %s", id.c_str(), e.what());
DDWAF_WARN("Failed to parse rule '%s': %s", id.c_str(), e.what());
info.add_failed(id, e.what());
}
}
Expand Down Expand Up @@ -475,6 +476,7 @@ rule_data_container parse_rule_data(parameter::vector &rule_data, base_section_i
continue;
}

DDWAF_DEBUG("Parsed rule data %s", id.c_str());
info.add_loaded(id);
processors.emplace(std::move(id), std::move(processor));
} catch (const ddwaf::exception &e) {
Expand Down Expand Up @@ -506,13 +508,14 @@ override_spec_container parse_overrides(parameter::vector &override_array, base_
overrides.by_tags.emplace_back(std::move(spec));
} else {
// This code is likely unreachable
DDWAF_WARN("rule override with no targets");
DDWAF_WARN("Rule override with no targets");
info.add_failed(id, "rule override with no targets");
continue;
}
DDWAF_DEBUG("Parsed override %s", id.c_str());
info.add_loaded(id);
} catch (const std::exception &e) {
DDWAF_WARN("failed to parse rule override: %s", e.what());
DDWAF_WARN("Failed to parse rule override: %s", e.what());
info.add_failed(id, e.what());
}
}
Expand All @@ -531,7 +534,7 @@ filter_spec_container parse_filters(parameter::vector &filter_array, base_sectio
try {
id = at<std::string>(node, "id");
if (filters.ids.find(id) != filters.ids.end()) {
DDWAF_WARN("duplicate filter: %s", id.c_str());
DDWAF_WARN("Duplicate filter: %s", id.c_str());
info.add_failed(id, "duplicate filter");
continue;
}
Expand All @@ -545,12 +548,13 @@ filter_spec_container parse_filters(parameter::vector &filter_array, base_sectio
filters.ids.emplace(id);
filters.rule_filters.emplace(id, std::move(filter));
}
DDWAF_DEBUG("Parsed exclusion filter %s", id.c_str());
info.add_loaded(id);
} catch (const std::exception &e) {
if (id.empty()) {
id = index_to_id(i);
}
DDWAF_WARN("failed to parse filter '%s': %s", id.c_str(), e.what());
DDWAF_WARN("Failed to parse filter '%s': %s", id.c_str(), e.what());
info.add_failed(id, e.what());
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/ruleset_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ ruleset_builder::change_state ruleset_builder::load(parameter::map &root, base_r

auto it = root.find("rules");
if (it != root.end()) {
DDWAF_DEBUG("Parsing base rules");
auto &section = info.add_section("rules");
try {
auto rules = static_cast<parameter::vector>(it->second);
Expand All @@ -241,16 +242,19 @@ ruleset_builder::change_state ruleset_builder::load(parameter::map &root, base_r
base_rules_ = parser::v2::parse_rules(
rules, section, target_manifest_, rule_data_ids_, limits_);
} else {
DDWAF_DEBUG("Clearing all base rules");
base_rules_.clear();
}
state = state | change_state::rules;
} catch (const std::exception &e) {
DDWAF_WARN("Failed to parse rules: %s", e.what());
section.set_error(e.what());
}
}

it = root.find("custom_rules");
if (it != root.end()) {
DDWAF_DEBUG("Parsing custom rules");
auto &section = info.add_section("custom_rules");
try {
auto rules = static_cast<parameter::vector>(it->second);
Expand All @@ -263,22 +267,26 @@ ruleset_builder::change_state ruleset_builder::load(parameter::map &root, base_r
rule_data_ids, limits_, rule::source_type::user);
user_rules_ = std::move(new_user_rules);
} else {
DDWAF_DEBUG("Clearing all custom rules");
user_rules_.clear();
}
state = state | change_state::custom_rules;
} catch (const std::exception &e) {
DDWAF_WARN("Failed to parse custom rules: %s", e.what());
section.set_error(e.what());
}
}

if (base_rules_.empty() && user_rules_.empty()) {
// If we haven't received rules and our base ruleset is empty, the
// WAF can't proceed.
DDWAF_WARN("No valid rules found");
throw ddwaf::parsing_error("no valid rules found");
}

it = root.find("rules_data");
if (it != root.end()) {
DDWAF_DEBUG("Parsing rule data");
auto &section = info.add_section("rules_data");
try {
auto rules_data = static_cast<parameter::vector>(it->second);
Expand All @@ -293,43 +301,51 @@ ruleset_builder::change_state ruleset_builder::load(parameter::map &root, base_r
dynamic_processors_ = std::move(new_processors);
}
} else {
DDWAF_DEBUG("Clearing all rule data");
dynamic_processors_.clear();
}
state = state | change_state::data;
} catch (const std::exception &e) {
DDWAF_WARN("Failed to parse rule data: %s", e.what());
section.set_error(e.what());
}
}

it = root.find("rules_override");
if (it != root.end()) {
DDWAF_DEBUG("Parsing overrides");
auto &section = info.add_section("rules_override");
try {
auto overrides = static_cast<parameter::vector>(it->second);
if (!overrides.empty()) {
overrides_ = parser::v2::parse_overrides(overrides, section);
} else {
DDWAF_DEBUG("Clearing all overrides");
overrides_.clear();
}
state = state | change_state::overrides;
} catch (const std::exception &e) {
DDWAF_WARN("Failed to parse overrides: %s", e.what());
section.set_error(e.what());
}
}

it = root.find("exclusions");
if (it != root.end()) {
DDWAF_DEBUG("Parsing exclusions");
auto &section = info.add_section("exclusions");
try {
auto exclusions = static_cast<parameter::vector>(it->second);
if (!exclusions.empty()) {
exclusions_ =
parser::v2::parse_filters(exclusions, section, target_manifest_, limits_);
} else {
DDWAF_DEBUG("Clearing all exclusions");
exclusions_.clear();
}
state = state | change_state::filters;
} catch (const std::exception &e) {
DDWAF_WARN("Failed to parse exclusions: %s", e.what());
section.set_error(e.what());
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/waf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ waf::waf(ddwaf::parameter input, ddwaf::base_ruleset_info &info, ddwaf::object_l
ddwaf::ruleset rs;
rs.free_fn = free_fn;
rs.event_obfuscator = event_obfuscator;
DDWAF_DEBUG("Parsing ruleset with schema version 1.x");
parser::v1::parse(input_map, info, rs, limits);
ruleset_ = std::make_shared<ddwaf::ruleset>(std::move(rs));
return;
}

if (version == 2) {
DDWAF_DEBUG("Parsing ruleset with schema version 2.x");
builder_ = std::make_shared<ruleset_builder>(limits, free_fn, std::move(event_obfuscator));
ruleset_ = builder_->build(input, info);
if (!ruleset_) {
Expand All @@ -43,6 +45,7 @@ waf::waf(ddwaf::parameter input, ddwaf::base_ruleset_info &info, ddwaf::object_l
}

DDWAF_ERROR("incompatible ruleset schema version %u.x", version);

throw unsupported_version();
}

Expand Down

0 comments on commit ce0dd1e

Please sign in to comment.