59

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 JSF, I guess I don't have to handle XSS attack as it is already handled by UIComponent. I am not using any JavaScript in any of the view pages. Even if I use do I really need to implement code to prevent XSS attacks?

For DB we are using prepared statements and stored procedures in all DB interactions.

Is there anything else needs to be handled for preventing these 3 common attacks? I have already been through the OWASP site and their cheat sheets.

Do I need to take care of any other potential attack vectors?

0

3 Answers 3

122

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.

<h:outputText value="#{user.name}" />
<h:outputText value="#{user.name}" escape="true" />
<h:inputText value="#{user.name}" />
etc...

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

<p>Welcome, #{user.name}</p>

This will also implicitly be escaped. You don't necessarily need <h:outputText> here.

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

<h:outputText value="#{user.name}" 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 <b>, <i>, <u>, 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 <f:selectItems itemLabel> incorrectly renders the label unescaped when provided a List<T> via <f:selectItems var> instead of List<SelectItem> or SelectItem[] as value (issue 3143). In other words, if you're redisplaying user-controlled data as item labels via a List<T>, 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.

<f:selectItems value="#{bean.entities}" var="entity" itemValue="#{entity}"
    itemLabel="#{entity.someUserControlledProperty}" itemLabelEscaped="true" />

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 <protected-views>.

Only when you're using stateless views as in <f:view transient="true">, 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.

16
  • 5
    I'd add that contrary to popular believe, using stored procedures does not automatically save you from SQL injection attacks: I've seen stored procedures create SQL statements by concatenation and that is equally vulnerable to SQL injection! It's the same way that using PreparedStatements doesn't automatically save your from SQL injection attacks, if you're using them wrong! Oct 11, 2011 at 12:17
  • 1
    @BalusC Well I cannot thank you enough for this awesome explanation..But I have some doubts. In my GUI, I use <h:outputText value="#{user.name}" escape="false" /> for displaying on the GUI..Now this is a potential XSS Attack.How do I avoid it.??. Secondly, I am stll using JSF 1.2 and so how do I handle CSRF then?.I used WebScrarab tool to intercept Http Request and modify the value and it indeed was successful.Does it mean that the application is prone to Attack.How can I avoid it. I have already handled SQL injection. Only thing to worry for me is XSS and CSRF as of now..Please help. Oct 12, 2011 at 10:32
  • @BalusC Also the explanation provided by Samuel suggest me to have a look at closure Template for sanitizing HTML input. But I have heard that closure template has serious performance issue and morever it is from Google Labs so it will be almost in Beta version hence not stable...Which one would you recommend for HTMl santizing..Jsoup or Closure..I am absolutely new to this santizing and parsing and hence I would prefer something which is easy to learn and implement. Oct 12, 2011 at 10:58
  • 3
    As to XSS, just remove escape="false". Or if you want to allow certain HTML, then use Jsoup. You just need to do String safe = Jsoup.clean(unsafe, Whitelist.basic());. See also this guide. You can do this directly before saving input in DB. As to CSRF prevention in JSF 1.x, you should maintain a session based anti-CSRF token in the form (basically, a hidden input field with a long/autogenerated/unpredictable value). Seam framework has similar component <s:token>: seamframework.org/Documentation/CrossSiteRequestForgery
    – BalusC
    Oct 12, 2011 at 12:40
  • @BalusC if you will have a look on the OWASP Dokument about ORM Injection you could read the current Oracle JDBC driver escapes input for prepared statements and parameterized stored procedures. so this means, your are safe from Injections, right?
    – Joergi
    Mar 9, 2012 at 11:16
8

I am not using any JavaScript in any of the view pages. Even if I use do I really need to implement code to bypass XSS Attack.

You can be vulnerable to XSS even if you don't use JavaScript in your pages. XSS occurs when you incorporate content controlled by an attacker without properly encoding it.

Anytime you do something like

response.write("<b>" + x + "</b>")

where an attacker can cause x to contain HTML that contains JavaScript, then you are vulnerable to XSS.

The solution is usually not to write large amounts of code. Typically the solution is to encode $x and any other values controlled by an attacker before including them in the HTML you generate.

response.write("<b>" + escapePlainTextToHtml(x) + "</b>")

Filtering or sanitizing inputs can help provide an additional layer of protection.

<shameless-plug>

You can also use a template language that encodes output automatically to protect against XSS.

Closure Template is one such option for Java.

Contextual autoescaping works by augmenting Closure Templates to properly encode each dynamic value based on the context in which it appears, thus defending against XSS vulnerabilities in values that are controlled by an attacker.

EDIT

Since you are using JSF you should read up on XSS mitigation in JSF:

Escape output text

<h:outputText/> and <h:outputLabel/> by default has the escape attribute set to True. By using this tag to display outputs, you are able to mitigate majority of the XSS vulnerability.

SeamTextParser and <s:formattedText/>

If you would like to allow users to utilise some of the basic html tags to customise their inputs, JBoss Seam provides a <s:formattedText/> tag that allows some basic html tags and styles specified by users.

5
  • @ankit, Is there a particular sentence that you find confusing? Oct 11, 2011 at 6:49
  • Problem is I am using JSF and i guess just like other frameworks it automatically generates HTML..So I am still clueless about what your wrote..I also went through the closure template..It is so new to me... Oct 11, 2011 at 7:27
  • 1
    Also to add:- My GUI will not allow the user to enter angle bracket tags<>.It will fail client side validation and the request won't be processed..As I understood IF I ALLOW USER to enter HTML tags in my GUI then I need to use the closure template to ensure which all are valid tags and which all are not valid...Please do correct me if am wrong. Oct 11, 2011 at 7:31
  • @ankit, I edited to add a pointer to some best practices for XSS mitigation in JSF. Oct 11, 2011 at 8:24
  • 3
    Unlike as in old fashioned JSP/Servlet, JSF has not really a concept of response.write(foo). This answer is a bit confusing towards users who are new to JSF 2.x.
    – BalusC
    Oct 11, 2011 at 12:03
1

When using <h:outputText escape="false"> 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:

<h:outputText value="{bean.value}" escape="false" converter="htmlSanitizingConverter"/>

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 <p:textEditor> - older versions (prior to 6.2) by default didn't sanitize user input.

4
  • 1
    1: individual components should not be an answer to a generic question. 2: The issue is fixed in 7.0 in that the component can be configured to do cleaning and in 7.1 it wil even be the default.
    – Kukeltje
    Sep 27, 2019 at 9:10
  • @Kukeltje converter is generic solution for solving this problem regardless of used technology. I'm just pointing this PF issue because it's the most popular JSF components library and older versions of PF are still heavily used (and in most cases won't be updated due to compatibility issues). Sep 27, 2019 at 9:17
  • I'm not saying your answer is not valuable, I think it is, just that it does not directly belong here. You are allowed (and even encouraged) in Stackoverflow to create a question and answer it yourself. E.g. 'How do I prevent XSS in p:textEditor` and answer it yourself with this answer. It is seriously highly appreciated and keeps things clear, separated etc.
    – Kukeltje
    Sep 27, 2019 at 9:42
  • 1
    I understand the converter is generic, but that is also the case when using a plain textarea and a custom js html editor plugin (or markdown or even when plain html is entered in a textarea). So you can make it more generic by focussing on the different ways of inputs too (in there referring to the p:textEditor to). Now the answer looks to focus soley on the p:textEditor while the solution is in the display and not the 'entering of data' (you could use a converter on the input too... Even more clean... No risks if someone forgets to apply it on the output
    – Kukeltje
    Sep 27, 2019 at 9:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.