Merge "jquery.makeCollapsible: Some low-level tests"
[lhc/web/wiklou.git] / includes / Sanitizer.php
index ed01235..f3a5281 100644 (file)
@@ -56,7 +56,7 @@ class Sanitizer {
         * As well as ' which is only defined starting in XHTML1.
         * @private
         */
-       static $htmlEntities = array(
+       private static $htmlEntities = array(
                'Aacute'   => 193,
                'aacute'   => 225,
                'Acirc'    => 194,
@@ -315,15 +315,16 @@ class Sanitizer {
        /**
         * Character entity aliases accepted by MediaWiki
         */
-       static $htmlEntityAliases = array(
+       private static $htmlEntityAliases = array(
                'רלמ' => 'rlm',
                'رلم' => 'rlm',
        );
 
        /**
         * Lazy-initialised attributes regex, see getAttribsRegex()
+        * @private
         */
-       static $attribsRegex;
+       private static $attribsRegex;
 
        /**
         * Regular expression to match HTML/XML attribute pairs within a tag.
@@ -357,13 +358,16 @@ class Sanitizer {
         * removes HTML comments
         * @private
         * @param $text String
-        * @param $processCallback Callback to do any variable or parameter replacements in HTML attribute values
+        * @param $processCallback Callback to do any variable or parameter
+        *        replacements in HTML attribute values
         * @param array $args for the processing callback
         * @param array $extratags for any extra tags to include
         * @param array $removetags for any tags (default or extra) to exclude
         * @return string
         */
-       static function removeHTMLtags( $text, $processCallback = null, $args = array(), $extratags = array(), $removetags = array() ) {
+       static function removeHTMLtags( $text, $processCallback = null,
+               $args = array(), $extratags = array(), $removetags = array()
+       ) {
                global $wgUseTidy, $wgAllowMicrodataAttributes, $wgAllowImageTag;
 
                static $htmlpairsStatic, $htmlsingle, $htmlsingleonly, $htmlnest, $tabletags,
@@ -397,7 +401,7 @@ class Sanitizer {
                        $htmlnest = array( # Tags that can be nested--??
                                'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
                                'li', 'dl', 'dt', 'dd', 'font', 'big', 'small', 'sub', 'sup', 'span',
-                               'var', 'kbd', 'samp'
+                               'var', 'kbd', 'samp', 'em', 'strong', 'q', 'ruby', 'bdo'
                        );
                        $tabletags = array( # Can only appear inside table, we will close them
                                'td', 'th', 'tr',
@@ -624,7 +628,8 @@ class Sanitizer {
                        while ( substr( $text, $spaceStart + $spaceLen, 1 ) === ' ' ) {
                                $spaceLen++;
                        }
-                       if ( substr( $text, $spaceStart, 1 ) === "\n" and substr( $text, $spaceStart + $spaceLen, 1 ) === "\n" ) {
+                       if ( substr( $text, $spaceStart, 1 ) === "\n"
+                               && substr( $text, $spaceStart + $spaceLen, 1 ) === "\n" ) {
                                # Remove the comment, leading and trailing
                                # spaces, and leave only one newline.
                                $text = substr_replace( $text, "\n", $spaceStart, $spaceLen + 1 );
@@ -740,7 +745,7 @@ class Sanitizer {
 
                        # WAI-ARIA
                        # http://www.w3.org/TR/wai-aria/
-                       # http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#wai-aria
+                       # http://www.whatwg.org/html/elements.html#wai-aria
                        # For now we only support role="presentation" until we work out what roles should be
                        # usable by content and we ensure that our code explicitly rejects patterns that
                        # violate HTML5's ARIA restrictions.
@@ -748,13 +753,18 @@ class Sanitizer {
                                continue;
                        }
 
-                       //RDFa and microdata properties allow URLs, URIs and/or CURIs. check them for sanity
-                       if ( $attribute === 'rel' || $attribute === 'rev' ||
-                               $attribute === 'about' || $attribute === 'property' || $attribute === 'resource' || #RDFa
-                               $attribute === 'datatype' || $attribute === 'typeof' ||                             #RDFa
-                               $attribute === 'itemid' || $attribute === 'itemprop' || $attribute === 'itemref' || #HTML5 microdata
-                               $attribute === 'itemscope' || $attribute === 'itemtype' ) {                         #HTML5 microdata
-
+                       // RDFa and microdata properties allow URLs, URIs and/or CURIs.
+                       // Check them for sanity.
+                       if ( $attribute === 'rel' || $attribute === 'rev'
+                               # RDFa
+                               || $attribute === 'about' || $attribute === 'property'
+                               || $attribute === 'resource' || $attribute === 'datatype'
+                               || $attribute === 'typeof'
+                               # HTML5 microdata
+                               || $attribute === 'itemid' || $attribute === 'itemprop'
+                               || $attribute === 'itemref' || $attribute === 'itemscope'
+                               || $attribute === 'itemtype'
+                       ) {
                                //Paranoia. Allow "simple" values but suppress javascript
                                if ( preg_match( self::EVIL_URI_PATTERN, $value ) ) {
                                        continue;
@@ -766,7 +776,7 @@ class Sanitizer {
                        if ( $attribute === 'href' || $attribute === 'src' ) {
                                if ( !preg_match( $hrefExp, $value ) ) {
                                        continue; //drop any href or src attributes not using an allowed protocol.
-                                                 //NOTE: this also drops all relative URLs
+                                       // NOTE: this also drops all relative URLs
                                }
                        }
 
@@ -813,9 +823,10 @@ class Sanitizer {
        /**
         * Pick apart some CSS and check it for forbidden or unsafe structures.
         * Returns a sanitized string. This sanitized string will have
-        * character references and escape sequences decoded, and comments
-        * stripped. If the input is just too evil, only a comment complaining
-        * about evilness will be returned.
+        * character references and escape sequences decoded and comments
+        * stripped (unless it is itself one valid comment, in which case the value
+        * will be passed through). If the input is just too evil, only a comment
+        * complaining about evilness will be returned.
         *
         * Currently URL references, 'expression', 'tps' are forbidden.
         *
@@ -856,19 +867,24 @@ class Sanitizer {
                $value = preg_replace_callback( $decodeRegex,
                        array( __CLASS__, 'cssDecodeCallback' ), $value );
 
-               // Remove any comments; IE gets token splitting wrong
-               // This must be done AFTER decoding character references and
-               // escape sequences, because those steps can introduce comments
-               // This step cannot introduce character references or escape
-               // sequences, because it replaces comments with spaces rather
-               // than removing them completely.
-               $value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value );
-
-               // Remove anything after a comment-start token, to guard against
-               // incorrect client implementations.
-               $commentPos = strpos( $value, '/*' );
-               if ( $commentPos !== false ) {
-                       $value = substr( $value, 0, $commentPos );
+               // Let the value through if it's nothing but a single comment, to
+               // allow other functions which may reject it to pass some error
+               // message through.
+               if ( !preg_match( '! ^ \s* /\* [^*\\/]* \*/ \s* $ !x', $value ) ) {
+                       // Remove any comments; IE gets token splitting wrong
+                       // This must be done AFTER decoding character references and
+                       // escape sequences, because those steps can introduce comments
+                       // This step cannot introduce character references or escape
+                       // sequences, because it replaces comments with spaces rather
+                       // than removing them completely.
+                       $value = StringUtils::delimiterReplace( '/*', '*/', ' ', $value );
+
+                       // Remove anything after a comment-start token, to guard against
+                       // incorrect client implementations.
+                       $commentPos = strpos( $value, '/*' );
+                       if ( $commentPos !== false ) {
+                               $value = substr( $value, 0, $commentPos );
+                       }
                }
 
                // Reject problematic keywords and control characters
@@ -932,14 +948,7 @@ class Sanitizer {
                $decoded = Sanitizer::decodeTagAttributes( $text );
                $stripped = Sanitizer::validateTagAttributes( $decoded, $element );
 
-               $attribs = array();
-               foreach ( $stripped as $attribute => $value ) {
-                       $encAttribute = htmlspecialchars( $attribute );
-                       $encValue = Sanitizer::safeEncodeAttribute( $value );
-
-                       $attribs[] = "$encAttribute=\"$encValue\"";
-               }
-               return count( $attribs ) ? ' ' . implode( ' ', $attribs ) : '';
+               return Sanitizer::safeEncodeTagAttributes( $stripped );
        }
 
        /**
@@ -1012,7 +1021,7 @@ class Sanitizer {
         *                                                          in the id and
         *                                                          name attributes
         * @see http://www.w3.org/TR/html401/struct/links.html#h-12.2.3 Anchors with the id attribute
-        * @see http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#the-id-attribute
+        * @see http://www.whatwg.org/html/elements.html#the-id-attribute
         *   HTML5 definition of id attribute
         *
         * @param string $id id to escape
@@ -1139,6 +1148,24 @@ class Sanitizer {
                return $attribs;
        }
 
+       /**
+        * Build a partial tag string from an associative array of attribute
+        * names and values as returned by decodeTagAttributes.
+        *
+        * @param $assoc_array Array
+        * @return String
+        */
+       public static function safeEncodeTagAttributes( $assoc_array ) {
+               $attribs = array();
+               foreach ( $assoc_array as $attribute => $value ) {
+                       $encAttribute = htmlspecialchars( $attribute );
+                       $encValue = Sanitizer::safeEncodeAttribute( $value );
+
+                       $attribs[] = "$encAttribute=\"$encValue\"";
+               }
+               return count( $attribs ) ? ' ' . implode( ' ', $attribs ) : '';
+       }
+
        /**
         * Pick the appropriate attribute value from a match set from the
         * attribs regex matches.
@@ -1443,14 +1470,16 @@ class Sanitizer {
                );
 
                if ( $wgAllowRdfaAttributes ) {
-                       #RDFa attributes as specified in section 9 of http://www.w3.org/TR/2008/REC-rdfa-syntax-20081014
+                       # RDFa attributes as specified in section 9 of
+                       # http://www.w3.org/TR/2008/REC-rdfa-syntax-20081014
                        $common = array_merge( $common, array(
                                'about', 'property', 'resource', 'datatype', 'typeof',
                        ) );
                }
 
                if ( $wgAllowMicrodataAttributes ) {
-                       # add HTML5 microdata tags as specified by http://www.whatwg.org/specs/web-apps/current-work/multipage/microdata.html#the-microdata-model
+                       # add HTML5 microdata tags as specified by
+                       # http://www.whatwg.org/html/microdata.html#the-microdata-model
                        $common = array_merge( $common, array(
                                'itemid', 'itemprop', 'itemref', 'itemscope', 'itemtype'
                        ) );
@@ -1562,7 +1591,9 @@ class Sanitizer {
                        'td'         => array_merge( $common, $tablecell, $tablealign ),
                        'th'         => array_merge( $common, $tablecell, $tablealign ),
 
-                       # 12.2 # NOTE: <a> is not allowed directly, but the attrib whitelist is used from the Parser object
+                       # 12.2
+                       # NOTE: <a> is not allowed directly, but the attrib
+                       # whitelist is used from the Parser object
                        'a'          => array_merge( $common, array( 'href', 'rel', 'rev' ) ), # rel/rev esp. for RDFa
 
                        # 13.2
@@ -1589,7 +1620,7 @@ class Sanitizer {
                        'hr'         => array_merge( $common, array( 'noshade', 'size', 'width' ) ),
 
                        # HTML Ruby annotation text module, simple ruby only.
-                       # http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-ruby-element
+                       # http://www.whatwg.org/html/text-level-semantics.html#the-ruby-element
                        'ruby'       => $common,
                        # rbc
                        # rtc
@@ -1606,7 +1637,7 @@ class Sanitizer {
                        'bdi' => $common,
 
                        # HTML5 elements, defined by:
-                       # http://www.whatwg.org/specs/web-apps/current-work/multipage/
+                       # http://www.whatwg.org/html/
                        'data' => array_merge( $common, array( 'value' ) ),
                        'time' => array_merge( $common, array( 'datetime' ) ),
                        'mark' => $common,
@@ -1723,7 +1754,7 @@ class Sanitizer {
         * Does a string look like an e-mail address?
         *
         * This validates an email address using an HTML5 specification found at:
-        * http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#valid-e-mail-address
+        * http://www.whatwg.org/html/states-of-the-type-attribute.html#valid-e-mail-address
         * Which as of 2011-01-24 says:
         *
         *   A valid e-mail address is a string that matches the ABNF production