* @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. */
| 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. |
no test coverage detected