Class InputSanitizer
InputSanitizer provides methods for escaping and cleaning user-supplied strings before they are used in HTML output, JavaScript contexts, URL parameters, or SQL queries. These methods help prevent XSS, script injection, and other common web application vulnerabilities.
Usage:
// Escape HTML special characters
String safe = InputSanitizer.escapeHtml("<script>alert('xss')</script>");
// Result: "<script>alert('xss')</script>"
// Strip all HTML tags
String text = InputSanitizer.stripHtml("<b>Hello</b> <script>evil()</script> World");
// Result: "Hello World"
// Escape for JavaScript string context
String jsStr = InputSanitizer.escapeJavaScript("alert('xss')");
// Result: "alert(\\u0027xss\\u0027)"
- Since:
- 2.1
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringencodeUrlParam(String input) Encodes a string for safe use as a URL parameter value.static StringescapeHtml(String input) Escapes HTML special characters to prevent XSS when inserting untrusted data into HTML element content.static StringescapeJavaScript(String input) Escapes a string for safe inclusion within a JavaScript string literal.static StringremoveControlChars(String input) Removes null bytes and other control characters from user input.static StringStrips all HTML tags from the input string, leaving only text content.
-
Method Details
-
escapeHtml
Escapes HTML special characters to prevent XSS when inserting untrusted data into HTML element content.Replaces:
& < > " 'with their HTML entity equivalents.- Parameters:
input- the untrusted input string- Returns:
- the HTML-escaped string, or
nullif the input is null
-
stripHtml
Strips all HTML tags from the input string, leaving only text content.This is useful for extracting plain text from HTML-formatted user input. Note that this method does not decode HTML entities.
- Parameters:
input- the input string potentially containing HTML- Returns:
- the text with all HTML tags removed, or
nullif the input is null
-
escapeJavaScript
Escapes a string for safe inclusion within a JavaScript string literal.Escapes single quotes, double quotes, backslashes, forward slashes, and control characters using Unicode escape sequences where needed.
- Parameters:
input- the untrusted input string- Returns:
- the JavaScript-escaped string, or
nullif the input is null
-
encodeUrlParam
Encodes a string for safe use as a URL parameter value.Uses percent-encoding (RFC 3986) for all characters that are not unreserved (letters, digits,
-,_,.,~).- Parameters:
input- the untrusted input string- Returns:
- the URL-encoded string, or
nullif the input is null
-
removeControlChars
Removes null bytes and other control characters from user input.Null byte injection can be used to truncate strings in some contexts. This method removes all control characters (U+0000 through U+001F and U+007F) except for tab (U+0009), newline (U+000A), and carriage return (U+000D).
- Parameters:
input- the input string- Returns:
- the cleaned string, or
nullif the input is null
-