Skip to content

Optimize duplicate rule ID check #1735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions headers/modsecurity/rules_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <vector>
#include <list>
#include <set>
#include <unordered_set>
#endif


Expand Down Expand Up @@ -273,7 +274,7 @@ class RulesProperties {
std::ostringstream *err) {
int amount_of_rules = 0;

amount_of_rules = appendRules(from->m_rules, to->m_rules, err);
amount_of_rules = appendRules(from, to, err);
if (amount_of_rules < 0) {
return amount_of_rules;
}
Expand Down Expand Up @@ -426,20 +427,24 @@ class RulesProperties {


static int appendRules(
std::vector<modsecurity::Rule *> *from,
std::vector<modsecurity::Rule *> *to,
RulesProperties *from,
RulesProperties *to,
std::ostringstream *err) {
std::vector<modsecurity::Rule *> *from_rules = from->m_rules;
std::vector<modsecurity::Rule *> *to_rules = to->m_rules;
int amount_of_rules = 0;
for (int i = 0; i < modsecurity::Phases::NUMBER_OF_PHASES; i++) {
std::vector<modsecurity::Rule *> *rules_to = to+i;
std::vector<modsecurity::Rule *> *rules_from = from+i;
std::vector<modsecurity::Rule *> *rules_to = to_rules+i;
std::vector<modsecurity::Rule *> *rules_from = from_rules+i;
for (size_t j = 0; j < rules_from->size(); j++) {
Rule *rule = rules_from->at(j);
for (size_t z = 0; z < rules_to->size(); z++) {
Rule *rule_ckc = rules_to->at(z);
if (rule_ckc->m_ruleId == rule->m_ruleId &&
rule_ckc->m_secMarker == false &&
rule->m_secMarker == false) {
bool do_check = rule->m_secMarker == false;

if (do_check) {
std::unordered_set<int64_t>::iterator it =
to->m_ruleIds.find(rule->m_ruleId);

if (it != to->m_ruleIds.end()) {
if (err != NULL) {
*err << "Rule id: " \
<< std::to_string(rule->m_ruleId) \
Expand All @@ -448,9 +453,13 @@ class RulesProperties {
return -1;
}
}

amount_of_rules++;
rules_to->push_back(rule);
rule->refCountIncrease();

if (do_check)
to->m_ruleIds.insert(rule->m_ruleId);
}
}
return amount_of_rules;
Expand Down Expand Up @@ -492,6 +501,7 @@ class RulesProperties {
ConfigString m_secWebAppId;
std::vector<actions::Action *> m_defaultActions[8];
std::vector<modsecurity::Rule *> m_rules[8];
std::unordered_set<int64_t> m_ruleIds;
ConfigUnicodeMap m_unicodeMapTable;
};

Expand Down