All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.webbitserver.helpers.XssCharacterEscaper Maven / Gradle / Ivy

package org.webbitserver.helpers;

public class XssCharacterEscaper {
    /**
     * Replaces characters in input which may open up cross-site scripting (XSS) attacks with XSS-safe equivalents.
     *
     * Follows escaping rules from
     * the OWASP.
     *
     * @param input String to sanitize.
     * @return XSS-safe version of input.
     */
    public static String escape(String input) {
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); ++i) {
            char original = input.charAt(i);
            switch (original) {
                case '&':
                    builder.append("&");
                    break;
                case '<':
                    builder.append("<");
                    break;
                case '>':
                    builder.append(">");
                    break;
                case '"':
                    builder.append(""");
                    break;
                case '\'':
                    builder.append("'");
                    break;
                case '/':
                    builder.append("/");
                    break;
                default:
                    builder.append(original);
                    break;
            }
        }
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy