Skip to content

Commit c721e10

Browse files
author
Felipe Zimmerle
committed
Adds request IDs and URIs to the debug log
1 parent 0e8cd76 commit c721e10

File tree

8 files changed

+63
-38
lines changed

8 files changed

+63
-38
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.0.3 - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- Adds request IDs and URIs to the debug log
5+
[Issue #1627 - @defanator, @zimmerle]
46
- Treating variables exception on load-time instead of run time.
57
[0x028e0 and 0x275a1 - @zimmerle]
68
- Fix: function m.setvar in Lua scripts and add testcases

headers/modsecurity/debug_log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class DebugLog {
4141
virtual ~DebugLog();
4242

4343
virtual void write(int level, const std::string &msg);
44+
virtual void write(int level, const std::string &id,
45+
const std::string &uri, const std::string &msg);
4446
bool isLogFileSet();
4547
bool isLogLevelSet();
4648
void setDebugLogLevel(int level);

headers/modsecurity/rules.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class Rules : public RulesProperties {
7878
int evaluate(int phase, Transaction *transaction);
7979
std::string getParserError();
8080

81-
void debug(int level, std::string message);
81+
void debug(int level, const std::string &id, const std::string &uri,
82+
const std::string &msg);
8283

8384
int64_t unicode_codepage;
8485

src/debug_log/debug_log.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ int DebugLog::getDebugLogLevel() {
7272
}
7373

7474

75+
void DebugLog::write(int level, const std::string &id,
76+
const std::string &uri, const std::string &msg) {
77+
if (level <= m_debugLevel) {
78+
std::string msgf = "[" + std::to_string(level) + "] " + msg;
79+
msgf = "[" + id + "] [" + uri + "] " + msgf;
80+
81+
DebugLogWriter &d = DebugLogWriter::getInstance();
82+
d.write_log(m_fileName, msgf);
83+
}
84+
}
85+
86+
7587
void DebugLog::write(int level, const std::string &msg) {
7688
if (level <= m_debugLevel) {
7789
std::string msgf = "[" + std::to_string(level) + "] " + msg;

src/rules.cc

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -151,69 +151,69 @@ std::string Rules::getParserError() {
151151
}
152152

153153

154-
int Rules::evaluate(int phase, Transaction *transaction) {
154+
int Rules::evaluate(int phase, Transaction *t) {
155155
if (phase > modsecurity::Phases::NUMBER_OF_PHASES) {
156156
return 0;
157157
}
158158

159159
std::vector<Rule *> rules = m_rules[phase];
160160

161-
debug(9, "This phase consists of " + std::to_string(rules.size()) + \
162-
" rule(s).");
161+
t->debug(9, "This phase consists of " \
162+
+ std::to_string(rules.size()) + " rule(s).");
163163

164-
if (transaction->m_allowType == actions::disruptive::FromNowOnAllowType
164+
if (t->m_allowType == actions::disruptive::FromNowOnAllowType
165165
&& phase != modsecurity::Phases::LoggingPhase) {
166-
debug(9, "Skipping all rules evaluation on this phase as request " \
166+
t->debug(9, "Skipping all rules evaluation on this phase as request " \
167167
"through the utilization of an `allow' action.");
168168
return true;
169169
}
170-
if (transaction->m_allowType == actions::disruptive::RequestAllowType
170+
if (t->m_allowType == actions::disruptive::RequestAllowType
171171
&& phase <= modsecurity::Phases::RequestBodyPhase) {
172-
debug(9, "Skipping all rules evaluation on this phase as request " \
172+
t->debug(9, "Skipping all rules evaluation on this phase as request " \
173173
"through the utilization of an `allow' action.");
174174
return true;
175175
}
176-
if (transaction->m_allowType != actions::disruptive::NoneAllowType) {
177-
transaction->m_allowType = actions::disruptive::NoneAllowType;
176+
if (t->m_allowType != actions::disruptive::NoneAllowType) {
177+
t->m_allowType = actions::disruptive::NoneAllowType;
178178
}
179179

180180
for (int i = 0; i < rules.size(); i++) {
181181
Rule *rule = rules[i];
182-
if (transaction->m_marker.empty() == false) {
182+
if (t->m_marker.empty() == false) {
183183
#ifndef NO_LOGS
184-
debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
185-
+ "' due to a SecMarker: " + transaction->m_marker);
184+
t->debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
185+
+ "' due to a SecMarker: " + t->m_marker);
186186
m_secmarker_skipped++;
187-
debug(9, "Rule: " + rule->m_marker);
187+
t->debug(9, "Rule: " + rule->m_marker);
188188
#endif
189-
if (rule->m_secMarker && rule->m_marker == transaction->m_marker) {
189+
if (rule->m_secMarker && rule->m_marker == t->m_marker) {
190190
#ifndef NO_LOGS
191-
debug(4, "Out of a SecMarker after skip " \
191+
t->debug(4, "Out of a SecMarker after skip " \
192192
+ std::to_string(m_secmarker_skipped) + " rules.");
193193
#endif
194-
transaction->m_marker.clear();
194+
t->m_marker.clear();
195195
#ifndef NO_LOGS
196196
m_secmarker_skipped = 0;
197197
#endif
198198
}
199-
} else if (transaction->m_skip_next > 0) {
200-
transaction->m_skip_next--;
201-
debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
199+
} else if (t->m_skip_next > 0) {
200+
t->m_skip_next--;
201+
t->debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
202202
+ "' due to a `skip' action. Still " + \
203-
std::to_string(transaction->m_skip_next) + " to be skipped.");
204-
} else if (transaction->m_allowType
203+
std::to_string(t->m_skip_next) + " to be skipped.");
204+
} else if (t->m_allowType
205205
!= actions::disruptive::NoneAllowType) {
206-
debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
206+
t->debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
207207
+ "' as request trough the utilization of an `allow' action.");
208208
} else if (m_exceptions.contains(rule->m_ruleId)) {
209-
debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
209+
t->debug(9, "Skipped rule id '" + std::to_string(rule->m_ruleId) \
210210
+ "'. Removed by an SecRuleRemove directive.");
211211
} else {
212212
bool remove_rule = false;
213213
if (m_exceptions.m_remove_rule_by_msg.empty() == false) {
214214
for (auto &z : m_exceptions.m_remove_rule_by_msg) {
215-
if (rule->containsMsg(z, transaction) == true) {
216-
debug(9, "Skipped rule id '" \
215+
if (rule->containsMsg(z, t) == true) {
216+
t->debug(9, "Skipped rule id '" \
217217
+ std::to_string(rule->m_ruleId) \
218218
+ "'. Removed by a SecRuleRemoveByMsg directive.");
219219
remove_rule = true;
@@ -227,8 +227,8 @@ int Rules::evaluate(int phase, Transaction *transaction) {
227227

228228
if (m_exceptions.m_remove_rule_by_tag.empty() == false) {
229229
for (auto &z : m_exceptions.m_remove_rule_by_tag) {
230-
if (rule->containsTag(z, transaction) == true) {
231-
debug(9, "Skipped rule id '" \
230+
if (rule->containsTag(z, t) == true) {
231+
t->debug(9, "Skipped rule id '" \
232232
+ std::to_string(rule->m_ruleId) \
233233
+ "'. Removed by a SecRuleRemoveByTag directive.");
234234
remove_rule = true;
@@ -240,19 +240,19 @@ int Rules::evaluate(int phase, Transaction *transaction) {
240240
}
241241
}
242242

243-
for (auto &z : transaction->m_ruleRemoveByTag) {
244-
if (rule->containsTag(z, transaction) == true) {
245-
debug(9, "Skipped rule id '" \
243+
for (auto &z : t->m_ruleRemoveByTag) {
244+
if (rule->containsTag(z, t) == true) {
245+
t->debug(9, "Skipped rule id '" \
246246
+ std::to_string(rule->m_ruleId) \
247247
+ "'. Skipped due to a ruleRemoveByTag action.");
248248
remove_rule = true;
249249
break;
250250
}
251251
}
252252

253-
rule->evaluate(transaction, NULL);
254-
if (transaction->m_it.disruptive == true) {
255-
debug(8, "Skipping this phase as this " \
253+
rule->evaluate(t, NULL);
254+
if (t->m_it.disruptive == true) {
255+
t->debug(8, "Skipping this phase as this " \
256256
"request was already intercepted.");
257257
break;
258258
}
@@ -284,10 +284,10 @@ int Rules::merge(Rules *from) {
284284
}
285285

286286

287-
288-
void Rules::debug(int level, std::string message) {
287+
void Rules::debug(int level, const std::string &id,
288+
const std::string &uri, const std::string &msg) {
289289
if (m_debugLog != NULL) {
290-
m_debugLog->write(level, message);
290+
m_debugLog->write(level, id, uri, msg);
291291
}
292292
}
293293

src/transaction.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void Transaction::debug(int level, std::string message) const {
186186
return;
187187
}
188188

189-
m_rules->debug(level, message);
189+
m_rules->debug(level, m_id, m_uri, message);
190190
}
191191
#endif
192192

test/regression/custom_debug_log.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ void CustomDebugLog::write(int level, const std::string& message) {
2929
m_log << "[" << level << "] " << message << std::endl;
3030
}
3131

32+
void CustomDebugLog::write(int level, const std::string &id,
33+
const std::string &uri, const std::string &msg) {
34+
std::string msgf = "[" + std::to_string(level) + "] " + msg;
35+
msgf = "[" + id + "] [" + uri + "] " + msgf;
36+
m_log << msgf << std::endl;
37+
}
3238

3339
bool CustomDebugLog::contains(const std::string& pattern) {
3440
modsecurity::Utils::Regex re(pattern);

test/regression/custom_debug_log.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class CustomDebugLog : public modsecurity::debug_log::DebugLog {
2929
~CustomDebugLog();
3030

3131
void write(int level, const std::string& message) override;
32+
void write(int level, const std::string &id,
33+
const std::string &uri, const std::string &msg) override;
3234
bool contains(const std::string& pattern);
3335
std::string log_messages();
3436
std::string error_log_messages();

0 commit comments

Comments
 (0)