Class DebugTextSanitizer

java.lang.Object
org.apache.juneau.http.DebugTextSanitizer

public final class DebugTextSanitizer extends Object
Neutralizes log-forging (log-injection) in client-controlled strings before they are written to a log line.

A remote client can place CR/LF (or Unicode line/paragraph separators, bidi controls, or other control characters) into a request URI, header name/value, or body, and — if that text is interpolated verbatim into a log record — forge additional physical log lines (e.g. a fake status banner) or corrupt a downstream log parser/SIEM. This utility replaces every such character with a visible, inert escape (\r, \n, or \\uXXXX) so the value's information is preserved (a reader can still see there was a newline) but it can no longer terminate the current line or start a new one.

The transform:

  • CR (\\u000D) → \r, LF (\\u000A) → \n.
  • All other C0 controls (\\u0000–\\u001F) except TAB (\\u0009) → \\uXXXX.
  • DEL and C1 controls (\\u007F–\\u009F, which includes NEL \\u0085) → \\uXXXX.
  • Line separator (\\u2028), paragraph separator (\\u2029), and bidi controls (\\u202A–\\u202E, \\u2066–\\u2069) → \\uXXXX.
  • TAB (\\u0009) is preserved — it cannot forge a line.

When a length cap is supplied, truncation is applied after escaping, counts sanitized (character) output, appends a …[truncated] marker, and never cuts an escape sequence or a surrogate pair in half. A fast path returns the original reference unchanged when the string contains no character requiring an escape and is within the cap, so well-behaved clients incur no allocation.

All methods are thread-safe (the utility is stateless).

See Also:
Since:
10.0.0
  • Field Details

  • Method Details

    • sanitize

      public static String sanitize(String value)
      Escapes every log-forging control character in the given string, with no length cap.
      Parameters:
      value - The string to sanitize. Can be null (returns null).
      Returns:
      The sanitized string, or the original reference if nothing needed escaping. null if the input was null.
    • sanitize

      public static String sanitize(String value, int maxLen)
      Escapes every log-forging control character in the given string, then caps the sanitized output to maxLen characters.

      The cap counts the escaped (rendered) length, appends …[truncated] when it clips, and never splits an escape sequence or a surrogate pair.

      Parameters:
      value - The string to sanitize. Can be null (returns null).
      maxLen - The maximum number of sanitized characters to keep (excluding the truncation marker).
      Returns:
      The sanitized (and possibly truncated) string, or the original reference if nothing needed escaping and it was within the cap. null if the input was null.