Skip to content

Commit 6fe8655

Browse files
author
Felipe Zimmerle
committed
Adds support for RunTimeString
Using RunTimeStrings instead of runtime parser for macro expansion.
1 parent cd30509 commit 6fe8655

24 files changed

+6793
-6415
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ libmodsecurity_la_SOURCES = \
272272
debug_log/debug_log.cc \
273273
debug_log/debug_log_writer.cc \
274274
macro_expansion.cc \
275+
run_time_string.cc \
275276
rule.cc \
276277
rule_message.cc \
277278
rule_script.cc \

src/actions/disruptive/redirect.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,14 @@ namespace disruptive {
3030

3131

3232
bool Redirect::init(std::string *error) {
33-
m_url = m_parser_payload;
34-
m_url = utils::string::parserSanitizer(m_url);
3533
m_status = 302;
3634
return true;
3735
}
3836

3937

4038
bool Redirect::evaluate(Rule *rule, Transaction *transaction,
41-
std::shared_ptr<RuleMessage> rm) {
42-
m_urlExpanded = MacroExpansion::expand(m_url, transaction);
43-
39+
std::shared_ptr<RuleMessage> rm) {
40+
std::string m_urlExpanded(m_string->evaluate(transaction));
4441
/* if it was changed before, lets keep it. */
4542
if (transaction->m_it.status == 200) {
4643
transaction->m_it.status = m_status;

src/actions/disruptive/redirect.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "modsecurity/actions/action.h"
2020
#include "modsecurity/rule_message.h"
21+
#include "src/run_time_string.h"
2122

2223
#ifndef SRC_ACTIONS_DISRUPTIVE_REDIRECT_H_
2324
#define SRC_ACTIONS_DISRUPTIVE_REDIRECT_H_
@@ -36,9 +37,11 @@ class Redirect : public Action {
3637
public:
3738
explicit Redirect(const std::string &action)
3839
: Action(action, RunTimeOnlyIfMatchKind),
39-
m_status(0),
40-
m_urlExpanded(""),
41-
m_url("") { }
40+
m_status(0) { }
41+
42+
explicit Redirect(std::unique_ptr<RunTimeString> z)
43+
: Action("redirert", RunTimeOnlyIfMatchKind),
44+
m_string(std::move(z)) { }
4245

4346
bool evaluate(Rule *rule, Transaction *transaction,
4447
std::shared_ptr<RuleMessage> rm) override;
@@ -47,8 +50,7 @@ class Redirect : public Action {
4750

4851
private:
4952
int m_status;
50-
std::string m_urlExpanded;
51-
std::string m_url;
53+
std::unique_ptr<RunTimeString> m_string;
5254
};
5355

5456

src/actions/init_col.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace actions {
3131
bool InitCol::init(std::string *error) {
3232
int posEquals = m_parser_payload.find("=");
3333

34-
if (m_parser_payload.size() < 8) {
34+
if (m_parser_payload.size() < 2) {
3535
error->assign("Something wrong with initcol format: too small");
3636
return false;
3737
}
@@ -42,7 +42,6 @@ bool InitCol::init(std::string *error) {
4242
}
4343

4444
m_collection_key = std::string(m_parser_payload, 0, posEquals);
45-
m_collection_value = std::string(m_parser_payload, posEquals + 1);
4645

4746
if (m_collection_key != "ip" &&
4847
m_collection_key != "global" &&
@@ -57,9 +56,7 @@ bool InitCol::init(std::string *error) {
5756

5857

5958
bool InitCol::evaluate(Rule *rule, Transaction *t) {
60-
std::string collectionName;
61-
collectionName = MacroExpansion::expand(m_collection_value, t);
62-
59+
std::string collectionName(m_string->evaluate(t));
6360

6461
if (m_collection_key == "ip") {
6562
t->m_collections.m_ip_collection_key = collectionName;

src/actions/init_col.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string>
1717

1818
#include "modsecurity/actions/action.h"
19+
#include "src/run_time_string.h"
1920

2021
#ifndef SRC_ACTIONS_INIT_COL_H_
2122
#define SRC_ACTIONS_INIT_COL_H_
@@ -31,11 +32,15 @@ class InitCol : public Action {
3132
public:
3233
explicit InitCol(std::string action) : Action(action) { }
3334

35+
InitCol(std::string action, std::unique_ptr<RunTimeString> z)
36+
: Action(action, RunTimeOnlyIfMatchKind),
37+
m_string(std::move(z)) { }
38+
3439
bool evaluate(Rule *rule, Transaction *transaction) override;
3540
bool init(std::string *error) override;
3641
private:
3742
std::string m_collection_key;
38-
std::string m_collection_value;
43+
std::unique_ptr<RunTimeString> m_string;
3944
};
4045

4146

src/actions/log_data.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ bool LogData::evaluate(Rule *rule, Transaction *transaction,
3939
}
4040

4141
std::string LogData::data(Transaction *transaction) {
42-
return MacroExpansion::expand(m_parser_payload, transaction);
42+
std::string a(m_string->evaluate(transaction));
43+
return a;
4344
}
4445

4546

src/actions/log_data.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <memory>
1818

1919
#include "modsecurity/actions/action.h"
20+
#include "src/run_time_string.h"
2021

2122
#ifndef SRC_ACTIONS_LOG_DATA_H_
2223
#define SRC_ACTIONS_LOG_DATA_H_
@@ -33,10 +34,16 @@ class LogData : public Action {
3334
explicit LogData(std::string action)
3435
: Action(action, RunTimeOnlyIfMatchKind) { }
3536

37+
explicit LogData(std::unique_ptr<RunTimeString> z)
38+
: Action("logdata", RunTimeOnlyIfMatchKind),
39+
m_string(std::move(z)) { }
40+
3641
bool evaluate(Rule *rule, Transaction *transaction,
3742
std::shared_ptr<RuleMessage> rm) override;
3843

3944
std::string data(Transaction *Transaction);
45+
46+
std::unique_ptr<RunTimeString> m_string;
4047
};
4148

4249

src/actions/msg.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ bool Msg::evaluate(Rule *rule, Transaction *transaction,
6161
}
6262

6363

64-
std::string Msg::data(Transaction *transaction) {
65-
return MacroExpansion::expand(m_parser_payload, transaction);
64+
std::string Msg::data(Transaction *t) {
65+
std::string a(m_string->evaluate(t));
66+
return a;
6667
}
6768

6869

src/actions/msg.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "modsecurity/actions/action.h"
2020
#include "modsecurity/rule_message.h"
21+
#include "src/run_time_string.h"
2122

2223
#ifndef SRC_ACTIONS_MSG_H_
2324
#define SRC_ACTIONS_MSG_H_
@@ -34,10 +35,15 @@ class Msg : public Action {
3435
explicit Msg(std::string action)
3536
: Action(action, RunTimeOnlyIfMatchKind) { }
3637

38+
explicit Msg(std::unique_ptr<RunTimeString> z)
39+
: Action("msg", RunTimeOnlyIfMatchKind),
40+
m_string(std::move(z)) { }
41+
3742
bool evaluate(Rule *rule, Transaction *transaction,
3843
std::shared_ptr<RuleMessage> rm) override;
3944

4045
std::string data(Transaction *Transaction);
46+
std::unique_ptr<RunTimeString> m_string;
4147
};
4248

4349

src/actions/set_rsc.cc

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,12 @@ namespace actions {
2828

2929

3030
bool SetRSC::init(std::string *error) {
31-
m_collection_key = std::string(m_parser_payload, 0,
32-
m_parser_payload.length());
33-
34-
if (m_collection_key.empty()) {
35-
error->assign("Missing collection key");
36-
return false;
37-
}
38-
3931
return true;
4032
}
4133

4234

4335
bool SetRSC::evaluate(Rule *rule, Transaction *t) {
44-
std::string colNameExpanded = MacroExpansion::expand(m_collection_key, t);
36+
std::string colNameExpanded(m_string->evaluate(t));
4537

4638
#ifndef NO_LOGS
4739
t->debug(8, "RESOURCE initiated with value: \'"

0 commit comments

Comments
 (0)