File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Expand file tree Collapse file tree 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 26
26
27
27
#include < boost/test/unit_test.hpp>
28
28
29
+ /* defined in logging.cpp */
30
+ namespace BCLog {
31
+ std::string LogEscapeMessage (const std::string& str);
32
+ }
33
+
29
34
BOOST_FIXTURE_TEST_SUITE (util_tests, BasicTestingSetup)
30
35
31
36
BOOST_AUTO_TEST_CASE(util_criticalsection)
@@ -1696,4 +1701,17 @@ BOOST_AUTO_TEST_CASE(test_spanparsing)
1696
1701
BOOST_CHECK_EQUAL (SpanToStr (results[3 ]), " " );
1697
1702
}
1698
1703
1704
+ BOOST_AUTO_TEST_CASE (test_LogEscapeMessage)
1705
+ {
1706
+ // ASCII and UTF-8 must pass through unaltered.
1707
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" Valid log message貓" ), " Valid log message貓" );
1708
+ // Newlines must pass through unaltered.
1709
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" Message\n with newlines\n " ), " Message\n with newlines\n " );
1710
+ // Other control characters are escaped in C syntax.
1711
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (" \x01\x7f Corrupted log message\x0d " ), R"( \x01\x7f Corrupted log message\x0d)" );
1712
+ // Embedded NULL characters are escaped too.
1713
+ const std::string NUL (" O\x00 O" , 3 );
1714
+ BOOST_CHECK_EQUAL (BCLog::LogEscapeMessage (NUL), R"( O\x00O)" );
1715
+ }
1716
+
1699
1717
BOOST_AUTO_TEST_SUITE_END ()
You can’t perform that action at this time.
0 commit comments