Skip to content

Commit 3b7ca3e

Browse files
committed
Escape log field 'data' value
1 parent 5dfc0a2 commit 3b7ca3e

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/rule_message.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ std::string RuleMessage::_details(const RuleMessage *rm) {
3131
msg.append(" [id \"" + std::to_string(rm->m_ruleId) + "\"]");
3232
msg.append(" [rev \"" + rm->m_rev + "\"]");
3333
msg.append(" [msg \"" + rm->m_message + "\"]");
34-
msg.append(" [data \"" + utils::string::limitTo(200, rm->m_data) + "\"]");
34+
msg.append(" [data \"" + utils::string::log_escape_hex(utils::string::limitTo(200, rm->m_data)) + "\"]");
3535
msg.append(" [severity \"" +
3636
std::to_string(rm->m_severity) + "\"]");
3737
msg.append(" [ver \"" + rm->m_ver + "\"]");

src/utils/string.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,28 @@ void replaceAll(std::string *str, const std::string& from,
267267
}
268268
}
269269

270+
std::string log_escape_hex(std::string s) {
271+
272+
std::string ret = "";
273+
char tchar[2];
274+
275+
for (std::string::size_type i = 0; i < s.size(); i++) {
276+
if ( (s[i] == '"')
277+
||(s[i] == '\\')
278+
||(s[i] <= 0x1f)
279+
||(s[i] >= 0x7f))
280+
{
281+
ret.append("\\x");
282+
c2x(s[i], (unsigned char*)tchar);
283+
ret.push_back(tchar[0]);
284+
ret.push_back(tchar[1]);
285+
}
286+
else {
287+
ret.push_back(s[i]);
288+
}
289+
}
290+
return ret;
291+
}
270292

271293
} // namespace string
272294
} // namespace utils

src/utils/string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void replaceAll(std::string *str, const std::string& from,
7272
const std::string& to);
7373
std::string removeWhiteSpacesIfNeeded(std::string a);
7474
std::string parserSanitizer(std::string a);
75+
std::string log_escape_hex(std::string s);
7576

7677
unsigned char x2c(unsigned char *what);
7778
unsigned char xsingle2c(unsigned char *what);

0 commit comments

Comments
 (0)