CSRF, XSS and SQL Injection attack prevention in JSF

前端 未结 3 1349
天涯浪人
天涯浪人 2020-11-22 01:44

I have a web application built on JSF with MySQL as DB. I have already implemented the code to prevent CSRF in my application.

Now since my underlying framework is J

3条回答
  •  花落未央
    2020-11-22 02:31

    When using with unescaped values (for example coming from html text editors) you're open for a nasty XSS attacks. In such cases I'm using a JSF converter which uses Jsoup to remove javascript from text leaving HTML intact. Converter can be used to sanitize user inputs as well. You can use it like this:

    And the converter itself:

    /**
     * Prevents from XSS attack if output text is not escaped.
     */
    @FacesConverter("htmlSanitizingConverter")
    public class HtmlSanitizingConverter implements Converter {
    
        private static final Whitelist JSOUP_WHITELIST = Whitelist.relaxed()
                .preserveRelativeLinks(true)
                .addAttributes(":all","style");
                /*
                 Optionally - add support for hyperlinks and base64 encoded images.
                .addTags("img")
                .addAttributes("img", "height", "src", "width")
                .addAttributes("a", "href")
                .addProtocols("img", "src", "http", "https", "data");
                */
    
        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
            return (submittedValue != null) ? Jsoup.clean(submittedValue, JSOUP_WHITELIST) : null;
        }
    
        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            return (value != null) ? Jsoup.clean(value.toString(), JSOUP_WHITELIST) : "";
        }
    
    }
    

    Note: When you're using JSF with PrimeFaces, beware of - older versions (prior to 6.2) by default didn't sanitize user input.

提交回复
热议问题