Commit dc0fe7a 1 parent ba46f39 commit dc0fe7a Copy full SHA for dc0fe7a
File tree 2 files changed +41
-1
lines changed
2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -224,10 +224,32 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
224
224
return strStamped;
225
225
}
226
226
227
+ namespace BCLog {
228
+ /* * Belts and suspenders: make sure outgoing log messages don't contain
229
+ * potentially suspicious characters, such as terminal control codes.
230
+ *
231
+ * This escapes control characters except newline ('\n') in C syntax.
232
+ * It escapes instead of removes them to still allow for troubleshooting
233
+ * issues where they accidentally end up in strings.
234
+ */
235
+ std::string LogEscapeMessage (const std::string& str) {
236
+ std::string ret;
237
+ for (char ch_in : str) {
238
+ uint8_t ch = (uint8_t )ch_in;
239
+ if ((ch >= 32 || ch == ' \n ' ) && ch != ' \x7f ' ) {
240
+ ret += ch_in;
241
+ } else {
242
+ ret += strprintf (" \\ x%02x" , ch);
243
+ }
244
+ }
245
+ return ret;
246
+ }
247
+ }
248
+
227
249
void BCLog::Logger::LogPrintStr (const std::string& str)
228
250
{
229
251
std::lock_guard<std::mutex> scoped_lock (m_cs);
230
- std::string str_prefixed = str;
252
+ std::string str_prefixed = LogEscapeMessage ( str) ;
231
253
232
254
if (m_log_threadnames && m_started_new_line) {
233
255
str_prefixed.insert (0 , " [" + util::ThreadGetInternalName () + " ] " );
Original file line number Diff line number Diff line change 25
25
26
26
#include < boost/test/unit_test.hpp>
27
27
28
+ /* defined in logging.cpp */
29
+ namespace BCLog {
30
+ std::string LogEscapeMessage (const std::string& str);
31
+ }
32
+
28
33
BOOST_FIXTURE_TEST_SUITE (util_tests, BasicTestingSetup)
29
34
30
35
BOOST_AUTO_TEST_CASE(util_criticalsection)
@@ -1572,4 +1577,17 @@ BOOST_AUTO_TEST_CASE(test_Capitalize)
1572
1577
BOOST_CHECK_EQUAL (Capitalize (" \x00\xfe\xff " ), " \x00\xfe\xff " );
1573
1578
}
1574
1579
1580
+ BOOST_AUTO_TEST_CASE (test_LogEscapeMessage)
1581
+ {
1582
+ // ASCII and UTF-8 must pass through unaltered.
1583
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" Valid log message貓" ), " Valid log message貓" );
1584
+ // Newlines must pass through unaltered.
1585
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" Message\n with newlines\n " ), " Message\n with newlines\n " );
1586
+ // Other control characters are escaped in C syntax.
1587
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" \x01\x7f Corrupted log message\x0d " ), R"( \x01\x7f Corrupted log message\x0d)" );
1588
+ // Embedded NULL characters are escaped too.
1589
+ const std::string NUL (" O\x00 O" , 3 );
1590
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (NUL), R"( O\x00O)" );
1591
+ }
1592
+
1575
1593
BOOST_AUTO_TEST_SUITE_END ()
You can’t perform that action at this time.
0 commit comments