001    // Copyright (c) 2011, Mike Samuel
002    // All rights reserved.
003    //
004    // Redistribution and use in source and binary forms, with or without
005    // modification, are permitted provided that the following conditions
006    // are met:
007    //
008    // Redistributions of source code must retain the above copyright
009    // notice, this list of conditions and the following disclaimer.
010    // Redistributions in binary form must reproduce the above copyright
011    // notice, this list of conditions and the following disclaimer in the
012    // documentation and/or other materials provided with the distribution.
013    // Neither the name of the OWASP nor the names of its contributors may
014    // be used to endorse or promote products derived from this software
015    // without specific prior written permission.
016    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
018    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
019    // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
020    // COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
021    // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022    // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023    // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
024    // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
025    // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
026    // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
027    // POSSIBILITY OF SUCH DAMAGE.
028    
029    package org.owasp.html;
030    
031    import com.google.common.collect.ImmutableMap;
032    
033    /**
034     * From section 8.1.2.6 of http://www.whatwg.org/specs/web-apps/current-work/
035     * <p>
036     * The text in CDATA and RCDATA elements must not contain any
037     * occurrences of the string "</" (U+003C LESS-THAN SIGN, U+002F
038     * SOLIDUS) followed by characters that case-insensitively match the
039     * tag name of the element followed by one of U+0009 CHARACTER
040     * TABULATION, U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C
041     * FORM FEED (FF), U+0020 SPACE, U+003E GREATER-THAN SIGN (>), or
042     * U+002F SOLIDUS (/), unless that string is part of an escaping
043     * text span.
044     * </p>
045     *
046     * <p>
047     * See also
048     * http://www.whatwg.org/specs/web-apps/current-work/#cdata-rcdata-restrictions
049     * for the elements which fall in each category.
050     * </p>
051     *
052     * @author Mike Samuel <mikesamuel@gmail.com>
053     */
054    public enum HtmlTextEscapingMode {
055      /**
056       * Normally escaped character data that breaks around comments and tags.
057       */
058      PCDATA,
059      /**
060       * A span of text where HTML special characters are interpreted literally,
061       * as in a SCRIPT tag.
062       */
063      CDATA,
064      /**
065       * Like {@link #CDATA} but only for certain browsers.
066       */
067      CDATA_SOMETIMES,
068      /**
069       * A span of text and character entity references where HTML special
070       * characters are interpreted literally, as in a TITLE tag.
071       */
072      RCDATA,
073      /**
074       * A span of text where HTML special characters are interpreted literally,
075       * where there is no end tag.  PLAIN_TEXT runs until the end of the file.
076       */
077      PLAIN_TEXT,
078    
079      /**
080       * Cannot contain data.
081       */
082      VOID,
083      ;
084    
085      private static final ImmutableMap<String, HtmlTextEscapingMode> ESCAPING_MODES
086          = ImmutableMap.<String, HtmlTextEscapingMode>builder()
087          .put("iframe", CDATA)
088          // HTML5 does not treat listing as CDATA and treats XMP as deprecated,
089          // but HTML2 does at
090          // http://www.w3.org/MarkUp/1995-archive/NonStandard.html
091          // Listing is not supported by browsers.
092          .put("listing", CDATA_SOMETIMES)
093          .put("xmp", CDATA)
094    
095          // Technically, noembed, noscript and noframes are CDATA_SOMETIMES but
096          // we can only be hurt by allowing tag content that looks like text so
097          // we treat them as regular..
098          //.put("noembed", CDATA_SOMETIMES)
099          //.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       */
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       */
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       */
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       */
171      public static boolean isVoidElement(String canonTagName) {
172        return getModeForTag(canonTagName) == VOID;
173      }
174    }