• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011, Mike Samuel
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 //
8 // Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // Neither the name of the OWASP nor the names of its contributors may
14 // be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 // COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 
29 package org.owasp.html;
30 
31 import com.google.common.collect.ImmutableMap;
32 
33 /**
34  * From section 8.1.2.6 of http://www.whatwg.org/specs/web-apps/current-work/
35  * <p>
36  * The text in CDATA and RCDATA elements must not contain any
37  * occurrences of the string "</" (U+003C LESS-THAN SIGN, U+002F
38  * SOLIDUS) followed by characters that case-insensitively match the
39  * tag name of the element followed by one of U+0009 CHARACTER
40  * TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
41  * FORM FEED (FF), U+0020 SPACE, U+003E GREATER-THAN SIGN (>), or
42  * U+002F SOLIDUS (/), unless that string is part of an escaping
43  * text span.
44  * </p>
45  *
46  * <p>
47  * See also
48  * http://www.whatwg.org/specs/web-apps/current-work/#cdata-rcdata-restrictions
49  * for the elements which fall in each category.
50  * </p>
51  *
52  * @author Mike Samuel <mikesamuel@gmail.com>
53  */
54 public enum HtmlTextEscapingMode {
55   /**
56    * Normally escaped character data that breaks around comments and tags.
57    */
58   PCDATA,
59   /**
60    * A span of text where HTML special characters are interpreted literally,
61    * as in a SCRIPT tag.
62    */
63   CDATA,
64   /**
65    * Like {@link #CDATA} but only for certain browsers.
66    */
67   CDATA_SOMETIMES,
68   /**
69    * A span of text and character entity references where HTML special
70    * characters are interpreted literally, as in a TITLE tag.
71    */
72   RCDATA,
73   /**
74    * A span of text where HTML special characters are interpreted literally,
75    * where there is no end tag.  PLAIN_TEXT runs until the end of the file.
76    */
77   PLAIN_TEXT,
78 
79   /**
80    * Cannot contain data.
81    */
82   VOID,
83   ;
84 
85   private static final ImmutableMap<String, HtmlTextEscapingMode> ESCAPING_MODES
86       = ImmutableMap.<String, HtmlTextEscapingMode>builder()
87       .put("iframe", CDATA)
88       // HTML5 does not treat listing as CDATA and treats XMP as deprecated,
89       // but HTML2 does at
90       // http://www.w3.org/MarkUp/1995-archive/NonStandard.html
91       // Listing is not supported by browsers.
92       .put("listing", CDATA_SOMETIMES)
93       .put("xmp", CDATA)
94 
95       // Technically, noembed, noscript and noframes are CDATA_SOMETIMES but
96       // we can only be hurt by allowing tag content that looks like text so
97       // we treat them as regular..
98       //.put("noembed", CDATA_SOMETIMES)
99       //.put("noframes", CDATA_SOMETIMES)
100       //.put("noscript", CDATA_SOMETIMES)
101       .put("comment", CDATA_SOMETIMES)  // IE only
102 
103       // Runs till end of file.
104       .put("plaintext", PLAIN_TEXT)
105 
106       .put("script", CDATA)
107       .put("style", CDATA)
108 
109       // Textarea and Title are RCDATA, not CDATA, so decode entity references.
110       .put("textarea", RCDATA)
111       .put("title", RCDATA)
112 
113       // Nodes that can't contain content.
114       // http://www.w3.org/TR/html-markup/syntax.html#void-elements
115       .put("area", VOID)
116       .put("base", VOID)
117       .put("br", VOID)
118       .put("col", VOID)
119       .put("command", VOID)
120       .put("embed", VOID)
121       .put("hr", VOID)
122       .put("img", VOID)
123       .put("input", VOID)
124       .put("keygen", VOID)
125       .put("link", VOID)
126       .put("meta", VOID)
127       .put("param", VOID)
128       .put("source", VOID)
129       .put("track", VOID)
130       .put("wbr", VOID)
131 
132        // EMPTY per http://www.w3.org/TR/REC-html32#basefont
133       .put("basefont", VOID)
134       .build();
135 
136 
137   /**
138    * The mode used for content following a start tag with the given name.
139    */
getModeForTag(String canonTagName)140   public static HtmlTextEscapingMode getModeForTag(String canonTagName) {
141     HtmlTextEscapingMode mode = ESCAPING_MODES.get(canonTagName);
142     return mode != null ? mode : PCDATA;
143   }
144 
145   /**
146    * True iff the content following the given tag allows escaping text
147    * spans: {@code <!--&hellip;-->} that escape even things that might
148    * be an end tag for the corresponding open tag.
149    */
allowsEscapingTextSpan(String canonTagName)150   public static boolean allowsEscapingTextSpan(String canonTagName) {
151     // <xmp> and <plaintext> do not admit escaping text spans.
152     return "style".equals(canonTagName) || "script".equals(canonTagName)
153         || "noembed".equals(canonTagName) || "noscript".equals(canonTagName)
154         || "noframes".equals(canonTagName);
155   }
156 
157   /**
158    * True if content immediately following the start tag must be treated as
159    * special CDATA so that &lt;'s are not treated as starting tags, comments
160    * or directives.
161    */
isTagFollowedByLiteralContent(String canonTagName)162   public static boolean isTagFollowedByLiteralContent(String canonTagName) {
163     HtmlTextEscapingMode mode = getModeForTag(canonTagName);
164     return mode != PCDATA && mode != VOID;
165   }
166 
167   /**
168    * True iff the tag cannot contain any content -- will an HTML parser consider
169    * the element to have ended immediately after the start tag.
170    */
isVoidElement(String canonTagName)171   public static boolean isVoidElement(String canonTagName) {
172     return getModeForTag(canonTagName) == VOID;
173   }
174 }
175