Skip to content

Commit

Permalink
chore(helper): correct variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Rulleau <[email protected]>
  • Loading branch information
Leiyks committed Nov 19, 2024
1 parent 5e0cb00 commit bcc8fb7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion appsec/src/helper/action.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum class action_type : unsigned int {
redirect = 2,
block = 3,
stack_trace = 4,
extract_schema = 5
extract_derivative = 5
};

struct action {
Expand Down
21 changes: 11 additions & 10 deletions appsec/src/helper/subscriber/waf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ action_type parse_action_type_string(const std::string &action)
}

if (action == "generate_schema") {
return action_type::extract_schema;
return action_type::extract_derivative;
}

return action_type::invalid;
Expand Down Expand Up @@ -233,9 +233,10 @@ void instance::listener::call(dds::parameter_view &data, event &event)
// NOLINTNEXTLINE
total_runtime_ += res.total_runtime / 1000.0;

const parameter_view schemas{res.derivatives};
for (const auto &schema : schemas) {
schemas_.emplace(schema.key(), std::move(parameter_to_json(schema)));
const parameter_view derivatives{res.derivatives};
for (const auto &derivative : derivatives) {
derivatives_.emplace(
derivative.key(), std::move(parameter_to_json(derivative)));
}

switch (code) {
Expand Down Expand Up @@ -264,19 +265,19 @@ void instance::listener::get_meta_and_metrics(
meta[std::string(tag::event_rules_version)] = ruleset_version_;
metrics[tag::waf_duration] = total_runtime_;

for (const auto &[key, value] : schemas_) {
std::string schema = value;
for (const auto &[key, value] : derivatives_) {
std::string derivative = value;
if (value.length() > max_plain_schema_allowed &&
key.starts_with("_dd.appsec.s")) {

auto encoded = compress(schema);
auto encoded = compress(derivative);
if (encoded) {
schema = base64_encode(encoded.value(), false);
derivative = base64_encode(encoded.value(), false);
}
}

if (schema.length() <= max_schema_size) {
meta.emplace(key, std::move(schema));
if (derivative.length() <= max_schema_size) {
meta.emplace(key, std::move(derivative));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion appsec/src/helper/subscriber/waf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class instance : public dds::subscriber {
std::chrono::microseconds waf_timeout_;
double total_runtime_{0.0};
std::string_view ruleset_version_;
std::map<std::string, std::string> schemas_;
std::map<std::string, std::string> derivatives_;
};

// NOLINTNEXTLINE(google-runtime-references)
Expand Down
19 changes: 10 additions & 9 deletions appsec/tests/helper/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,14 @@ TEST(EngineTest, WafDefaultActions)

auto listener = std::make_unique<mock::listener>();
EXPECT_CALL(*listener, call(_, _))
.WillRepeatedly(Invoke([](dds::parameter_view &data,
dds::event &event_) -> void {
event_.actions.push_back({dds::action_type::redirect, {}});
event_.actions.push_back({dds::action_type::block, {}});
event_.actions.push_back({dds::action_type::stack_trace, {}});
event_.actions.push_back({dds::action_type::extract_schema, {}});
}));
.WillRepeatedly(
Invoke([](dds::parameter_view &data, dds::event &event_) -> void {
event_.actions.push_back({dds::action_type::redirect, {}});
event_.actions.push_back({dds::action_type::block, {}});
event_.actions.push_back({dds::action_type::stack_trace, {}});
event_.actions.push_back(
{dds::action_type::extract_derivative, {}});
}));

auto sub = std::make_unique<mock::subscriber>();
EXPECT_CALL(*sub, get_listener()).WillOnce(Invoke([&]() {
Expand All @@ -288,7 +289,7 @@ TEST(EngineTest, WafDefaultActions)
EXPECT_EQ(res->actions[0].type, dds::action_type::redirect);
EXPECT_EQ(res->actions[1].type, dds::action_type::block);
EXPECT_EQ(res->actions[2].type, dds::action_type::stack_trace);
EXPECT_EQ(res->actions[3].type, dds::action_type::extract_schema);
EXPECT_EQ(res->actions[3].type, dds::action_type::extract_derivative);

p = parameter::map();
p.add("b", parameter::string("value"sv));
Expand All @@ -298,7 +299,7 @@ TEST(EngineTest, WafDefaultActions)
EXPECT_EQ(res->actions[0].type, dds::action_type::redirect);
EXPECT_EQ(res->actions[1].type, dds::action_type::block);
EXPECT_EQ(res->actions[2].type, dds::action_type::stack_trace);
EXPECT_EQ(res->actions[3].type, dds::action_type::extract_schema);
EXPECT_EQ(res->actions[3].type, dds::action_type::extract_derivative);
}

TEST(EngineTest, InvalidActionsAreDiscarded)
Expand Down

0 comments on commit bcc8fb7

Please sign in to comment.