MCPcopy Create free account
hub / github.com/MITK/MITK / SanitizeForLog

Function SanitizeForLog

Modules/RESTAPI/src/mitkRestServer.cpp:52–73  ·  view source on GitHub ↗

* @brief Sanitize a string for safe inclusion in log messages. * * Replaces control characters (newlines, tabs, etc.) with their escaped * representations to prevent log injection attacks. * * @param input The raw string from external input. * @return Sanitized string safe for logging. */

Source from the content-addressed store, hash-verified

50 * @return Sanitized string safe for logging.
51 */
52 std::string SanitizeForLog(const std::string& input)
53 {
54 std::string result;
55 result.reserve(input.size());
56
57 for (const unsigned char c : input)
58 {
59 if (c == '\n')
60 result += "\\n";
61 else if (c == '\r')
62 result += "\\r";
63 else if (c == '\t')
64 result += "\\t";
65 else if (c < 0x20)
66 result += "\\x" + std::string(1, "0123456789abcdef"[(c >> 4) & 0xF]) +
67 std::string(1, "0123456789abcdef"[c & 0xF]);
68 else
69 result += static_cast<char>(c);
70 }
71
72 return result;
73 }
74
75 /**
76 * @brief Check if an IP address is a localhost address.

Callers 5

StartMethod · 0.85
RecordRequestMethod · 0.85
CheckClientAccessMethod · 0.85
CheckAuthenticationMethod · 0.85
ServerThreadFuncMethod · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected