Skip to content

Commit

Permalink
Makes RuleWithActions const in run time operations
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmerle committed Jan 6, 2020
1 parent 79432b3 commit b27e19f
Show file tree
Hide file tree
Showing 91 changed files with 192 additions and 185 deletions.
2 changes: 1 addition & 1 deletion headers/modsecurity/actions/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Action {
}

virtual bool execute(Transaction *transaction) noexcept { return true; };
inline virtual bool executeAsDefaulAction(Transaction *transaction, RuleWithActions *r) noexcept {
inline virtual bool executeAsDefaulAction(Transaction *transaction, const RuleWithActions *r) noexcept {
return execute(transaction);
};

Expand Down
14 changes: 7 additions & 7 deletions headers/modsecurity/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,29 @@ class Rule {
m_phase(r.m_phase)
{ };

virtual bool evaluate(Transaction *transaction) = 0;
virtual bool evaluate(Transaction *transaction) const = 0;

std::shared_ptr<std::string> getFileName() {
std::shared_ptr<std::string> getFileName() const {
return m_fileName;
}

int getLineNumber() {
int getLineNumber() const {
return m_lineNumber;
}

int getPhase() { return m_phase; }
int getPhase() const { return m_phase; }
void setPhase(int phase) { m_phase = phase; }

virtual std::string getReference() {
virtual std::string getReference() const {
return *m_fileName + ":" + std::to_string(m_lineNumber);
}

virtual void dump(std::stringstream &out) {
virtual void dump(std::stringstream &out) const {
out << getOriginInTextFormat() << std::endl;
}

protected:
std::string getOriginInTextFormat() {
std::string getOriginInTextFormat() const {
std::stringstream ss;
ss << "# File name: " << *getFileName() << std::endl;
ss << "# Line number: " << getLineNumber();
Expand Down
6 changes: 3 additions & 3 deletions headers/modsecurity/rule_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class RuleMessage {
static std::string _details(RuleMessage *rm);
static std::string _errorLogTail(RuleMessage *rm);

RuleWithActions *getRule() {
const RuleWithActions *getRule() {
return m_rule;
}
void setRule(RuleWithActions *rule) {
void setRule(const RuleWithActions *rule) {
m_rule = rule;
}

Expand Down Expand Up @@ -159,7 +159,7 @@ class RuleMessage {
bool m_saveMessage:1;
bool m_isSettle:1;
Transaction *m_transaction;
RuleWithActions *m_rule;
const RuleWithActions *m_rule;
};


Expand Down
2 changes: 1 addition & 1 deletion headers/modsecurity/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class TransactionRuleMessageManagement {

RuleMessage *messageGetLast();
void messageNew();
void messageLog(RuleWithActions *rule);
void messageLog(const RuleWithActions *rule);

void messageSetNoAuditLog(bool a) {
m_noAuditLog = a;
Expand Down
7 changes: 4 additions & 3 deletions src/engine/lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ const char *Lua::blob_reader(lua_State *L, void *ud, size_t *size) {
#endif


int Lua::run(Transaction *t, const std::string &str) {
int Lua::run(Transaction *t, const std::string &str) const {
#ifdef WITH_LUA
LuaScriptBlob blob = m_blob;
std::string luaRet;
const char *a = NULL;
int ret = true;
Expand All @@ -140,9 +141,9 @@ int Lua::run(Transaction *t, const std::string &str) {
lua_setglobal(L, "m");

#ifdef WITH_LUA_5_1
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str());
int rc = lua_load(L, Lua::blob_reader, &blob, m_scriptName.c_str());
#else
int rc = lua_load(L, Lua::blob_reader, &m_blob, m_scriptName.c_str(),
int rc = lua_load(L, Lua::blob_reader, &blob, m_scriptName.c_str(),
NULL);
#endif
if (rc != LUA_OK) {
Expand Down
13 changes: 10 additions & 3 deletions src/engine/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ class LuaScriptBlob {
public:
LuaScriptBlob() :
m_data(NULL),
m_len(0) { }
m_len(0)
{ };

~LuaScriptBlob() {
if (m_data) {
free(m_data);
m_data = NULL;
}
}
};

LuaScriptBlob(const LuaScriptBlob &lua) {
m_data = reinterpret_cast<unsigned char *>(std::malloc(lua.m_len));
std::memcpy(m_data, lua.m_data, lua.m_len);
m_len = lua.m_len;
};

void write(const void *data, size_t len) {
unsigned char *d = NULL;
Expand All @@ -69,7 +75,8 @@ class Lua {
Lua() { }

bool load(std::string script, std::string *err);
int run(Transaction *t, const std::string &str_param = "");

int run(Transaction *t, const std::string &str_param = "") const;
static bool isCompatible(std::string script, Lua *l, std::string *error);

#ifdef WITH_LUA
Expand Down
2 changes: 1 addition & 1 deletion src/operators/begins_with.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace operators {


bool BeginsWith::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
std::string p(m_string->evaluate(transaction));
Expand Down
2 changes: 1 addition & 1 deletion src/operators/begins_with.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BeginsWith : public Operator {
: Operator("BeginsWith", std::move(param)) { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/contains.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace modsecurity {
namespace operators {

bool Contains::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
std::string p(m_string->evaluate(transaction));
Expand Down
2 changes: 1 addition & 1 deletion src/operators/contains.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Contains : public Operator {
: Operator("Contains", std::move(param)) { };

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
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 @@ -37,7 +37,7 @@ bool ContainsWord::acceptableChar(const bpstd::string_view &a, size_t pos) {
}

bool ContainsWord::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &inputView,
RuleMessage *ruleMessage) {
std::string paramTarget(m_string->evaluate(transaction));
Expand Down
2 changes: 1 addition & 1 deletion src/operators/contains_word.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ContainsWord : public Operator {
: Operator("ContainsWord", std::move(param)) { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;

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


bool DetectSQLi::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
char fingerprint[8];
Expand Down
2 changes: 1 addition & 1 deletion src/operators/detect_sqli.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DetectSQLi : public Operator {
}

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/detect_xss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace operators {


bool DetectXSS::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
int is_xss;
Expand Down
2 changes: 1 addition & 1 deletion src/operators/detect_xss.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DetectXSS : public Operator {
}

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/ends_with.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace operators {


bool EndsWith::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
bool ret = false;
Expand Down
2 changes: 1 addition & 1 deletion src/operators/ends_with.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EndsWith : public Operator {
}

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/eq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace operators {


bool Eq::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) {
int p = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/operators/eq.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Eq : public Operator {
: Operator("Eq", std::move(param)) { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/fuzzy_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ FuzzyHash::~FuzzyHash() {


bool FuzzyHash::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
#ifdef WITH_SSDEEP
Expand Down
2 changes: 1 addition & 1 deletion src/operators/fuzzy_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FuzzyHash : public Operator {
~FuzzyHash();

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;

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

bool Ge::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
std::string p(m_string->evaluate(transaction));
Expand Down
2 changes: 1 addition & 1 deletion src/operators/ge.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Ge : public Operator {
}

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/geo_lookup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool GeoLookup::debug(Transaction *transaction, int x, const bpstd::string_view


bool GeoLookup::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
using std::placeholders::_1;
Expand Down
2 changes: 1 addition & 1 deletion src/operators/geo_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GeoLookup : public Operator {
: Operator("GeoLookup") { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;

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


bool GsbLookup::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/operators/gsblookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GsbLookup : public Operator {
: Operator("GsbLookup", std::move(param)) { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/gt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace modsecurity {
namespace operators {

bool Gt::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
std::string p(m_string->evaluate(transaction));
Expand Down
2 changes: 1 addition & 1 deletion src/operators/gt.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Gt : public Operator {
}

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;
};
Expand Down
2 changes: 1 addition & 1 deletion src/operators/inspect_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool InspectFile::init(const std::string &param2, std::string *error) {


bool InspectFile::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
if (m_isScript) {
Expand Down
2 changes: 1 addition & 1 deletion src/operators/inspect_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class InspectFile : public Operator {
bool init(const std::string &file, std::string *error) override;

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;

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 @@ -38,7 +38,7 @@ bool IpMatch::init(const std::string &file, std::string *error) {


bool IpMatch::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
return m_tree.contains(str.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/operators/ip_match.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class IpMatch : public Operator {
: Operator(n, std::move(param)) { }

bool evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &input,
RuleMessage *ruleMessage) override;

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

bool Le::evaluate(Transaction *transaction,
RuleWithActions *rule,
const RuleWithActions *rule,
const bpstd::string_view &str,
RuleMessage *ruleMessage) {
std::string p(m_string->evaluate(transaction));
Expand Down
Loading

0 comments on commit b27e19f

Please sign in to comment.