Skip to content

Commit

Permalink
Refactoring on the operators: negation is now being handled globally
Browse files Browse the repository at this point in the history
Other minors changes were also made, including adding the prefix `m_'
to all the members of the class.
  • Loading branch information
zimmerle committed Oct 19, 2016
1 parent 28a44b9 commit 8757840
Show file tree
Hide file tree
Showing 36 changed files with 88 additions and 138 deletions.
6 changes: 1 addition & 5 deletions src/operators/begins_with.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ namespace operators {
bool BeginsWith::evaluate(Transaction *transaction, const std::string &str) {
bool ret = false;

std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);

if (str.size() < p.size()) {
ret = false;
} else if (!str.compare(0, p.size(), p)) {
ret = true;
}

if (negation) {
return !ret;
}

return ret;
}

Expand Down
6 changes: 1 addition & 5 deletions src/operators/contains.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ namespace modsecurity {
namespace operators {

bool Contains::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);
bool contains = input.find(p) != std::string::npos;

if (contains && transaction) {
transaction->m_matched.push_back(p);
}

if (negation) {
return !contains;
}

return contains;
}

Expand Down
2 changes: 1 addition & 1 deletion src/operators/contains_word.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool ContainsWord::acceptableChar(const std::string& a, size_t pos) {

bool ContainsWord::evaluate(Transaction *transaction,
const std::string& input) {
std::string paramTarget = MacroExpansion::expand(param, transaction);
std::string paramTarget = MacroExpansion::expand(m_param, transaction);

if (paramTarget.empty()) {
return true;
Expand Down
4 changes: 0 additions & 4 deletions src/operators/detect_sqli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ bool DetectSQLi::evaluate(Transaction *transaction, const std::string &input) {
}
}

if (negation) {
return issqli == 0;
}

return issqli != 0;
}

Expand Down
4 changes: 0 additions & 4 deletions src/operators/detect_xss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ bool DetectXSS::evaluate(Transaction *transaction, const std::string &input) {
#endif
}

if (negation) {
return is_xss == 0;
}

return is_xss != 0;
}

Expand Down
6 changes: 1 addition & 5 deletions src/operators/ends_with.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ namespace operators {

bool EndsWith::evaluate(Transaction *transaction, const std::string &input) {
bool ret = false;
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);

if (input.length() >= p.length()) {
ret = (0 == input.compare(input.length() - p.length(),
p.length(), p));
}

if (negation) {
return !ret;
}

return ret;
}

Expand Down
6 changes: 1 addition & 5 deletions src/operators/eq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool Eq::evaluate(Transaction *transaction, const std::string &input) {
int p = 0;
int i = 0;
bool eq = false;
std::string pt = MacroExpansion::expand(param, transaction);
std::string pt = MacroExpansion::expand(m_param, transaction);

try {
p = std::stoi(pt);
Expand All @@ -43,10 +43,6 @@ bool Eq::evaluate(Transaction *transaction, const std::string &input) {

eq = p == i;

if (negation) {
return !eq;
}

return eq;
}

Expand Down
4 changes: 2 additions & 2 deletions src/operators/fuzzy_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ bool FuzzyHash::evaluate(Transaction *transaction, const std::string &str) {
FuzzyHash::FuzzyHash(std::string op, std::string param,
bool negation)
: Operator() {
this->op = op;
this->param = param;
this->m_op = op;
this->m_param = param;
}

} // namespace operators
Expand Down
6 changes: 1 addition & 5 deletions src/operators/ge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ namespace modsecurity {
namespace operators {

bool Ge::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);
std::string i = MacroExpansion::expand(input, transaction);

bool ge = atoll(i.c_str()) >= atoll(p.c_str());

if (negation) {
return !ge;
}

return ge;
}

Expand Down
4 changes: 2 additions & 2 deletions src/operators/geo_lookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ bool GeoLookup::evaluate(Transaction *trans, const std::string &exp) {
GeoLookup::GeoLookup(std::string op, std::string param,
bool negation)
: Operator() {
this->op = op;
this->param = param;
this->m_op = op;
this->m_param = param;
}

} // namespace operators
Expand Down
4 changes: 2 additions & 2 deletions src/operators/gsblookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ bool GsbLookup::evaluate(Transaction *transaction, const std::string &str) {
GsbLookup::GsbLookup(std::string op, std::string param,
bool negation)
: Operator() {
this->op = op;
this->param = param;
this->m_op = op;
this->m_param = param;
}

} // namespace operators
Expand Down
6 changes: 1 addition & 5 deletions src/operators/gt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ namespace modsecurity {
namespace operators {

bool Gt::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);

bool gt = atoll(input.c_str()) > atoll(p.c_str());

if (negation) {
return !gt;
}

return gt;
}

Expand Down
4 changes: 2 additions & 2 deletions src/operators/inspect_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ bool InspectFile::evaluate(Transaction *transaction, const std::string &str) {
InspectFile::InspectFile(std::string op, std::string param,
bool negation)
: Operator() {
this->op = op;
this->param = param;
this->m_op = op;
this->m_param = param;
}

} // namespace operators
Expand Down
2 changes: 1 addition & 1 deletion src/operators/ip_match.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace operators {

bool IpMatch::init(const std::string &file, std::string *error) {
std::string e("");
bool res = m_tree.addFromBuffer(param, &e);
bool res = m_tree.addFromBuffer(m_param, &e);

if (res == false) {
error->assign(e);
Expand Down
6 changes: 3 additions & 3 deletions src/operators/ip_match_from_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ bool IpMatchFromFile::init(const std::string &file,
std::string e("");
bool res = false;

if (param.compare(0, 8, "https://") == 0) {
res = m_tree.addFromUrl(param, &e);
if (m_param.compare(0, 8, "https://") == 0) {
res = m_tree.addFromUrl(m_param, &e);
} else {
res = m_tree.addFromFile(param, &e);
res = m_tree.addFromFile(m_param, &e);
}

if (res == false) {
Expand Down
6 changes: 1 addition & 5 deletions src/operators/le.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ namespace modsecurity {
namespace operators {

bool Le::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);

bool le = atoll(input.c_str()) <= atoll(p.c_str());

if (negation) {
return !le;
}

return le;
}

Expand Down
6 changes: 1 addition & 5 deletions src/operators/lt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ namespace modsecurity {
namespace operators {

bool Lt::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);

bool lt = atoll(input.c_str()) < atoll(p.c_str());

if (negation) {
return !lt;
}

return lt;
}

Expand Down
17 changes: 14 additions & 3 deletions src/operators/operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,25 @@ bool Operator::debug(Transaction *transaction, int x, std::string a) {
}


bool Operator::evaluateInternal(Transaction *transaction,
const std::string& a) {
bool res = evaluate(transaction, a);

if (m_negation) {
return !res;
}

return res;
}


bool Operator::evaluate(Transaction *transaction, const std::string& a) {
#ifndef NO_LOGS
if (transaction) {
transaction->debug(2, "Operator: " + this->op + \
transaction->debug(2, "Operator: " + this->m_op + \
" is not implemented or malfunctioning.");
} else {
std::cerr << "Operator: " + this->op + \
std::cerr << "Operator: " + this->m_op + \
" is not implemented or malfunctioning.";
}
#endif
Expand Down Expand Up @@ -183,4 +195,3 @@ Operator *Operator::instantiate(std::string op_string) {

} // namespace operators
} // namespace modsecurity

25 changes: 14 additions & 11 deletions src/operators/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,30 @@ class Operator {
public:
/** @ingroup ModSecurity_Operator */
Operator()
: op(""),
param(""),
negation(false) { }
: m_match_message(""),
m_negation(false),
m_op(""),
m_param("") { }
Operator(std::string op, std::string param, bool negation)
: op(op),
param(param),
negation(negation) { }
: m_match_message(""),
m_negation(negation),
m_op(op),
m_param(param) { }

virtual ~Operator() { }
std::string op;
std::string param;
bool negation;
static Operator *instantiate(std::string opName);

virtual bool init(const std::string &file, std::string *error) {
virtual bool init(const std::string &arg, std::string *error) {
return true;
}

bool evaluateInternal(Transaction *t, const std::string& a);
virtual bool evaluate(Transaction *transaction, const std::string &str);
static Operator *instantiate(std::string op);

bool m_negation;
std::string m_match_message;
std::string m_op;
std::string m_param;

protected:
bool debug(Transaction *transaction, int x, std::string a);
Expand Down
6 changes: 3 additions & 3 deletions src/operators/pm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ bool Pm::init(const std::string &file, std::string *error) {
std::istringstream *iss;
const char *err = NULL;

replaceAll(param, "\\", "\\\\");
replaceAll(m_param, "\\", "\\\\");

char *content = parse_pm_content(param.c_str(), param.length(), &err);
char *content = parse_pm_content(m_param.c_str(), m_param.length(), &err);
if (content == NULL) {
iss = new std::istringstream(param);
iss = new std::istringstream(m_param);
} else {
iss = new std::istringstream(content);
}
Expand Down
8 changes: 4 additions & 4 deletions src/operators/pm_from_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ namespace operators {
bool PmFromFile::init(const std::string &config, std::string *error) {
std::istream *iss;

if (param.compare(0, 8, "https://") == 0) {
if (m_param.compare(0, 8, "https://") == 0) {
Utils::HttpsClient client;
bool ret = client.download(param);
bool ret = client.download(m_param);
if (ret == false) {
error->assign(client.error);
return false;
}
iss = new std::stringstream(client.content);
} else {
std::string resource = find_resource(param, config);
std::string resource = find_resource(m_param, config);
iss = new std::ifstream(resource, std::ios::in);

if (((std::ifstream *)iss)->is_open() == false) {
error->assign("Failed to open file: " + param);
error->assign("Failed to open file: " + m_param);
delete iss;
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/operators/rsub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ bool Rsub::evaluate(Transaction *transaction, const std::string &str) {

Rsub::Rsub(std::string op, std::string param, bool negation)
: Operator() {
this->op = op;
this->param = param;
this->m_op = op;
this->m_param = param;
}

} // namespace operators
Expand Down
6 changes: 1 addition & 5 deletions src/operators/str_eq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ namespace modsecurity {
namespace operators {

bool StrEq::evaluate(Transaction *transaction, const std::string &str) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);
bool eq = !p.compare(str);

if (negation) {
return !eq;
}

return eq;
}

Expand Down
6 changes: 1 addition & 5 deletions src/operators/str_match.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ namespace operators {


bool StrMatch::evaluate(Transaction *transaction, const std::string &input) {
std::string p = MacroExpansion::expand(param, transaction);
std::string p = MacroExpansion::expand(m_param, transaction);
bool ret = input.find(p) != std::string::npos;

if (negation) {
return !ret;
}

return ret;
}

Expand Down
Loading

0 comments on commit 8757840

Please sign in to comment.