Make WordPress Core

Changeset 33388


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

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Merge of [33359] to the 3.8 branch.

Props miqrogroove.

See #15694.

Location:
branches/3.8
Files:
1 added
6 edited

Legend:

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

    r25868 r33388  
    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
     
    281281         */
    282282        function autoembed( $content ) {
     283                // Strip newlines from all elements.
     284                $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     285
     286                // Find URLs that are on their own line.
    283287                return preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $content );
    284288        }
  • branches/3.8/src/wp-includes/formatting.php

    r32191 r33388  
    248248        $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
    249249        $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
     250
     251        // Strip newlines from all elements.
     252        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     253
    250254        if ( strpos($pee, '<object') !== false ) {
    251255                $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
     
    279283
    280284        return $pee;
     285}
     286
     287/**
     288 * Replace characters or phrases within HTML elements only.
     289 *
     290 * @since 4.2.3
     291 *
     292 * @param string $haystack The text which has to be formatted.
     293 * @param array $replace_pairs In the form array('from' => 'to', ...).
     294 * @return string The formatted text.
     295 */
     296function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     297        // Find all elements.
     298        $comments =
     299                  '!'           // Start of comment, after the <.
     300                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     301                .     '-(?!->)' // Dash not followed by end of comment.
     302                .     '[^\-]*+' // Consume non-dashes.
     303                . ')*+'         // Loop possessively.
     304                . '(?:-->)?';   // End of comment. If not found, match all input.
     305
     306        $regex =
     307                  '/('              // Capture the entire match.
     308                .     '<'           // Find start of element.
     309                .     '(?(?=!--)'   // Is this a comment?
     310                .         $comments // Find end of comment.
     311                .     '|'
     312                .         '[^>]*>?' // Find end of element. If not found, match all input.
     313                .     ')'
     314                . ')/s';
     315
     316        $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     317        $changed = false;
     318
     319        // Optimize when searching for one item.
     320        if ( 1 === count( $replace_pairs ) ) {
     321                // Extract $needle and $replace.
     322                foreach ( $replace_pairs as $needle => $replace );
     323
     324                // Loop through delimeters (elements) only.
     325                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     326                        if ( false !== strpos( $textarr[$i], $needle ) ) {
     327                                $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
     328                                $changed = true;
     329                        }
     330                }
     331        } else {
     332                // Extract all $needles.
     333                $needles = array_keys( $replace_pairs );
     334
     335                // Loop through delimeters (elements) only.
     336                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     337                        foreach ( $needles as $needle ) {
     338                                if ( false !== strpos( $textarr[$i], $needle ) ) {
     339                                        $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
     340                                        $changed = true;
     341                                        // After one strtr() break out of the foreach loop and look at next element.
     342                                        break;
     343                                }
     344                        }
     345                }
     346        }
     347
     348        if ( $changed ) {
     349                $haystack = implode( $textarr );
     350        }
     351
     352        return $haystack;
    281353}
    282354
  • branches/3.8/src/wp-includes/kses.php

    r30428 r33388  
    488488
    489489/**
     490 * Filters one attribute only and ensures its value is allowed.
     491 *
     492 * This function has the advantage of being more secure than esc_attr() and can
     493 * escape data in some situations where wp_kses() must strip the whole attribute.
     494 *
     495 * @since 4.2.3
     496 *
     497 * @param string $string The 'whole' attribute, including name and value.
     498 * @param string $element The element name to which the attribute belongs.
     499 * @return string Filtered attribute.
     500 */
     501function wp_kses_one_attr( $string, $element ) {
     502        $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
     503        $allowed_html = wp_kses_allowed_html( 'post' );
     504        $allowed_protocols = wp_allowed_protocols();
     505        $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
     506        $string = wp_kses_js_entities( $string );
     507        $string = wp_kses_normalize_entities( $string );
     508
     509        // Preserve leading and trailing whitespace.
     510        $matches = array();
     511        preg_match('/^\s*/', $string, $matches);
     512        $lead = $matches[0];
     513        preg_match('/\s*$/', $string, $matches);
     514        $trail = $matches[0];
     515        if ( empty( $trail ) ) {
     516                $string = substr( $string, strlen( $lead ) );
     517        } else {
     518                $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
     519        }
     520       
     521        // Parse attribute name and value from input.
     522        $split = preg_split( '/\s*=\s*/', $string, 2 );
     523        $name = $split[0];
     524        if ( count( $split ) == 2 ) {
     525                $value = $split[1];
     526
     527                // Remove quotes surrounding $value.
     528                // Also guarantee correct quoting in $string for this one attribute.
     529                if ( '' == $value ) {
     530                        $quote = '';
     531                } else {
     532                        $quote = $value[0];
     533                }
     534                if ( '"' == $quote || "'" == $quote ) {
     535                        if ( substr( $value, -1 ) != $quote ) {
     536                                return '';
     537                        }
     538                        $value = substr( $value, 1, -1 );
     539                } else {
     540                        $quote = '"';
     541                }
     542
     543                // Sanitize quotes and angle braces.
     544                $value = htmlspecialchars( $value, ENT_QUOTES, null, false );
     545
     546                // Sanitize URI values.
     547                if ( in_array( strtolower( $name ), $uris ) ) {
     548                        $value = wp_kses_bad_protocol( $value, $allowed_protocols );
     549                }
     550
     551                $string = "$name=$quote$value$quote";
     552                $vless = 'n';
     553        } else {
     554                $value = '';
     555                $vless = 'y';
     556        }
     557       
     558        // Sanitize attribute by name.
     559        wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
     560
     561        // Restore whitespace.
     562        return $lead . $string . $trail;
     563}
     564
     565/**
    490566 * Return a list of allowed tags and attributes for a given context.
    491567 *
     
    684760        # in $attr2
    685761        $attr2 = '';
    686 
    687         $allowed_attr = $allowed_html[strtolower($element)];
    688         foreach ($attrarr as $arreach) {
    689                 if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
    690                         continue; # the attribute is not allowed
    691 
    692                 $current = $allowed_attr[strtolower($arreach['name'])];
    693                 if ( $current == '' )
    694                         continue; # the attribute is not allowed
    695 
    696                 if ( strtolower( $arreach['name'] ) == 'style' ) {
    697                         $orig_value = $arreach['value'];
    698                         $value = safecss_filter_attr( $orig_value );
    699 
    700                         if ( empty( $value ) )
    701                                 continue;
    702 
    703                         $arreach['value'] = $value;
    704                         $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
     762        foreach ( $attrarr as $arreach ) {
     763                if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
     764                        $attr2 .= ' '.$arreach['whole'];
    705765                }
    706 
    707                 if ( ! is_array($current) ) {
    708                         $attr2 .= ' '.$arreach['whole'];
    709                 # there are no checks
    710 
    711                 } else {
    712                         # there are some checks
    713                         $ok = true;
    714                         foreach ($current as $currkey => $currval) {
    715                                 if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
    716                                         $ok = false;
    717                                         break;
    718                                 }
    719                         }
    720 
    721                         if ( $ok )
    722                                 $attr2 .= ' '.$arreach['whole']; # it passed them
    723                 } # if !is_array($current)
    724         } # foreach
     766        }
    725767
    726768        # Remove any "<" or ">" characters
     
    728770
    729771        return "<$element$attr2$xhtml_slash>";
     772}
     773
     774/**
     775 * Determine whether an attribute is allowed.
     776 *
     777 * @since 4.2.3
     778 *
     779 * @param string $name The attribute name. Returns empty string when not allowed.
     780 * @param string $value The attribute value. Returns a filtered value.
     781 * @param string $whole The name=value input. Returns filtered input.
     782 * @param string $vless 'y' when attribute like "enabled", otherwise 'n'.
     783 * @param string $element The name of the element to which this attribute belongs.
     784 * @param array $allowed_html The full list of allowed elements and attributes.
     785 * @return bool Is the attribute allowed?
     786 */
     787function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
     788        $allowed_attr = $allowed_html[strtolower( $element )];
     789
     790        $name_low = strtolower( $name );
     791        if ( ! isset( $allowed_attr[$name_low] ) || '' == $allowed_attr[$name_low] ) {
     792                $name = $value = $whole = '';
     793                return false;
     794        }
     795
     796        if ( 'style' == $name_low ) {
     797                $new_value = safecss_filter_attr( $value );
     798
     799                if ( empty( $new_value ) ) {
     800                        $name = $value = $whole = '';
     801                        return false;
     802                }
     803
     804                $whole = str_replace( $value, $new_value, $whole );
     805                $value = $new_value;
     806        }
     807
     808        if ( is_array( $allowed_attr[$name_low] ) ) {
     809                // there are some checks
     810                foreach ( $allowed_attr[$name_low] as $currkey => $currval ) {
     811                        if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
     812                                $name = $value = $whole = '';
     813                                return false;
     814                        }
     815                }
     816        }
     817
     818        return true;
    730819}
    731820
     
    857946
    858947        return $attrarr;
     948}
     949
     950/**
     951 * Finds all attributes of an HTML element.
     952 *
     953 * Does not modify input.  May return "evil" output.
     954 *
     955 * Based on wp_kses_split2() and wp_kses_attr()
     956 *
     957 * @since 4.2.3
     958 *
     959 * @param string $element HTML element/tag
     960 * @return array|bool List of attributes found in $element. Returns false on failure.
     961 */
     962function wp_kses_attr_parse( $element ) {
     963        $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
     964        if ( 1 !== $valid ) {
     965                return false;
     966        }
     967
     968        $begin =  $matches[1];
     969        $slash =  $matches[2];
     970        $elname = $matches[3];
     971        $attr =   $matches[4];
     972        $end =    $matches[5];
     973
     974        if ( '' !== $slash ) {
     975                // Closing elements do not get parsed.
     976                return false;
     977        }
     978
     979        // Is there a closing XHTML slash at the end of the attributes?
     980        if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
     981                $xhtml_slash = $matches[0];
     982                $attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
     983        } else {
     984                $xhtml_slash = '';
     985        }
     986       
     987        // Split it
     988        $attrarr = wp_kses_hair_parse( $attr );
     989        if ( false === $attrarr ) {
     990                return false;
     991        }
     992
     993        // Make sure all input is returned by adding front and back matter.
     994        array_unshift( $attrarr, $begin . $slash . $elname );
     995        array_push( $attrarr, $xhtml_slash . $end );
     996       
     997        return $attrarr;
     998}
     999
     1000/**
     1001 * Builds an attribute list from string containing attributes.
     1002 *
     1003 * Does not modify input.  May return "evil" output.
     1004 * In case of unexpected input, returns false instead of stripping things.
     1005 *
     1006 * Based on wp_kses_hair() but does not return a multi-dimensional array.
     1007 *
     1008 * @since 4.2.3
     1009 *
     1010 * @param string $attr Attribute list from HTML element to closing HTML element tag
     1011 * @return array|bool List of attributes found in $attr. Returns false on failure.
     1012 */
     1013function wp_kses_hair_parse( $attr ) {
     1014        if ( '' === $attr ) {
     1015                return array();
     1016        }
     1017
     1018        $regex =
     1019          '(?:'
     1020        .     '[-a-zA-Z:]+'   // Attribute name.
     1021        . '|'
     1022        .     '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
     1023        . ')'
     1024        . '(?:'               // Attribute value.
     1025        .     '\s*=\s*'       // All values begin with '='
     1026        .     '(?:'
     1027        .         '"[^"]*"'   // Double-quoted
     1028        .     '|'
     1029        .         "'[^']*'"   // Single-quoted
     1030        .     '|'
     1031        .         '[^\s"\']+' // Non-quoted
     1032        .         '(?:\s|$)'  // Must have a space
     1033        .     ')'
     1034        . '|'
     1035        .     '(?:\s|$)'      // If attribute has no value, space is required.
     1036        . ')'
     1037        . '\s*';              // Trailing space is optional except as mentioned above.
     1038
     1039        // Although it is possible to reduce this procedure to a single regexp,
     1040        // we must run that regexp twice to get exactly the expected result.
     1041
     1042        $validation = "%^($regex)+$%";
     1043        $extraction = "%$regex%";
     1044
     1045        if ( 1 === preg_match( $validation, $attr ) ) {
     1046                preg_match_all( $extraction, $attr, $attrarr );
     1047                return $attrarr[0];
     1048        } else {
     1049                return false;
     1050        }
    8591051}
    8601052
  • branches/3.8/src/wp-includes/shortcodes.php

    r25880 r33388  
    177177 *
    178178 * @param string $content Content to search for shortcodes
     179 * @param bool $ignore_html When true, shortcodes inside HTML elements will be skipped.
    179180 * @return string Content with shortcodes filtered out.
    180181 */
    181 function do_shortcode($content) {
    182         global $shortcode_tags;
     182function do_shortcode( $content, $ignore_html = false ) {
     183        global $shortcode_tags;
     184
     185        if ( false === strpos( $content, '[' ) ) {
     186                return $content;
     187        }
    183188
    184189        if (empty($shortcode_tags) || !is_array($shortcode_tags))
    185190                return $content;
    186191
     192        $tagnames = array_keys($shortcode_tags);
     193        $tagregexp = join( '|', array_map('preg_quote', $tagnames) );
     194        $pattern = "/\\[($tagregexp)/s";
     195
     196        if ( 1 !== preg_match( $pattern, $content ) ) {
     197                // Avoids parsing HTML when there are no shortcodes or embeds anyway.
     198                return $content;
     199        }
     200
     201        $content = do_shortcodes_in_html_tags( $content, $ignore_html );
     202
    187203        $pattern = get_shortcode_regex();
    188         return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
     204        $content = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
     205       
     206        // Always restore square braces so we don't break things like <!--[if IE ]>
     207        $content = unescape_invalid_shortcodes( $content );
     208       
     209        return $content;
    189210}
    190211
     
    279300
    280301/**
     302 * Search only inside HTML elements for shortcodes and process them.
     303 *
     304 * Any [ or ] characters remaining inside elements will be HTML encoded
     305 * to prevent interference with shortcodes that are outside the elements.
     306 * Assumes $content processed by KSES already.  Users with unfiltered_html
     307 * capability may get unexpected output if angle braces are nested in tags.
     308 *
     309 * @since 4.2.3
     310 *
     311 * @param string $content Content to search for shortcodes
     312 * @param bool $ignore_html When true, all square braces inside elements will be encoded.
     313 * @return string Content with shortcodes filtered out.
     314 */
     315function do_shortcodes_in_html_tags( $content, $ignore_html ) {
     316        // Normalize entities in unfiltered HTML before adding placeholders.
     317        $trans = array( '&#91;' => '&#091;', '&#93;' => '&#093;' );
     318        $content = strtr( $content, $trans );
     319        $trans = array( '[' => '&#91;', ']' => '&#93;' );
     320       
     321        $pattern = get_shortcode_regex();
     322
     323        $comment_regex =
     324                  '!'           // Start of comment, after the <.
     325                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     326                .     '-(?!->)' // Dash not followed by end of comment.
     327                .     '[^\-]*+' // Consume non-dashes.
     328                . ')*+'         // Loop possessively.
     329                . '(?:-->)?';   // End of comment. If not found, match all input.
     330
     331        $regex =
     332                  '/('                   // Capture the entire match.
     333                .     '<'                // Find start of element.
     334                .     '(?(?=!--)'        // Is this a comment?
     335                .         $comment_regex // Find end of comment.
     336                .     '|'
     337                .         '[^>]*>?'      // Find end of element. If not found, match all input.
     338                .     ')'
     339                . ')/s';
     340
     341        $textarr = preg_split( $regex, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
     342
     343        foreach ( $textarr as &$element ) {
     344                if ( '<' !== $element[0] ) {
     345                        continue;
     346                }
     347
     348                $noopen = false === strpos( $element, '[' );
     349                $noclose = false === strpos( $element, ']' );
     350                if ( $noopen || $noclose ) {
     351                        // This element does not contain shortcodes.
     352                        if ( $noopen xor $noclose ) {
     353                                // Need to encode stray [ or ] chars.
     354                                $element = strtr( $element, $trans );
     355                        }
     356                        continue;
     357                }
     358
     359                if ( $ignore_html || '<!--' === substr( $element, 0, 4 ) ) {
     360                        // Encode all [ and ] chars.
     361                        $element = strtr( $element, $trans );
     362                        continue;
     363                }
     364
     365                $attributes = wp_kses_attr_parse( $element );
     366                if ( false === $attributes ) {
     367                        // Looks like we found some crazy unfiltered HTML.  Skipping it for sanity.
     368                        $element = strtr( $element, $trans );
     369                        continue;
     370                }
     371               
     372                // Get element name
     373                $front = array_shift( $attributes );
     374                $back = array_pop( $attributes );
     375                $matches = array();
     376                preg_match('%[a-zA-Z0-9]+%', $front, $matches);
     377                $elname = $matches[0];
     378               
     379                // Look for shortcodes in each attribute separately.
     380                foreach ( $attributes as &$attr ) {
     381                        $open = strpos( $attr, '[' );
     382                        $close = strpos( $attr, ']' );
     383                        if ( false === $open || false === $close ) {
     384                                continue; // Go to next attribute.  Square braces will be escaped at end of loop.
     385                        }
     386                        $double = strpos( $attr, '"' );
     387                        $single = strpos( $attr, "'" );
     388                        if ( ( false === $single || $open < $single ) && ( false === $double || $open < $double ) ) {
     389                                // $attr like '[shortcode]' or 'name = [shortcode]' implies unfiltered_html.
     390                                // In this specific situation we assume KSES did not run because the input
     391                                // was written by an administrator, so we should avoid changing the output
     392                                // and we do not need to run KSES here.
     393                                $attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr );
     394                        } else {
     395                                // $attr like 'name = "[shortcode]"' or "name = '[shortcode]'"
     396                                // We do not know if $content was unfiltered. Assume KSES ran before shortcodes.
     397                                $count = 0;
     398                                $new_attr = preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $attr, -1, $count );
     399                                if ( $count > 0 ) {
     400                                        // Sanitize the shortcode output using KSES.
     401                                        $new_attr = wp_kses_one_attr( $new_attr, $elname );
     402                                        if ( '' !== $new_attr ) {
     403                                                // The shortcode is safe to use now.
     404                                                $attr = $new_attr;
     405                                        }
     406                                }
     407                        }
     408                }
     409                $element = $front . implode( '', $attributes ) . $back;
     410               
     411                // Now encode any remaining [ or ] chars.
     412                $element = strtr( $element, $trans );
     413        }
     414       
     415        $content = implode( '', $textarr );
     416       
     417        return $content;
     418}
     419
     420/**
     421 * Remove placeholders added by do_shortcodes_in_html_tags().
     422 *
     423 * @since 4.2.3
     424 *
     425 * @param string $content Content to search for placeholders.
     426 * @return string Content with placeholders removed.
     427 */
     428function unescape_invalid_shortcodes( $content ) {
     429        // Clean up entire string, avoids re-parsing HTML.
     430        $trans = array( '&#91;' => '[', '&#93;' => ']' );
     431        $content = strtr( $content, $trans );
     432       
     433        return $content;
     434}
     435
     436/**
    281437 * Retrieve all attributes from the shortcodes tag.
    282438 *
     
    372528                return $content;
    373529
     530        $content = do_shortcodes_in_html_tags( $content, true );
     531
    374532        $pattern = get_shortcode_regex();
    375 
    376         return preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
     533        $content = preg_replace_callback( "/$pattern/s", 'strip_shortcode_tag', $content );
     534
     535        // Always restore square braces so we don't break things like <!--[if IE ]>
     536        $content = unescape_invalid_shortcodes( $content );
     537       
     538        return $content;
    377539}
    378540
  • branches/3.8/tests/phpunit/tests/kses.php

    r26431 r33388  
    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.8/tests/phpunit/tests/shortcode.php

    r32150 r33388  
    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.