Make WordPress Core

Changeset 33386


Ignore:
Timestamp:
07/23/2015 05:00:44 AM (11 years ago)
Author:
pento
Message:

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Merge of [33359] to the 3.9 branch.

Props miqrogroove.

See #15694.

Location:
branches/3.9
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9/src/wp-includes/class-wp-embed.php

    r28083 r33386  
    5858
    5959                // Do the shortcode (only the [embed] one is registered)
    60                 $content = do_shortcode( $content );
     60                $content = do_shortcode( $content, true );
    6161
    6262                // Put the original shortcodes back
    … …  
    292292         */
    293293        function autoembed( $content ) {
     294                // Strip newlines from all elements.
     295                $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     296
     297                // Find URLs that are on their own line.
    294298                return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
    295299        }
  • branches/3.9/src/wp-includes/formatting.php

    r32190 r33386  
    292292        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
    293293
     294        // Strip newlines from all elements.
     295        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     296
    294297        if ( strpos( $pee, '</object>' ) !== false ) {
    295298                // no P/BR around param and embed
    … …  
    338341
    339342        return $pee;
     343}
     344
     345/**
     346 * Replace characters or phrases within HTML elements only.
     347 *
     348 * @since 4.2.3
     349 *
     350 * @param string $haystack The text which has to be formatted.
     351 * @param array $replace_pairs In the form array('from' => 'to', ...).
     352 * @return string The formatted text.
     353 */
     354function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     355        // Find all elements.
     356        $comments =
     357                  '!'           // Start of comment, after the <.
     358                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     359                .     '-(?!->)' // Dash not followed by end of comment.
     360                .     '[^\-]*+' // Consume non-dashes.
     361                . ')*+'         // Loop possessively.
     362                . '(?:-->)?';   // End of comment. If not found, match all input.
     363
     364        $regex =
     365                  '/('              // Capture the entire match.
     366                .     '<'           // Find start of element.
     367                .     '(?(?=!--)'   // Is this a comment?
     368                .         $comments // Find end of comment.
     369                .     '|'
     370                .         '[^>]*>?' // Find end of element. If not found, match all input.
     371                .     ')'
     372                . ')/s';
     373
     374        $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     375        $changed = false;
     376
     377        // Optimize when searching for one item.
     378        if ( 1 === count( $replace_pairs ) ) {
     379                // Extract $needle and $replace.
     380                foreach ( $replace_pairs as $needle => $replace );
     381
     382                // Loop through delimeters (elements) only.
     383                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     384                        if ( false !== strpos( $textarr[$i], $needle ) ) {
     385                                $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
     386                                $changed = true;
     387                        }
     388                }
     389        } else {
     390                // Extract all $needles.
     391                $needles = array_keys( $replace_pairs );
     392
     393                // Loop through delimeters (elements) only.
     394                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     395                        foreach ( $needles as $needle ) {
     396                                if ( false !== strpos( $textarr[$i], $needle ) ) {
     397                                        $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
     398                                        $changed = true;
     399                                        // After one strtr() break out of the foreach loop and look at next element.
     400                                        break;
     401                                }
     402                        }
     403                }
     404        }
     405
     406        if ( $changed ) {
     407                $haystack = implode( $textarr );
     408        }
     409
     410        return $haystack;
    340411}
    341412
  • branches/3.9/src/wp-includes/kses.php

    r30427 r33386  
    491491
    492492/**
     493 * Filters one attribute only and ensures its value is allowed.
     494 *
     495 * This function has the advantage of being more secure than esc_attr() and can
     496 * escape data in some situations where wp_kses() must strip the whole attribute.
     497 *
     498 * @since 4.2.3
     499 *
     500 * @param string $string The 'whole' attribute, including name and value.
     501 * @param string $element The element name to which the attribute belongs.
     502 * @return string Filtered attribute.
     503 */
     504function wp_kses_one_attr( $string, $element ) {
     505        $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
     506        $allowed_html = wp_kses_allowed_html( 'post' );
     507        $allowed_protocols = wp_allowed_protocols();
     508        $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
     509        $string = wp_kses_js_entities( $string );
     510        $string = wp_kses_normalize_entities( $string );
     511
     512        // Preserve leading and trailing whitespace.
     513        $matches = array();
     514        preg_match('/^\s*/', $string, $matches);
     515        $lead = $matches[0];
     516        preg_match('/\s*$/', $string, $matches);
     517        $trail = $matches[0];
     518        if ( empty( $trail ) ) {
     519                $string = substr( $string, strlen( $lead ) );
     520        } else {
     521                $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
     522        }
     523       
     524        // Parse attribute name and value from input.
     525        $split = preg_split( '/\s*=\s*/', $string, 2 );
     526        $name = $split[0];
     527        if ( count( $split ) == 2 ) {
     528                $value = $split[1];
     529
     530                // Remove quotes surrounding $value.
     531                // Also guarantee correct quoting in $string for this one attribute.
     532                if ( '' == $value ) {
     533                        $quote = '';
     534                } else {
     535                        $quote = $value[0];
     536                }
     537                if ( '"' == $quote || "'" == $quote ) {
     538                        if ( substr( $value, -1 ) != $quote ) {
     539                                return '';
     540                        }
     541                        $value = substr( $value, 1, -1 );
     542                } else {
     543                        $quote = '"';
     544                }
     545
     546                // Sanitize quotes and angle braces.
     547                $value = htmlspecialchars( $value, ENT_QUOTES, null, false );
     548
     549                // Sanitize URI values.
     550                if ( in_array( strtolower( $name ), $uris ) ) {
     551                        $value = wp_kses_bad_protocol( $value, $allowed_protocols );
     552                }
     553
     554                $string = "$name=$quote$value$quote";
     555                $vless = 'n';
     556        } else {
     557                $value = '';
     558                $vless = 'y';
     559        }
     560       
     561        // Sanitize attribute by name.
     562        wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
     563
     564        // Restore whitespace.
     565        return $lead . $string . $trail;
     566}
     567
     568/**
    493569 * Return a list of allowed tags and attributes for a given context.
    494570 *
    … …  
    711787        # in $attr2
    712788        $attr2 = '';
    713 
    714         $allowed_attr = $allowed_html[strtolower($element)];
    715         foreach ($attrarr as $arreach) {
    716                 if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
    717                         continue; # the attribute is not allowed
    718 
    719                 $current = $allowed_attr[strtolower($arreach['name'])];
    720                 if ( $current == '' )
    721                         continue; # the attribute is not allowed
    722 
    723                 if ( strtolower( $arreach['name'] ) == 'style' ) {
    724                         $orig_value = $arreach['value'];
    725                         $value = safecss_filter_attr( $orig_value );
    726 
    727                         if ( empty( $value ) )
    728                                 continue;
    729 
    730                         $arreach['value'] = $value;
    731                         $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
     789        foreach ( $attrarr as $arreach ) {
     790                if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
     791                        $attr2 .= ' '.$arreach['whole'];
    732792                }
    733 
    734                 if ( ! is_array($current) ) {
    735                         $attr2 .= ' '.$arreach['whole'];
    736                 # there are no checks
    737 
    738                 } else {
    739                         # there are some checks
    740                         $ok = true;
    741                         foreach ($current as $currkey => $currval) {
    742                                 if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
    743                                         $ok = false;
    744                                         break;
    745                                 }
    746                         }
    747 
    748                         if ( $ok )
    749                                 $attr2 .= ' '.$arreach['whole']; # it passed them
    750                 } # if !is_array($current)
    751         } # foreach
     793        }
    752794
    753795        # Remove any "<" or ">" characters
    … …  
    755797
    756798        return "<$element$attr2$xhtml_slash>";
     799}
     800
     801/**
     802 * Determine whether an attribute is allowed.
     803 *
     804 * @since 4.2.3
     805 *
     806 * @param string $name The attribute name. Returns empty string when not allowed.
     807 * @param string $value The attribute value. Returns a filtered value.
     808 * @param string $whole The name=value input. Returns filtered input.
     809 * @param string $vless 'y' when attribute like "enabled", otherwise 'n'.
     810 * @param string $element The name of the element to which this attribute belongs.
     811 * @param array $allowed_html The full list of allowed elements and attributes.
     812 * @return bool Is the attribute allowed?
     813 */
     814function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
     815        $allowed_attr = $allowed_html[strtolower( $element )];
     816
     817        $name_low = strtolower( $name );
     818        if ( ! isset( $allowed_attr[$name_low] ) || '' == $allowed_attr[$name_low] ) {
     819                $name = $value = $whole = '';
     820                return false;
     821        }
     822
     823        if ( 'style' == $name_low ) {
     824                $new_value = safecss_filter_attr( $value );
     825
     826                if ( empty( $new_value ) ) {
     827                        $name = $value = $whole = '';
     828                        return false;
     829                }
     830
     831                $whole = str_replace( $value, $new_value, $whole );
     832                $value = $new_value;
     833        }
     834
     835        if ( is_array( $allowed_attr[$name_low] ) ) {
     836                // there are some checks
     837                foreach ( $allowed_attr[$name_low] as $currkey => $currval ) {
     838                        if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
     839                                $name = $value = $whole = '';
     840                                return false;
     841                        }
     842                }
     843        }
     844
     845        return true;
    757846}
    758847
    … …  
    884973
    885974        return $attrarr;
     975}
     976
     977/**
     978 * Finds all attributes of an HTML element.
     979 *
     980 * Does not modify input.  May return "evil" output.
     981 *
     982 * Based on wp_kses_split2() and wp_kses_attr()
     983 *
     984 * @since 4.2.3
     985 *
     986 * @param string $element HTML element/tag
     987 * @return array|bool List of attributes found in $element. Returns false on failure.
     988 */
     989function wp_kses_attr_parse( $element ) {
     990        $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
     991        if ( 1 !== $valid ) {
     992                return false;
     993        }
     994
     995        $begin =  $matches[1];
     996        $slash =  $matches[2];
     997        $elname = $matches[3];
     998        $attr =   $matches[4];
     999        $end =    $matches[5];
     1000
     1001        if ( '' !== $slash ) {
     1002                // Closing elements do not get parsed.
     1003                return false;
     1004        }
     1005
     1006        // Is there a closing XHTML slash at the end of the attributes?
     1007        if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
     1008                $xhtml_slash = $matches[0];
     1009                $attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
     1010        } else {
     1011                $xhtml_slash = '';
     1012        }
     1013       
     1014        // Split it
     1015        $attrarr = wp_kses_hair_parse( $attr );
     1016        if ( false === $attrarr ) {
     1017                return false;
     1018        }
     1019
     1020        // Make sure all input is returned by adding front and back matter.
     1021        array_unshift( $attrarr, $begin . $slash . $elname );
     1022        array_push( $attrarr, $xhtml_slash . $end );
     1023       
     1024        return $attrarr;
     1025}
     1026
     1027/**
     1028 * Builds an attribute list from string containing attributes.
     1029 *
     1030 * Does not modify input.  May return "evil" output.
     1031 * In case of unexpected input, returns false instead of stripping things.
     1032 *
     1033 * Based on wp_kses_hair() but does not return a multi-dimensional array.
     1034 *
     1035 * @since 4.2.3
     1036 *
     1037 * @param string $attr Attribute list from HTML element to closing HTML element tag
     1038 * @return array|bool List of attributes found in $attr. Returns false on failure.
     1039 */
     1040function wp_kses_hair_parse( $attr ) {
     1041        if ( '' === $attr ) {
     1042                return array();
     1043        }
     1044
     1045        $regex =
     1046          '(?:'
     1047        .     '[-a-zA-Z:]+'   // Attribute name.
     1048        . '|'
     1049        .     '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
     1050        . ')'
     1051        . '(?:'               // Attribute value.
     1052        .     '\s*=\s*'       // All values begin with '='
     1053        .     '(?:'
     1054        .         '"[^"]*"'   // Double-quoted
     1055        .     '|'
     1056        .         "'[^']*'"   // Single-quoted
     1057        .     '|'
     1058        .         '[^\s"\']+' // Non-quoted
     1059        .         '(?:\s|$)'  // Must have a space
     1060        .     ')'
     1061        . '|'
     1062        .     '(?:\s|$)'      // If attribute has no value, space is required.
     1063        . ')'
     1064        . '\s*';              // Trailing space is optional except as mentioned above.
     1065
     1066        // Although it is possible to reduce this procedure to a single regexp,
     1067        // we must run that regexp twice to get exactly the expected result.
     1068
     1069        $validation = "%^($regex)+$%";
     1070        $extraction = "%$regex%";
     1071
     1072        if ( 1 === preg_match( $validation, $attr ) ) {
     1073                preg_match_all( $extraction, $attr, $attrarr );
     1074                return $attrarr[0];
     1075        } else {
     1076                return false;
     1077        }
    8861078}
    8871079
  • branches/3.9/src/wp-includes/shortcodes.php

    r27394 r33386  
    186186 *
    187187 * @param string $content Content to search for shortcodes
     188 * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
    188189 * @return string Content with shortcodes filtered out.
    189190 */
    190 function do_shortcode($content) {
     191function do_shortcode( $content, $ignore_html = false ) {
    191192        global $shortcode_tags;
    192193
    … …  
    198199                return $content;
    199200
     201        $tagnames = array_keys($shortcode_tags);
     202        $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
     203        $pattern = "/\\[($tagregexp)/s";
     204
     205        if ( 1 !== preg_match( $pattern, $content ) ) {
     206                // Avoids parsing HTML when there are no shortcodes or embeds anyway.
     207                return $content;
     208        }
     209
     210        $content = do_shortcodes_in_html_tags( $content, $ignore_html );
     211
    200212        $pattern = get_shortcode_regex();
    201         return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
     213        $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
     214       
     215        // Always restore square braces so we don't break things like <!--[if IE ]>
     216        $content = unescape_invalid_shortcodes( $content );
     217       
     218        return $content;
    202219}
    203220
    … …  
    293310
    294311/**
     312 * Search only inside HTML elements for shortcodes and process them.
     313 *
     314 * Any [ or ] characters remaining inside elements will be HTML encoded
     315 * to prevent interference with shortcodes that are outside the elements.
     316 * Assumes $content processed by KSES already.  Users with unfiltered_html
     317 * capability may get unexpected output if angle braces are nested in tags.
     318 *
     319 * @since 4.2.3
     320 *
     321 * @param string $content Content to search for shortcodes
     322 * @param bool $ignore_html When true, all square braces inside elements will be encoded.
     323 * @return string Content with shortcodes filtered out.
     324 */
     325function do_shortcodes_in_html_tags( $content, $ignore_html ) {
     326        // Normalize entities in unfiltered HTML before adding placeholders.
     327        $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
     328        $content = strtr( $content, $trans );
     329        $trans = array( '[' => '&#91;', ']' => '&#93;' );
     330       
     331        $pattern = get_shortcode_regex();
     332
     333        $comment_regex =
     334                  '!'           // Start of comment, after the <.
     335                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     336                .     '-(?!->)' // Dash not followed by end of comment.
     337                .     '[^\-]*+' // Consume non-dashes.
     338                . ')*+'         // Loop possessively.
     339                . '(?:-->)?';   // End of comment. If not found, match all input.
     340
     341        $regex =
     342                  '/('                   // Capture the entire match.
     343                .     '<'                // Find start of element.
     344                .     '(?(?=!--)'        // Is this a comment?
     345                .         $comment_regex // Find end of comment.
     346                .     '|'
     347                .         '[^>]*>?'      // Find end of element. If not found, match all input.
     348                .     ')'
     349                . ')/s';
     350
     351        $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     352
     353        foreach ( $textarr as &$element ) {
     354                if ( '<' !== $element[0] ) {
     355                        continue;
     356                }
     357
     358                $noopen = false === strpos( $element, '[' );
     359                $noclose = false === strpos( $element, ']' );
     360                if ( $noopen || $noclose ) {
     361                        // This element does not contain shortcodes.
     362                        if ( $noopen xor $noclose ) {
     363                                // Need to encode stray [ or ] chars.
     364                                $element = strtr( $element, $trans );
     365                        }
     366                        continue;
     367                }
     368
     369                if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
     370                        // Encode all [ and ] chars.
     371                        $element = strtr( $element, $trans );
     372                        continue;
     373                }
     374
     375                $attributes = wp_kses_attr_parse( $element );
     376                if ( false === $attributes ) {
     377                        // Looks like we found some crazy unfiltered HTML.  Skipping it for sanity.
     378                        $element = strtr( $element, $trans );
     379                        continue;
     380                }
     381               
     382                // Get element name
     383                $front = array_shift( $attributes );
     384                $back = array_pop( $attributes );
     385                $matches = array();
     386                preg_match('%[a-zA-Z0-9]+%', $front, $matches);
     387                $elname = $matches[0];
     388               
     389                // Look for shortcodes in each attribute separately.
     390                foreach ( $attributes as &$attr ) {
     391                        $open = strpos( $attr, '[' );
     392                        $close = strpos( $attr, ']' );
     393                        if ( false === $open || false === $close ) {
     394                                continue; // Go to next attribute.  Square braces will be escaped at end of loop.
     395                        }
     396                        $double = strpos( $attr, '"' );
     397                        $single = strpos( $attr, "'" );
     398                        if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
     399                                // $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
     400                                // In this specific situation we assume KSES did not run because the input
     401                                // was written by an administrator, so we should avoid changing the output
     402                                // and we do not need to run KSES here.
     403                                $attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr );
     404                        } else {
     405                                // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'"
     406                                // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
     407                                $count = 0;
     408                                $new_attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr, -1, $count );
     409                                if ( $count > 0 ) {
     410                                        // Sanitize the shortcode output using KSES.
     411                                        $new_attr = wp_kses_one_attr( $new_attr, $elname );
     412                                        if ( '' !== $new_attr ) {
     413                                                // The shortcode is safe to use now.
     414                                                $attr = $new_attr;
     415                                        }
     416                                }
     417                        }
     418                }
     419                $element = $front . implode( '', $attributes ) . $back;
     420               
     421                // Now encode any remaining [ or ] chars.
     422                $element = strtr( $element, $trans );
     423        }
     424       
     425        $content = implode( '', $textarr );
     426       
     427        return $content;
     428}
     429
     430/**
     431 * Remove placeholders added by do_shortcodes_in_html_tags().
     432 *
     433 * @since 4.2.3
     434 *
     435 * @param string $content Content to search for placeholders.
     436 * @return string Content with placeholders removed.
     437 */
     438function unescape_invalid_shortcodes( $content ) {
     439        // Clean up entire string, avoids re-parsing HTML.
     440        $trans = array( '&#91;' => '[', '&#93;' => ']' );
     441        $content = strtr( $content, $trans );
     442       
     443        return $content;
     444}
     445
     446/**
    295447 * Retrieve all attributes from the shortcodes tag.
    296448 *
    … …  
    391543                return $content;
    392544
     545        $content = do_shortcodes_in_html_tags( $content, true );
     546
    393547        $pattern = get_shortcode_regex();
    394 
    395         return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
     548        $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
     549
     550        // Always restore square braces so we don't break things like <!--[if IE ]>
     551        $content = unescape_invalid_shortcodes( $content );
     552       
     553        return $content;
    396554}
    397555
  • branches/3.9/tests/phpunit/tests/kses.php

    r26431 r33386  
    375375                $this->assertEquals( '&there4;', wp_kses_normalize_entities( '&there4;' ) );
    376376        }
     377
     378        /**
     379         * Test new function wp_kses_hair_parse().
     380         *
     381         * @dataProvider data_hair_parse
     382         */
     383        function test_hair_parse( $input, $output ) {
     384                return $this->assertEquals( $output, wp_kses_hair_parse( $input ) );
     385        }
     386
     387        function data_hair_parse() {
     388                return array(
     389                        array(
     390                                'title="hello" href="#" id="my_id" ',
     391                                array( 'title="hello" ', 'href="#" ', 'id="my_id" ' ),
     392                        ),
     393                        array(
     394                                '[shortcode attr="value"] href="http://www.google.com/"title="moo"disabled',
     395                                array( '[shortcode attr="value"] ', 'href="http://www.google.com/"', 'title="moo"', 'disabled' ),
     396                        ),
     397                        array(
     398                                '',
     399                                array(),
     400                        ),
     401                        array(
     402                                'a',
     403                                array( 'a' ),
     404                        ),
     405                        array(
     406                                'title="hello"disabled href=# id=\'my_id\'',
     407                                array( 'title="hello"', 'disabled ', 'href=# ', "id='my_id'" ),
     408                        ),
     409                        array(
     410                                '     ', // Calling function is expected to strip leading whitespace.
     411                                false,
     412                        ),
     413                        array(
     414                                'abcd=abcd"abcd"',
     415                                false,
     416                        ),
     417                        array(
     418                                "array[1]='z'z'z'z",
     419                                false,
     420                        ),
     421                );
     422        }
     423
     424        /**
     425         * Test new function wp_kses_attr_parse().
     426         *
     427         * @dataProvider data_attr_parse
     428         */
     429        function test_attr_parse( $input, $output ) {
     430                return $this->assertEquals( $output, wp_kses_attr_parse( $input ) );
     431        }
     432
     433        function data_attr_parse() {
     434                return array(
     435                        array(
     436                                '<a title="hello" href="#" id="my_id" >',
     437                                array( '<a ', 'title="hello" ', 'href="#" ', 'id="my_id" ', '>' ),
     438                        ),
     439                        array(
     440                                '<a [shortcode attr="value"] href="http://www.google.com/"title="moo"disabled>',
     441                                array( '<a ', '[shortcode attr="value"] ', 'href="http://www.google.com/"', 'title="moo"', 'disabled', '>' ),
     442                        ),
     443                        array(
     444                                '',
     445                                false,
     446                        ),
     447                        array(
     448                                'a',
     449                                false,
     450                        ),
     451                        array(
     452                                '<a>',
     453                                array( '<a', '>' ),
     454                        ),
     455                        array(
     456                                '<a%%&&**>',
     457                                false,
     458                        ),
     459                        array(
     460                                '<a title="hello"disabled href=# id=\'my_id\'>',
     461                                array( '<a ', 'title="hello"', 'disabled ', 'href=# ', "id='my_id'", ">" ),
     462                        ),
     463                        array(
     464                                '<a     >',
     465                                array( '<a     ', '>' ),
     466                        ),
     467                        array(
     468                                '<a abcd=abcd"abcd">',
     469                                false,
     470                        ),
     471                        array(
     472                                "<a array[1]='z'z'z'z>",
     473                                false,
     474                        ),
     475                        array(
     476                                '<img title="hello" src="#" id="my_id" />',
     477                                array( '<img ', 'title="hello" ', 'src="#" ', 'id="my_id"', ' />' ),
     478                        ),
     479                );
     480        }
     481
     482        /**
     483         * Test new function wp_kses_one_attr().
     484         *
     485         * @dataProvider data_one_attr
     486         */
     487        function test_one_attr( $element, $input, $output ) {
     488                return $this->assertEquals( $output, wp_kses_one_attr( $input, $element ) );
     489        }
     490
     491        function data_one_attr() {
     492                return array(
     493                        array(
     494                                'a',
     495                                ' title="hello" ',
     496                                ' title="hello" ',
     497                        ),
     498                        array(
     499                                'a',
     500                                'title  =  "hello"',
     501                                'title="hello"',
     502                        ),
     503                        array(
     504                                'a',
     505                                "title='hello'",
     506                                "title='hello'",
     507                        ),
     508                        array(
     509                                'a',
     510                                'title=hello',
     511                                'title="hello"',
     512                        ),
     513                        array(
     514                                'a',
     515                                'href="javascript:alert(1)"',
     516                                'href="alert(1)"',
     517                        ),
     518                        array(
     519                                'a',
     520                                'style ="style "',
     521                                'style="style"',
     522                        ),
     523                        array(
     524                                'a',
     525                                'style="style "',
     526                                'style="style"',
     527                        ),
     528                        array(
     529                                'a',
     530                                'style ="style ="',
     531                                '',
     532                        ),
     533                        array(
     534                                'img',
     535                                'src="mypic.jpg"',
     536                                'src="mypic.jpg"',
     537                        ),
     538                        array(
     539                                'img',
     540                                'onerror=alert(1)',
     541                                '',
     542                        ),
     543                        array(
     544                                'img',
     545                                'title=>',
     546                                'title="&gt;"',
     547                        ),
     548                        array(
     549                                'img',
     550                                'title="&garbage";"',
     551                                'title="&amp;garbage&quot;;"',
     552                        ),
     553                );
     554        }
    377555}
  • branches/3.9/tests/phpunit/tests/shortcode.php

    r32149 r33386  
    374374        }
    375375
     376        /**
     377         * Check for bugginess using normal input with latest patches.
     378         *
     379         * @dataProvider data_escaping
     380         */
     381        function test_escaping( $input, $output ) {
     382                return $this->assertEquals( $output, do_shortcode( $input ) );
     383        }
     384
     385        function data_escaping() {
     386                return array(
     387                        array(
     388                                '<!--[if lt IE 7]>',
     389                                '<!--[if lt IE 7]>',
     390                        ),
     391                        array(
     392                                '[gallery title="<div>hello</div>"]',
     393                                '',
     394                        ),
     395                        array(
     396                                '[caption caption="test" width="2"]<div>hello</div>[/caption]',
     397                                '<div style="width: 12px" class="wp-caption alignnone"><div>hello</div><p class="wp-caption-text">test</p></div>',
     398                        ),
     399                        array(
     400                                '<div [gallery]>',
     401                                '<div >',
     402                        ),
     403                        array(
     404                                '<div [[gallery]]>',
     405                                '<div [gallery]>',
     406                        ),
     407                        array(
     408                                '[gallery]<div>Hello</div>[/gallery]',
     409                                '',
     410                        ),
     411                );
     412        }
     413
     414        /**
     415         * Check for bugginess using normal input with latest patches.
     416         *
     417         * @dataProvider data_escaping2
     418         */
     419        function test_escaping2( $input, $output ) {
     420                return $this->assertEquals( $output, strip_shortcodes( $input ) );
     421        }
     422
     423        function data_escaping2() {
     424                return array(
     425                        array(
     426                                '<!--[if lt IE 7]>',
     427                                '<!--[if lt IE 7]>',
     428                        ),
     429                        array(
     430                                '[gallery title="<div>hello</div>"]',
     431                                '',
     432                        ),
     433                        array(
     434                                '[caption caption="test" width="2"]<div>hello</div>[/caption]',
     435                                '',
     436                        ),
     437                        array(
     438                                '<div [gallery]>', // Shortcodes will never be stripped inside elements.
     439                                '<div [gallery]>',
     440                        ),
     441                        array(
     442                                '<div [[gallery]]>', // Shortcodes will never be stripped inside elements.
     443                                '<div [[gallery]]>',
     444                        ),
     445                        array(
     446                                '[gallery]<div>Hello</div>[/gallery]',
     447                                '',
     448                        ),
     449                );
     450        }
     451
    376452}
Note: See TracChangeset for help on using the changeset viewer.