Difference between revisions of "How to perform HTML entity encoding in Java"

Jump to: navigation, search
m (Why was this article deleted)
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Released 14/1/2008
Deleted 9/16/2010
==Why was this article deleted==
Injection attacks rely on the fact that interpreters take data and execute it as commands. If an attacker can modify the data that's sent to an interpreter, they may be able to make it misbehave. One way to help prevent this from happening is to encode the attacker's data in such a way that the interpreter will not get confused. [http://www.w3.org/TR/html401/sgml/entities.html HTML entity encoding] is just such an encoding mechanism for many interpreters.
HTML Entity Encoding is not enough to stop XSS in web applications. Please see [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet]] for more information.
This is not a guarantee by the way. It's almost certain that someone, probably from the XML/Web Services world, will create an engine that performs HTML entity decoding automatically, thus reintroducing the injection threat.  However, for the time being, HTML entity encoding seems to work pretty well to prevent many types of injection.
We're going to implement a simple little method that encodes special characters. The nice .NET folks over at Microsoft had the foresight to build this into their platform, but the Java community seems to resist adding validation to the Java EE environment despite all the security issues that it could solve.  View layers such as Java Server Faces, Spring-MVC, WebWork and others automatically perform HTML encoding through custom tags that is often incomplete.
For example, Spring provides both HTML and JavaScript encoding functionality (spring:message htmlEscape and htmlEscape) that can be set at the form element level. [http://static.springframework.org/spring/docs/1.1.5/taglib/tag/MessageTag.html]
HTML escape functionality in Spring can also be set at the page or servlet container. [http://static.springframework.org/spring/docs/1.1.5/taglib/tag/HtmlEscapeTag.html] Note that's Spring's default entity encoder only encodes the "big 5" and does not handle double-encoding. This code that handles this functionality was last updated in 2003.[http://springframework.cvs.sourceforge.net/springframework/i21/src/com/interface21/web/util/HtmlUtils.java?revision=1.2&view=markup]
  21          {"#39", new Integer(39)}, // ' - apostrophe
  22          {"quot", new Integer(34)}, // " - double-quote
  23          {"amp", new Integer(38)}, // & - ampersand
  24          {"lt", new Integer(60)}, // < - less-than
  25          {"gt", new Integer(62)}, // > - greater-than
Encoding the "big 5" serves exactly the purpose it was designed for: prevents injecting HTML markup with ilegal characters inside tags and attribute values. However it does not prevent more elaborate injections, does not help with "out of range characters = question marks" when outputting Strings to Writers with single byte encodings, nor prevents character reinterpretation when user switches browser encoding over displayed page.--[[User:A.in.the.k|A.in.the.k]] 07:18, 18 March 2009 (UTC)
The best place for a more complete method of HTML entity encoding is in some kind of ValidationEngine, but since it's a good candidate for being static, it doesn't matter what class it ends up in that much.
Note that this implementation doesn't produce the special characters like & lt; or & gt; - but it's not difficult to implement with a simple lookup table.
    /* return StringBuilder and/or make Writer param and write to stream directly*/
    public static String htmlEntityEncode( String s )
        StringBuilder buf = new StringBuilder(s.length());
        for ( int i = 0; i < len; i++ )
            char c = s.charAt( i );
            if ( c>='a' && c<='z' || c>='A' && c<='Z' || c>='0' && c<='9' )
                buf.append( c );
        return buf.toString();
When testing this simple approach on several browsers and comparing with non-escaped version
we can observe several problems, specially on ISOControlCharacter ranges (0000-001F and 0080-009F).
MSIE 6.0 does display &#0;  in escaped form.
All other browsers (tested on Win platform) display escaped range 0080-009F  as incorrect displayable characters,
mapped to local OS charset (windows-1250 in the test case).
This leads to confusion, and self question: "how and why control characters should be outputted in html."
I recommend removing all nonWhitespace ISOControlCharacters from the outputted stream.--[[User:A.in.the.k|A.in.the.k]] 07:12, 18 March 2009 (UTC)
  public static StringBuilder escapeHtmlFull(String s)
      StringBuilder b = new StringBuilder(s.length());
      for (int i = 0; i < s.length(); i++)
        char ch = s.charAt(i);
        if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
          // safe
        else if (Character.isWhitespace(ch))
          // paranoid version: whitespaces are unsafe - escape
          // conversion of (int)ch is naive
          b.append("&#").append((int) ch).append(";");
        else if (Character.isISOControl(ch))
          // paranoid version:isISOControl which are not isWhitespace removed !
          // do nothing do not include in output !
          // paranoid version
          // the rest is unsafe, including <127 control chars
          b.append("&#" + (int) ch + ";");
      return b;
* The Jakara Commons Lang package has a generic class for performing a wide range of [http://commons.apache.org/lang/api-release/org/apache/commons/lang/StringEscapeUtils.html String escaping functions].
* jTidy includes an [http://jtidy.sourceforge.net/multiproject/jtidyservlet/apidocs/org/w3c/tidy/servlet/util/HTMLEncode.html HTMLEncode class] for performing HTML encoding.
[[Category:How To]]
[[Category:OWASP Java Project]]
[[Category:OWASP Validation Project]]

Latest revision as of 00:38, 17 September 2010


Deleted 9/16/2010

Why was this article deleted

HTML Entity Encoding is not enough to stop XSS in web applications. Please see XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet for more information.