Search This Blog

Java: remove unsafe html tags


public class HtmlTagRemover {
    public static String removeUnsafeTags(String html) {
        // Define the regular expression pattern to match unsafe tags
        String unsafeTagsPattern = "<(script|iframe|object|embed)[^>]*>.*?</\\1>";

        // Remove the unsafe tags from the HTML string
        String safeHtml = html.replaceAll(unsafeTagsPattern, "");

        return safeHtml;
    }

    public static void main(String[] args) {
        String html = "<b>Safe HTML</b><script>alert('XSS')</script>";
        String safeHtml = removeUnsafeTags(html);
        System.out.println(safeHtml);
    }
}

No comments:

Post a Comment