CSRF, XSS and SQL Injection attack prevention in JSF

前端 未结 3 1344
天涯浪人
天涯浪人 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

    XSS

    JSF is designed to have builtin XSS prevention. You can safely redisplay all user-controlled input (request headers (including cookies!), request parameters (also the ones which are saved in DB!) and request bodies (uploaded text files, etc)) using any JSF component.

    
    
    
    etc...
    

    Note that when you're using JSF 2.0 on Facelets, then you can use EL in template text like so:

    Welcome, #{user.name}

    This will also implicitly be escaped. You don't necessarily need here.

    Only when you're explicitly unescaping user-controlled input using escape="false":

    
    

    then you've a potential XSS attack hole!

    If you'd like to redisplay user-controlled input as HTML wherein you would like to allow only a specific subset of HTML tags like , , , etc, then you need to sanitize the input by a whitelist. The HTML parser Jsoup is very helpful in this.

    itemLabelEscaped bug in Mojarra < 2.2.6

    Older Mojarra versions before 2.2.6 had the bug wherein incorrectly renders the label unescaped when provided a List via instead of List or SelectItem[] as value (issue 3143). In other words, if you're redisplaying user-controlled data as item labels via a List, then you've a potential XSS hole. If upgrading to at least Mojarra 2.2.6 is not an option, then you need to explicitly set itemLabelEscaped attribute to true to prevent that.

    
    

    CSRF

    JSF 2.x has already builtin CSRF prevention in flavor of javax.faces.ViewState hidden field in the form when using server side state saving. In JSF 1.x this value was namely pretty weak and too easy predictable (it was actually never intended as CSRF prevention). In JSF 2.0 this has been improved by using a long and strong autogenerated value instead of a rather predictable sequence value and thus making it a robust CSRF prevention.

    In JSF 2.2 this is even be further improved by making it a required part of the JSF specification, along with a configurable AES key to encrypt the client side state, in case client side state saving is enabled. See also JSF spec issue 869 and Reusing ViewState value in other session (CSRF). New in JSF 2.2 is CSRF protection on GET requests by .

    Only when you're using stateless views as in , or there's somewhere a XSS attack hole in the application, then you've a potential CSRF attack hole.


    SQL injection

    This is not JSF's responsibility. How to prevent this depends on the persistence API you're using (raw JDBC, modern JPA or good ol' Hibernate), but all boils down that you should never concatenate user-controlled input into SQL strings like so

    String sql = "SELECT * FROM user WHERE username = '" + username + "' AND password = md5(" + password + ")";
    String jpql = "SELECT u FROM User u WHERE u.username = '" + username + "' AND u.password = md5('" + password + "')";
    

    Imagine what would happen if the enduser chooses the following name:

    x'; DROP TABLE user; --
    

    You should always use parameterized queries where applicable.

    String sql = "SELECT * FROM user WHERE username = ? AND password = md5(?)";
    String jpql = "SELECT u FROM User u WHERE u.username = ?1 AND u.password = md5(?2)";
    

    In plain JDBC you need to use PreparedStatement to fill the parameter values and in JPA (and Hibernate), the Query object offers setters for this as well.

提交回复
热议问题