import java.util.regex.Pattern;
public class InputSanitizer {
// Method to sanitize a string input
public static String sanitizeInput(String input) {
if (input == null || input.isEmpty()) {
return ""; // Return empty string for null or empty input
}
// Remove HTML tags
input = stripHtmlTags(input);
// Remove special characters (keep alphanumeric and spaces)
input = allowAlphanumericAndSpaces(input);
// Optional: Limit length
// input = limitLength(input, 255); // Example: Limit to 255 characters
return input;
}
// Removes HTML tags
private static String stripHtmlTags(String input) {
Pattern pattern = Pattern.compile("<[^>]*>");
return pattern.matcher(input).replaceAll("");
}
// Allows only alphanumeric characters and spaces
private static String allowAlphanumericAndSpaces(String input) {
return input.replaceAll("[^a-zA-Z0-9\\s]", "");
}
// Optional: Limits the length of the string. Can be computationally expensive for large strings.
private static String limitLength(String input, int maxLength) {
if (input.length() > maxLength) {
return input.substring(0, maxLength);
}
return input;
}
public static void main(String[] args) {
String testInput = "<script>alert('XSS')</script> Hello, World! & Special chars: !@#$%^&*()_+=-`~[]\{}|;':\",./<>?";
String sanitizedInput = sanitizeInput(testInput);
System.out.println("Original Input: " + testInput);
System.out.println("Sanitized Input: " + sanitizedInput);
}
}
Add your comment