Skip to content

Commit 860b118

Browse files
authored
Merge pull request #2854 from airween/v3/logescape
Escape log field 'data' value
2 parents 62ec4ed + 6dd00be commit 860b118

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/rule_message.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ std::string RuleMessage::_details(const RuleMessage *rm) {
2929
msg.append(" [file \"" + std::string(*rm->m_ruleFile.get()) + "\"]");
3030
msg.append(" [line \"" + std::to_string(rm->m_ruleLine) + "\"]");
3131
msg.append(" [id \"" + std::to_string(rm->m_ruleId) + "\"]");
32-
msg.append(" [rev \"" + rm->m_rev + "\"]");
32+
msg.append(" [rev \"" + utils::string::toHexIfNeeded(rm->m_rev, true) + "\"]");
3333
msg.append(" [msg \"" + rm->m_message + "\"]");
34-
msg.append(" [data \"" + utils::string::limitTo(200, rm->m_data) + "\"]");
34+
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 \"" + rm->m_ver + "\"]");
37+
msg.append(" [ver \"" + utils::string::toHexIfNeeded(rm->m_ver, true) + "\"]");
3838
msg.append(" [maturity \"" + std::to_string(rm->m_maturity) + "\"]");
3939
msg.append(" [accuracy \"" + std::to_string(rm->m_accuracy) + "\"]");
4040

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

4545
msg.append(" [hostname \"" + *rm->m_serverIpAddress.get() \

src/utils/string.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,14 @@ std::string string_to_hex(const std::string& input) {
135135
return output;
136136
}
137137

138-
139-
std::string toHexIfNeeded(const std::string &str) {
138+
std::string toHexIfNeeded(const std::string &str, bool escape_spec) {
139+
// escape_spec: escape special chars or not
140+
// spec chars: '"' (quotation mark, ascii 34), '\' (backslash, ascii 92)
140141
std::stringstream res;
141142

142143
for (int i = 0; i < str.size(); i++) {
143144
int c = (unsigned char)str.at(i);
144-
if (c < 32 || c > 126) {
145+
if (c < 32 || c > 126 || (escape_spec == true && (c == 34 || c == 92))) {
145146
res << "\\x" << std::setw(2) << std::setfill('0') << std::hex << c;
146147
} else {
147148
res << str.at(i);
@@ -267,7 +268,6 @@ void replaceAll(std::string *str, const std::string& from,
267268
}
268269
}
269270

270-
271271
} // namespace string
272272
} // namespace utils
273273
} // namespace modsecurity

src/utils/string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ std::string dash_if_empty(const std::string *str);
6161
std::string limitTo(int amount, const std::string &str);
6262
std::string removeBracketsIfNeeded(std::string a);
6363
std::string string_to_hex(const std::string& input);
64-
std::string toHexIfNeeded(const std::string &str);
64+
std::string toHexIfNeeded(const std::string &str, bool escape_spec = false);
6565
std::string tolower(std::string str);
6666
std::string toupper(std::string str);
6767
std::vector<std::string> ssplit(std::string str, char delimiter);

0 commit comments

Comments
 (0)