|
28 | 28 | import java.util.logging.Formatter; |
29 | 29 | import java.util.logging.Handler; |
30 | 30 | import java.util.function.Supplier; |
| 31 | +import java.util.regex.Pattern; |
31 | 32 |
|
32 | 33 | /** |
33 | 34 | * Central location for all debugging messages |
@@ -106,6 +107,31 @@ public String toString() { |
106 | 107 | /** Info level debug message */ |
107 | 108 | public static final String INFO = "INFO"; |
108 | 109 |
|
| 110 | + /** Characters removed from externally-influenced log values: CR, LF, |
| 111 | + * other C0 control characters, DEL, and double quote. */ |
| 112 | + private static final Pattern LOG_UNSAFE_CHARS = |
| 113 | + Pattern.compile("[\\x00-\\x1F\\x7F\"]"); |
| 114 | + |
| 115 | + /** |
| 116 | + * Sanitize input string: replaces CR, LF, other C0 control characters, |
| 117 | + * DEL, and double quote with underscore. |
| 118 | + * |
| 119 | + * Used by callers to sanitize values that come from network or remote |
| 120 | + * peers before placing them in a log message, so uncontrolled input |
| 121 | + * cannot forge or split log entries. |
| 122 | + * |
| 123 | + * @param value string to sanitize, may be null |
| 124 | + * @return sanitized string, or null if value was null |
| 125 | + */ |
| 126 | + public static String sanitizeForLog(String value) { |
| 127 | + |
| 128 | + if (value == null) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + return LOG_UNSAFE_CHARS.matcher(value).replaceAll("_"); |
| 133 | + } |
| 134 | + |
109 | 135 | /** |
110 | 136 | * Native wolfSSL logging callback. |
111 | 137 | * Used to print native wolfSSL debug logs when 'wolfssl.debug' System |
@@ -230,6 +256,47 @@ public String format(LogRecord record) { |
230 | 256 | } |
231 | 257 | } |
232 | 258 |
|
| 259 | + /** |
| 260 | + * Escape a string for safe inclusion as a JSON string value. Handles |
| 261 | + * backslash, double quote, control characters. |
| 262 | + * |
| 263 | + * @param value string to escape, may be null |
| 264 | + * @return JSON-escaped string, empty string if value was null |
| 265 | + */ |
| 266 | + private static String jsonEscape(String value) { |
| 267 | + |
| 268 | + StringBuilder sb; |
| 269 | + |
| 270 | + if (value == null) { |
| 271 | + return ""; |
| 272 | + } |
| 273 | + |
| 274 | + sb = new StringBuilder(value.length()); |
| 275 | + for (int i = 0; i < value.length(); i++) { |
| 276 | + char c = value.charAt(i); |
| 277 | + switch (c) { |
| 278 | + case '\\': sb.append("\\\\"); break; |
| 279 | + case '"': sb.append("\\\""); break; |
| 280 | + case '\n': sb.append("\\n"); break; |
| 281 | + case '\r': sb.append("\\r"); break; |
| 282 | + case '\t': sb.append("\\t"); break; |
| 283 | + case '\b': sb.append("\\b"); break; |
| 284 | + case '\f': sb.append("\\f"); break; |
| 285 | + default: |
| 286 | + /* other control chars have no shorthand, emit as a |
| 287 | + * unicode escape */ |
| 288 | + if (c < 0x20) { |
| 289 | + sb.append(String.format("\\u%04x", (int) c)); |
| 290 | + } |
| 291 | + else { |
| 292 | + sb.append(c); |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + return sb.toString(); |
| 298 | + } |
| 299 | + |
233 | 300 | /** |
234 | 301 | * JSON formatter for wolfSSL logs |
235 | 302 | */ |
@@ -270,10 +337,10 @@ public String format(LogRecord record) { |
270 | 337 | " \"thread_id\": \"%d\"\n" + |
271 | 338 | "}\n", |
272 | 339 | TIME_FORMATTER.format(Instant.ofEpochMilli(record.getMillis())), |
273 | | - levelStr, |
274 | | - component, |
275 | | - message, |
276 | | - threadName, |
| 340 | + jsonEscape(levelStr), |
| 341 | + jsonEscape(component), |
| 342 | + jsonEscape(message), |
| 343 | + jsonEscape(threadName), |
277 | 344 | record.getThreadID()); |
278 | 345 | } |
279 | 346 | } |
|
0 commit comments