Skip to content

Commit 2ad87f6

Browse files
committed
Reference RuleWithActions & Transaction object instead of copying values in RuleMessage
- Because the lifetime of the RuleMessage instances do not extend beyond the lifetime of the enclosing RuleWithActions & Transaction, RuleMessage can just reference it and simplify its definition. - Additionally, make the references const to show that it doesn't modify it. - Replace RuleMessage copy constructor with default implementations. - Removed unused RuleMessage assignment operator (which cannot be implemented now that it has reference members). - Removed constructor from RuleMessage pointer. - Addressed Sonarcloud suggestions: Do not use the constructor's initializer list for data member "xxx". Use the in-class initializer instead.
1 parent 2ec640f commit 2ad87f6

File tree

11 files changed

+85
-205
lines changed

11 files changed

+85
-205
lines changed

examples/reading_logs_via_rule_message/reading_logs_via_rule_message.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ class ReadingLogsViaRuleMessage {
155155
const modsecurity::RuleMessage *ruleMessage = \
156156
reinterpret_cast<const modsecurity::RuleMessage *>(ruleMessagev);
157157

158-
std::cout << "Rule Id: " << std::to_string(ruleMessage->m_ruleId);
159-
std::cout << " phase: " << std::to_string(ruleMessage->m_phase);
158+
std::cout << "Rule Id: " << std::to_string(ruleMessage->m_rule.m_ruleId);
159+
std::cout << " phase: " << std::to_string(ruleMessage->getPhase());
160160
std::cout << std::endl;
161161
if (ruleMessage->m_isDisruptive) {
162162
std::cout << " * Disruptive action: ";

examples/using_bodies_in_chunks/simple_request.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ static void logCb(void *data, const void *ruleMessagev) {
7676
const modsecurity::RuleMessage *ruleMessage = \
7777
reinterpret_cast<const modsecurity::RuleMessage *>(ruleMessagev);
7878

79-
std::cout << "Rule Id: " << std::to_string(ruleMessage->m_ruleId);
80-
std::cout << " phase: " << std::to_string(ruleMessage->m_phase);
79+
std::cout << "Rule Id: " << std::to_string(ruleMessage->m_rule.m_ruleId);
80+
std::cout << " phase: " << std::to_string(ruleMessage->getPhase());
8181
std::cout << std::endl;
8282
if (ruleMessage->m_isDisruptive) {
8383
std::cout << " * Disruptive action: ";

headers/modsecurity/rule.h

+5-8
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ using MatchActions = std::vector<actions::Action *>;
6565
class Rule {
6666
public:
6767
Rule(std::unique_ptr<std::string> fileName, int lineNumber)
68-
: m_fileName(std::make_shared<std::string>(*fileName)),
68+
: m_fileName(*fileName),
6969
m_lineNumber(lineNumber),
7070
m_phase(modsecurity::Phases::RequestHeadersPhase) {
7171
}
@@ -81,7 +81,7 @@ class Rule {
8181
virtual bool evaluate(Transaction *transaction,
8282
std::shared_ptr<RuleMessage> rm) = 0;
8383

84-
std::shared_ptr<std::string> getFileName() const {
84+
const std::string& getFileName() const {
8585
return m_fileName;
8686
}
8787

@@ -93,18 +93,15 @@ class Rule {
9393
void setPhase(int phase) { m_phase = phase; }
9494

9595
virtual std::string getReference() {
96-
if (m_fileName) {
97-
return *m_fileName + ":" + std::to_string(m_lineNumber);
98-
}
99-
return "<<no file>>:" + std::to_string(m_lineNumber);
96+
return m_fileName + ":" + std::to_string(m_lineNumber);
10097
}
10198

10299

103100
virtual bool isMarker() { return false; }
104101

105102
private:
106-
std::shared_ptr<std::string> m_fileName;
107-
int m_lineNumber;
103+
const std::string m_fileName;
104+
const int m_lineNumber;
108105
// FIXME: phase may not be neede to SecMarker.
109106
int m_phase;
110107
};

headers/modsecurity/rule_message.h

+12-126
Original file line numberDiff line numberDiff line change
@@ -42,124 +42,20 @@ class RuleMessage {
4242
ClientLogMessageInfo = 4
4343
};
4444

45-
/**
46-
*
47-
* FIXME: RuleMessage is currently too big, doing a lot of
48-
* unnecessary data duplication. Needs to be shrink down.
49-
*
50-
*/
51-
RuleMessage(RuleWithActions *rule, Transaction *trans) :
52-
m_accuracy(rule->m_accuracy),
53-
m_clientIpAddress(trans->m_clientIpAddress),
54-
m_data(""),
55-
m_id(trans->m_id),
56-
m_isDisruptive(false),
57-
m_match(""),
58-
m_maturity(rule->m_maturity),
59-
m_message(""),
60-
m_noAuditLog(false),
61-
m_phase(rule->getPhase() - 1),
62-
m_reference(""),
63-
m_rev(rule->m_rev),
45+
RuleMessage(const RuleWithActions &rule, const Transaction &trans) :
6446
m_rule(rule),
65-
m_ruleFile(rule->getFileName()),
66-
m_ruleId(rule->m_ruleId),
67-
m_ruleLine(rule->getLineNumber()),
68-
m_saveMessage(true),
69-
m_serverIpAddress(trans->m_serverIpAddress),
70-
m_requestHostName(trans->m_requestHostName),
71-
m_severity(0),
72-
m_uriNoQueryStringDecoded(trans->m_uri_no_query_string_decoded),
73-
m_ver(rule->m_ver),
74-
m_tags()
47+
m_transaction(trans)
7548
{ }
7649

77-
explicit RuleMessage(RuleMessage *rule) :
78-
m_accuracy(rule->m_accuracy),
79-
m_clientIpAddress(rule->m_clientIpAddress),
80-
m_data(rule->m_data),
81-
m_id(rule->m_id),
82-
m_isDisruptive(rule->m_isDisruptive),
83-
m_match(rule->m_match),
84-
m_maturity(rule->m_maturity),
85-
m_message(rule->m_message),
86-
m_noAuditLog(rule->m_noAuditLog),
87-
m_phase(rule->m_phase),
88-
m_reference(rule->m_reference),
89-
m_rev(rule->m_rev),
90-
m_rule(rule->m_rule),
91-
m_ruleFile(rule->m_ruleFile),
92-
m_ruleId(rule->m_ruleId),
93-
m_ruleLine(rule->m_ruleLine),
94-
m_saveMessage(rule->m_saveMessage),
95-
m_serverIpAddress(rule->m_serverIpAddress),
96-
m_requestHostName(rule->m_requestHostName),
97-
m_severity(rule->m_severity),
98-
m_uriNoQueryStringDecoded(rule->m_uriNoQueryStringDecoded),
99-
m_ver(rule->m_ver),
100-
m_tags(rule->m_tags)
101-
{ }
102-
103-
RuleMessage(const RuleMessage& ruleMessage)
104-
: m_accuracy(ruleMessage.m_accuracy),
105-
m_clientIpAddress(ruleMessage.m_clientIpAddress),
106-
m_data(ruleMessage.m_data),
107-
m_id(ruleMessage.m_id),
108-
m_isDisruptive(ruleMessage.m_isDisruptive),
109-
m_match(ruleMessage.m_match),
110-
m_maturity(ruleMessage.m_maturity),
111-
m_message(ruleMessage.m_message),
112-
m_noAuditLog(ruleMessage.m_noAuditLog),
113-
m_phase(ruleMessage.m_phase),
114-
m_reference(ruleMessage.m_reference),
115-
m_rev(ruleMessage.m_rev),
116-
m_rule(ruleMessage.m_rule),
117-
m_ruleFile(ruleMessage.m_ruleFile),
118-
m_ruleId(ruleMessage.m_ruleId),
119-
m_ruleLine(ruleMessage.m_ruleLine),
120-
m_saveMessage(ruleMessage.m_saveMessage),
121-
m_serverIpAddress(ruleMessage.m_serverIpAddress),
122-
m_requestHostName(ruleMessage.m_requestHostName),
123-
m_severity(ruleMessage.m_severity),
124-
m_uriNoQueryStringDecoded(ruleMessage.m_uriNoQueryStringDecoded),
125-
m_ver(ruleMessage.m_ver),
126-
m_tags(ruleMessage.m_tags)
127-
{ }
128-
129-
RuleMessage &operator=(const RuleMessage& ruleMessage) {
130-
m_accuracy = ruleMessage.m_accuracy;
131-
m_clientIpAddress = ruleMessage.m_clientIpAddress;
132-
m_data = ruleMessage.m_data;
133-
m_id = ruleMessage.m_id;
134-
m_isDisruptive = ruleMessage.m_isDisruptive;
135-
m_match = ruleMessage.m_match;
136-
m_maturity = ruleMessage.m_maturity;
137-
m_message = ruleMessage.m_message;
138-
m_noAuditLog = ruleMessage.m_noAuditLog;
139-
m_phase = ruleMessage.m_phase;
140-
m_reference = ruleMessage.m_reference;
141-
m_rev = ruleMessage.m_rev;
142-
m_rule = ruleMessage.m_rule;
143-
m_ruleFile = ruleMessage.m_ruleFile;
144-
m_ruleId = ruleMessage.m_ruleId;
145-
m_ruleLine = ruleMessage.m_ruleLine;
146-
m_saveMessage = ruleMessage.m_saveMessage;
147-
m_serverIpAddress = ruleMessage.m_serverIpAddress;
148-
m_requestHostName = ruleMessage.m_requestHostName;
149-
m_severity = ruleMessage.m_severity;
150-
m_uriNoQueryStringDecoded = ruleMessage.m_uriNoQueryStringDecoded;
151-
m_ver = ruleMessage.m_ver;
152-
m_tags = ruleMessage.m_tags;
153-
return *this;
154-
}
50+
RuleMessage(const RuleMessage &ruleMessage) = default;
51+
RuleMessage &operator=(const RuleMessage &ruleMessage) = delete;
15552

15653
void clean() {
15754
m_data = "";
15855
m_match = "";
15956
m_isDisruptive = false;
16057
m_reference = "";
16158
m_severity = 0;
162-
m_ver = "";
16359
}
16460

16561
std::string log() {
@@ -187,28 +83,18 @@ class RuleMessage {
18783
static std::string _details(const RuleMessage *rm);
18884
static std::string _errorLogTail(const RuleMessage *rm);
18985

190-
int m_accuracy;
191-
std::shared_ptr<std::string> m_clientIpAddress;
86+
int getPhase() const { return m_rule.getPhase() - 1; }
87+
88+
const RuleWithActions &m_rule;
89+
const Transaction &m_transaction;
19290
std::string m_data;
193-
std::shared_ptr<std::string> m_id;
194-
bool m_isDisruptive;
91+
bool m_isDisruptive = false;
19592
std::string m_match;
196-
int m_maturity;
19793
std::string m_message;
198-
bool m_noAuditLog;
199-
int m_phase;
94+
bool m_noAuditLog = false;
20095
std::string m_reference;
201-
std::string m_rev;
202-
RuleWithActions *m_rule;
203-
std::shared_ptr<std::string> m_ruleFile;
204-
int m_ruleId;
205-
int m_ruleLine;
206-
bool m_saveMessage;
207-
std::shared_ptr<std::string> m_serverIpAddress;
208-
std::shared_ptr<std::string> m_requestHostName;
209-
int m_severity;
210-
std::shared_ptr<std::string> m_uriNoQueryStringDecoded;
211-
std::string m_ver;
96+
bool m_saveMessage = true;
97+
int m_severity = 0;
21298

21399
std::list<std::string> m_tags;
214100
};

headers/modsecurity/rule_with_actions.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class RuleWithActions : public Rule {
7676
void performLogging(Transaction *trans,
7777
std::shared_ptr<RuleMessage> ruleMessage,
7878
bool lastLog = true,
79-
bool chainedParentNull = false);
79+
bool chainedParentNull = false) const;
8080

8181
std::vector<actions::Action *> getActionsByName(const std::string& name,
8282
Transaction *t);

headers/modsecurity/transaction.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef struct Rules_t RulesSet;
5757
#define ms_dbg(b, c) \
5858
do { \
5959
if (m_rules && m_rules->m_debugLog && m_rules->m_debugLog->m_debugLevel >= b) { \
60-
m_rules->debug(b, *m_id.get(), m_uri, c); \
60+
m_rules->debug(b, m_id, m_uri, c); \
6161
} \
6262
} while (0);
6363
#else
@@ -431,7 +431,7 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
431431
/**
432432
* Holds the client IP address.
433433
*/
434-
std::shared_ptr<std::string> m_clientIpAddress;
434+
std::string m_clientIpAddress;
435435

436436
/**
437437
* Holds the HTTP version: 1.2, 2.0, 3.0 and so on....
@@ -441,12 +441,12 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
441441
/**
442442
* Holds the server IP Address
443443
*/
444-
std::shared_ptr<std::string> m_serverIpAddress;
444+
std::string m_serverIpAddress;
445445

446446
/**
447447
* Holds the request's hostname
448448
*/
449-
std::shared_ptr<std::string> m_requestHostName;
449+
std::string m_requestHostName;
450450

451451
/**
452452
* Holds the raw URI that was requested.
@@ -456,7 +456,7 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
456456
/**
457457
* Holds the URI that was requests (without the query string).
458458
*/
459-
std::shared_ptr<std::string> m_uri_no_query_string_decoded;
459+
std::string m_uri_no_query_string_decoded;
460460

461461
/**
462462
* Holds the combined size of all arguments, later used to fill the
@@ -568,7 +568,7 @@ class Transaction : public TransactionAnchoredVariables, public TransactionSecMa
568568
* Contains the unique ID of the transaction. Use by the variable
569569
* `UNIQUE_ID'. This unique id is also saved as part of the AuditLog.
570570
*/
571-
std::shared_ptr<std::string> m_id;
571+
std::string m_id;
572572

573573
/**
574574
* Holds the amount of rules that should be skipped. If bigger than 0 the

src/audit_log/writer/parallel.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ bool Parallel::write(Transaction *transaction, int parts, std::string *error) {
123123
}
124124

125125
const auto &logPath = m_audit->m_storage_dir;
126-
fileName = logPath + fileName + "-" + *transaction->m_id.get();
126+
fileName = logPath + fileName + "-" + transaction->m_id;
127127

128128
if (logPath.empty()) {
129129
error->assign("Log path is not valid.");

src/request_body_processor/multipart.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void MultipartPartTmpFile::Open() {
7474
strftime(tstr, std::size(tstr), "/%Y%m%d-%H%M%S", &timeinfo);
7575

7676
std::string path = m_transaction->m_rules->m_uploadDirectory.m_value;
77-
path = path + tstr + "-" + *m_transaction->m_id.get();
77+
path = path + tstr + "-" + m_transaction->m_id;
7878
path += "-file-XXXXXX";
7979

8080
#ifndef WIN32

src/rule_message.cc

+16-16
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ namespace modsecurity {
2626
std::string RuleMessage::_details(const RuleMessage *rm) {
2727
std::string msg;
2828

29-
msg.append(" [file \"" + std::string(*rm->m_ruleFile.get()) + "\"]");
30-
msg.append(" [line \"" + std::to_string(rm->m_ruleLine) + "\"]");
31-
msg.append(" [id \"" + std::to_string(rm->m_ruleId) + "\"]");
32-
msg.append(" [rev \"" + utils::string::toHexIfNeeded(rm->m_rev, true) + "\"]");
29+
msg.append(" [file \"" + rm->m_rule.getFileName() + "\"]");
30+
msg.append(" [line \"" + std::to_string(rm->m_rule.getLineNumber()) + "\"]");
31+
msg.append(" [id \"" + std::to_string(rm->m_rule.m_ruleId) + "\"]");
32+
msg.append(" [rev \"" + utils::string::toHexIfNeeded(rm->m_rule.m_rev, true) + "\"]");
3333
msg.append(" [msg \"" + rm->m_message + "\"]");
3434
msg.append(" [data \"" + utils::string::toHexIfNeeded(utils::string::limitTo(200, rm->m_data), true) + "\"]");
3535
msg.append(" [severity \"" +
3636
std::to_string(rm->m_severity) + "\"]");
37-
msg.append(" [ver \"" + utils::string::toHexIfNeeded(rm->m_ver, true) + "\"]");
38-
msg.append(" [maturity \"" + std::to_string(rm->m_maturity) + "\"]");
39-
msg.append(" [accuracy \"" + std::to_string(rm->m_accuracy) + "\"]");
37+
msg.append(" [ver \"" + utils::string::toHexIfNeeded(rm->m_rule.m_ver, true) + "\"]");
38+
msg.append(" [maturity \"" + std::to_string(rm->m_rule.m_maturity) + "\"]");
39+
msg.append(" [accuracy \"" + std::to_string(rm->m_rule.m_accuracy) + "\"]");
4040

4141
for (const auto &a : rm->m_tags) {
4242
msg.append(" [tag \"" + utils::string::toHexIfNeeded(a, true) + "\"]");
4343
}
4444

45-
msg.append(" [hostname \"" + *rm->m_requestHostName.get() + "\"]");
46-
47-
msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
48-
msg.append(" [unique_id \"" + *rm->m_id + "\"]");
45+
msg.append(" [hostname \"" + rm->m_transaction.m_requestHostName \
46+
+ "\"]");
47+
msg.append(" [uri \"" + utils::string::limitTo(200, rm->m_transaction.m_uri_no_query_string_decoded) + "\"]");
48+
msg.append(" [unique_id \"" + rm->m_transaction.m_id + "\"]");
4949
msg.append(" [ref \"" + utils::string::limitTo(200, rm->m_reference) + "\"]");
5050

5151
return msg;
@@ -55,9 +55,9 @@ std::string RuleMessage::_details(const RuleMessage *rm) {
5555
std::string RuleMessage::_errorLogTail(const RuleMessage *rm) {
5656
std::string msg;
5757

58-
msg.append("[hostname \"" + *rm->m_serverIpAddress.get() + "\"]");
59-
msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
60-
msg.append(" [unique_id \"" + *rm->m_id + "\"]");
58+
msg.append("[hostname \"" + rm->m_transaction.m_serverIpAddress + "\"]");
59+
msg.append(" [uri \"" + utils::string::limitTo(200, rm->m_transaction.m_uri_no_query_string_decoded) + "\"]");
60+
msg.append(" [unique_id \"" + rm->m_transaction.m_id + "\"]");
6161

6262
return msg;
6363
}
@@ -68,7 +68,7 @@ std::string RuleMessage::log(const RuleMessage *rm, int props, int code) {
6868
msg.reserve(2048);
6969

7070
if (props & ClientLogMessageInfo) {
71-
msg.append("[client " + std::string(*rm->m_clientIpAddress.get()) + "] ");
71+
msg.append("[client " + rm->m_transaction.m_clientIpAddress + "] ");
7272
}
7373

7474
if (rm->m_isDisruptive) {
@@ -79,7 +79,7 @@ std::string RuleMessage::log(const RuleMessage *rm, int props, int code) {
7979
msg.append(std::to_string(code));
8080
}
8181
msg.append(" (phase ");
82-
msg.append(std::to_string(rm->m_rule->getPhase() - 1) + "). ");
82+
msg.append(std::to_string(rm->getPhase()) + "). ");
8383
} else {
8484
msg.append("ModSecurity: Warning. ");
8585
}

0 commit comments

Comments
 (0)