Make WordPress Core

Changeset 33359


Ignore:
Timestamp:
07/22/2015 05:14:50 AM (11 years ago)
Author:
pento
Message:

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Props miqrogroove.

See #15694.

Location:
trunk
Files:
6 edited

Legend:

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

    r32545 r33359  
    6060
    6161                // Do the shortcode (only the [embed] one is registered)
    62                 $content = do_shortcode( $content );
     62                $content = do_shortcode( $content, true );
    6363
    6464                // Put the original shortcodes back
     
    319319         */
    320320        public function autoembed( $content ) {
     321                // Strip newlines from all elements.
     322                $content = wp_replace_in_html_tags( $content, array( "\n" => " " ) );
     323
     324                // Find URLs that are on their own line.
    321325                return preg_replace_callback( '|^(\s*)(https?://[^\s"]+)(\s*)$|im', array( $this, 'autoembed_callback' ), $content );
    322326        }
  • trunk/src/wp-includes/formatting.php

    r33271 r33359  
    505505        $pee = str_replace(array("\r\n", "\r"), "\n", $pee);
    506506
     507        // Strip newlines from all elements.
     508        $pee = wp_replace_in_html_tags( $pee, array( "\n" => " " ) );
     509
    507510        // Collapse line breaks before and after <option> elements so they don't get autop'd.
    508511        if ( strpos( $pee, '<option' ) !== false ) {
     
    591594
    592595        return $pee;
     596}
     597
     598/**
     599 * Replace characters or phrases within HTML elements only.
     600 *
     601 * @since 4.2.3
     602 *
     603 * @param string $haystack The text which has to be formatted.
     604 * @param array $replace_pairs In the form array('from' => 'to', ...).
     605 * @return string The formatted text.
     606 */
     607function wp_replace_in_html_tags( $haystack, $replace_pairs ) {
     608        // Find all elements.
     609        $comments =
     610                  '!'           // Start of comment, after the <.
     611                . '(?:'         // Unroll the loop: Consume everything until --> is found.
     612                .     '-(?!->)' // Dash not followed by end of comment.
     613                .     '[^\-]*+' // Consume non-dashes.
     614                . ')*+'         // Loop possessively.
     615                . '(?:-->)?';   // End of comment. If not found, match all input.
     616
     617        $regex =
     618                  '/('              // Capture the entire match.
     619                .     '<'           // Find start of element.
     620                .     '(?(?=!--)'   // Is this a comment?
     621                .         $comments // Find end of comment.
     622                .     '|'
     623                .         '[^>]*>?' // Find end of element. If not found, match all input.
     624                .     ')'
     625                . ')/s';
     626
     627        $textarr = preg_split( $regex, $haystack, -1, PREG_SPLIT_DELIM_CAPTURE );
     628        $changed = false;
     629
     630        // Optimize when searching for one item.
     631        if ( 1 === count( $replace_pairs ) ) {
     632                // Extract $needle and $replace.
     633                foreach ( $replace_pairs as $needle => $replace );
     634
     635                // Loop through delimeters (elements) only.
     636                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     637                        if ( false !== strpos( $textarr[$i], $needle ) ) {
     638                                $textarr[$i] = str_replace( $needle, $replace, $textarr[$i] );
     639                                $changed = true;
     640                        }
     641                }
     642        } else {
     643                // Extract all $needles.
     644                $needles = array_keys( $replace_pairs );
     645
     646                // Loop through delimeters (elements) only.
     647                for ( $i = 1, $c = count( $textarr ); $i < $c; $i += 2 ) {
     648                        foreach ( $needles as $needle ) {
     649                                if ( false !== strpos( $textarr[$i], $needle ) ) {
     650                                        $textarr[$i] = strtr( $textarr[$i], $replace_pairs );
     651                                        $changed = true;
     652                                        // After one strtr() break out of the foreach loop and look at next element.
     653                                        break;
     654                                }
     655                        }
     656                }
     657        }
     658
     659        if ( $changed ) {
     660                $haystack = implode( $textarr );
     661        }
     662
     663        return $haystack;
    593664}
    594665
  • trunk/src/wp-includes/kses.php

    r32860 r33359  
    530530
    531531/**
     532 * Filters one attribute only and ensures its value is allowed.
     533 *
     534 * This function has the advantage of being more secure than esc_attr() and can
     535 * escape data in some situations where wp_kses() must strip the whole attribute.
     536 *
     537 * @since 4.2.3
     538 *
     539 * @param string $string The 'whole' attribute, including name and value.
     540 * @param string $element The element name to which the attribute belongs.
     541 * @return string Filtered attribute.
     542 */
     543function wp_kses_one_attr( $string, $element ) {
     544        $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
     545        $allowed_html = wp_kses_allowed_html( 'post' );
     546        $allowed_protocols = wp_allowed_protocols();
     547        $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) );
     548        $string = wp_kses_js_entities( $string );
     549       
     550        // Preserve leading and trailing whitespace.
     551        $matches = array();
     552        preg_match('/^\s*/', $string, $matches);
     553        $lead = $matches[0];
     554        preg_match('/\s*$/', $string, $matches);
     555        $trail = $matches[0];
     556        if ( empty( $trail ) ) {
     557                $string = substr( $string, strlen( $lead ) );
     558        } else {
     559                $string = substr( $string, strlen( $lead ), -strlen( $trail ) );
     560        }
     561       
     562        // Parse attribute name and value from input.
     563        $split = preg_split( '/\s*=\s*/', $string, 2 );
     564        $name = $split[0];
     565        if ( count( $split ) == 2 ) {
     566                $value = $split[1];
     567
     568                // Remove quotes surrounding $value.
     569                // Also guarantee correct quoting in $string for this one attribute.
     570                if ( '' == $value ) {
     571                        $quote = '';
     572                } else {
     573                        $quote = $value[0];
     574                }
     575                if ( '"' == $quote || "'" == $quote ) {
     576                        if ( substr( $value, -1 ) != $quote ) {
     577                                return '';
     578                        }
     579                        $value = substr( $value, 1, -1 );
     580                } else {
     581                        $quote = '"';
     582                }
     583
     584                // Sanitize quotes, angle braces, and entities.
     585                $value = esc_attr( $value );
     586
     587                // Sanitize URI values.
     588                if ( in_array( strtolower( $name ), $uris ) ) {
     589                        $value = wp_kses_bad_protocol( $value, $allowed_protocols );
     590                }
     591
     592                $string = "$name=$quote$value$quote";
     593                $vless = 'n';
     594        } else {
     595                $value = '';
     596                $vless = 'y';
     597        }
     598       
     599        // Sanitize attribute by name.
     600        wp_kses_attr_check( $name, $value, $string, $vless, $element, $allowed_html );
     601
     602        // Restore whitespace.
     603        return $lead . $string . $trail;
     604}
     605
     606/**
    532607 * Return a list of allowed tags and attributes for a given context.
    533608 *
     
    742817 */
    743818function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
    744         // Is there a closing XHTML slash at the end of the attributes?
    745 
    746819        if ( ! is_array( $allowed_html ) )
    747820                $allowed_html = wp_kses_allowed_html( $allowed_html );
    748821
     822        // Is there a closing XHTML slash at the end of the attributes?
    749823        $xhtml_slash = '';
    750824        if (preg_match('%\s*/\s*$%', $attr))
     
    761835        // in $attr2
    762836        $attr2 = '';
    763 
    764         $allowed_attr = $allowed_html[strtolower($element)];
    765         foreach ($attrarr as $arreach) {
    766                 if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
    767                         continue; // the attribute is not allowed
    768 
    769                 $current = $allowed_attr[strtolower($arreach['name'])];
    770                 if ( $current == '' )
    771                         continue; // the attribute is not allowed
    772 
    773                 if ( strtolower( $arreach['name'] ) == 'style' ) {
    774                         $orig_value = $arreach['value'];
    775                         $value = safecss_filter_attr( $orig_value );
    776 
    777                         if ( empty( $value ) )
    778                                 continue;
    779 
    780                         $arreach['value'] = $value;
    781                         $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
     837        foreach ( $attrarr as $arreach ) {
     838                if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) {
     839                        $attr2 .= ' '.$arreach['whole'];
    782840                }
    783 
    784                 if ( ! is_array($current) ) {
    785                         $attr2 .= ' '.$arreach['whole'];
    786                 // there are no checks
    787 
    788                 } else {
    789                         // there are some checks
    790                         $ok = true;
    791                         foreach ($current as $currkey => $currval) {
    792                                 if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
    793                                         $ok = false;
    794                                         break;
    795                                 }
    796                         }
    797 
    798                         if ( $ok )
    799                                 $attr2 .= ' '.$arreach['whole']; // it passed them
    800                 } // if !is_array($current)
    801         } // foreach
     841        }
    802842
    803843        // Remove any "<" or ">" characters
     
    805845
    806846        return "<$element$attr2$xhtml_slash>";
     847}
     848
     849/**
     850 * Determine whether an attribute is allowed.
     851 *
     852 * @since 4.2.3
     853 *
     854 * @param string $name The attribute name. Returns empty string when not allowed.
     855 * @param string $value The attribute value. Returns a filtered value.
     856 * @param string $whole The name=value input. Returns filtered input.
     857 * @param string $vless 'y' when attribute like "enabled", otherwise 'n'.
     858 * @param string $element The name of the element to which this attribute belongs.
     859 * @param array $allowed_html The full list of allowed elements and attributes.
     860 * @return bool Is the attribute allowed?
     861 */
     862function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {
     863        $allowed_attr = $allowed_html[strtolower( $element )];
     864
     865        $name_low = strtolower( $name );
     866        if ( ! isset( $allowed_attr[$name_low] ) || '' == $allowed_attr[$name_low] ) {
     867                $name = $value = $whole = '';
     868                return false;
     869        }
     870
     871        if ( 'style' == $name_low ) {
     872                $new_value = safecss_filter_attr( $value );
     873
     874                if ( empty( $new_value ) ) {
     875                        $name = $value = $whole = '';
     876                        return false;
     877                }
     878
     879                $whole = str_replace( $value, $new_value, $whole );
     880                $value = $new_value;
     881        }
     882
     883        if ( is_array( $allowed_attr[$name_low] ) ) {
     884                // there are some checks
     885                foreach ( $allowed_attr[$name_low] as $currkey => $currval ) {
     886                        if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {
     887                                $name = $value = $whole = '';
     888                                return false;
     889                        }
     890                }
     891        }
     892
     893        return true;
    807894}
    808895
     
    9341021
    9351022        return $attrarr;
     1023}
     1024
     1025/**
     1026 * Finds all attributes of an HTML element.
     1027 *
     1028 * Does not modify input.  May return "evil" output.
     1029 *
     1030 * Based on wp_kses_split2() and wp_kses_attr()
     1031 *
     1032 * @since 4.2.3
     1033 *
     1034 * @param string $element HTML element/tag
     1035 * @return array|bool List of attributes found in $element. Returns false on failure.
     1036 */
     1037function wp_kses_attr_parse( $element ) {
     1038        $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
     1039        if ( 1 !== $valid ) {
     1040                return false;
     1041        }
     1042
     1043        $begin =  $matches[1];
     1044        $slash =  $matches[2];
     1045        $elname = $matches[3];
     1046        $attr =   $matches[4];
     1047        $end =    $matches[5];
     1048
     1049        if ( '' !== $slash ) {
     1050                // Closing elements do not get parsed.
     1051                return false;
     1052        }
     1053
     1054        // Is there a closing XHTML slash at the end of the attributes?
     1055        if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) {
     1056                $xhtml_slash = $matches[0];
     1057                $attr = substr( $attr, 0, -strlen( $xhtml_slash ) );
     1058        } else {
     1059                $xhtml_slash = '';
     1060        }
     1061       
     1062        // Split it
     1063        $attrarr = wp_kses_hair_parse( $attr );
     1064        if ( false === $attrarr ) {
     1065                return false;
     1066        }
     1067
     1068        // Make sure all input is returned by adding front and back matter.
     1069        array_unshift( $attrarr, $begin . $slash . $elname );
     1070        array_push( $attrarr, $xhtml_slash . $end );
     1071       
     1072        return $attrarr;
     1073}
     1074
     1075/**
     1076 * Builds an attribute list from string containing attributes.
     1077 *
     1078 * Does not modify input.  May return "evil" output.
     1079 * In case of unexpected input, returns false instead of stripping things.
     1080 *
     1081 * Based on wp_kses_hair() but does not return a multi-dimensional array.
     1082 *
     1083 * @since 4.2.3
     1084 *
     1085 * @param string $attr Attribute list from HTML element to closing HTML element tag
     1086 * @return array|bool List of attributes found in $attr. Returns false on failure.
     1087 */
     1088function wp_kses_hair_parse( $attr ) {
     1089        if ( '' === $attr ) {
     1090                return array();
     1091        }
     1092
     1093        $regex =
     1094          '(?:'
     1095        .     '[-a-zA-Z:]+'   // Attribute name.
     1096        . '|'
     1097        .     '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html.
     1098        . ')'
     1099        . '(?:'               // Attribute value.
     1100        .     '\s*=\s*'       // All values begin with '='
     1101        .     '(?:'
     1102        .         '"[^"]*"'   // Double-quoted
     1103        .     '|'
     1104        .         "'[^']*'"   // Single-quoted
     1105        .     '|'
     1106        .         '[^\s"\']+' // Non-quoted
     1107        .         '(?:\s|$)'  // Must have a space
     1108        .     ')'
     1109        . '|'
     1110        .     '(?:\s|$)'      // If attribute has no value, space is required.
     1111        . ')'
     1112        . '\s*';              // Trailing space is optional except as mentioned above.
     1113
     1114        // Although it is possible to reduce this procedure to a single regexp,
     1115        // we must run that regexp twice to get exactly the expected result.
     1116
     1117        $validation = "%^($regex)+$%";
     1118        $extraction = "%$regex%";
     1119
     1120        if ( 1 === preg_match( $validation, $attr ) ) {
     1121                preg_match_all( $extraction, $attr, $attrarr );
     1122                return $attrarr[0];
     1123        } else {
     1124                return false;
     1125        }
    9361126}
    9371127
  • trunk/src/wp-includes/shortcodes.php

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

    r32860 r33359  
    465465                );
    466466        }
     467
     468        /**
     469         * Test new function wp_kses_hair_parse().
     470         *
     471         * @dataProvider data_hair_parse
     472         */
     473        function test_hair_parse( $input, $output ) {
     474                return $this->assertEquals( $output, wp_kses_hair_parse( $input ) );
     475        }
     476
     477        function data_hair_parse() {
     478                return array(
     479                        array(
     480                                'title="hello" href="#" id="my_id" ',
     481                                array( 'title="hello" ', 'href="#" ', 'id="my_id" ' ),
     482                        ),
     483                        array(
     484                                '[shortcode attr="value"] href="http://www.google.com/"title="moo"disabled',
     485                                array( '[shortcode attr="value"] ', 'href="http://www.google.com/"', 'title="moo"', 'disabled' ),
     486                        ),
     487                        array(
     488                                '',
     489                                array(),
     490                        ),
     491                        array(
     492                                'a',
     493                                array( 'a' ),
     494                        ),
     495                        array(
     496                                'title="hello"disabled href=# id=\'my_id\'',
     497                                array( 'title="hello"', 'disabled ', 'href=# ', "id='my_id'" ),
     498                        ),
     499                        array(
     500                                '     ', // Calling function is expected to strip leading whitespace.
     501                                false,
     502                        ),
     503                        array(
     504                                'abcd=abcd"abcd"',
     505                                false,
     506                        ),
     507                        array(
     508                                "array[1]='z'z'z'z",
     509                                false,
     510                        ),
     511                );
     512        }
     513
     514        /**
     515         * Test new function wp_kses_attr_parse().
     516         *
     517         * @dataProvider data_attr_parse
     518         */
     519        function test_attr_parse( $input, $output ) {
     520                return $this->assertEquals( $output, wp_kses_attr_parse( $input ) );
     521        }
     522
     523        function data_attr_parse() {
     524                return array(
     525                        array(
     526                                '<a title="hello" href="#" id="my_id" >',
     527                                array( '<a ', 'title="hello" ', 'href="#" ', 'id="my_id" ', '>' ),
     528                        ),
     529                        array(
     530                                '<a [shortcode attr="value"] href="http://www.google.com/"title="moo"disabled>',
     531                                array( '<a ', '[shortcode attr="value"] ', 'href="http://www.google.com/"', 'title="moo"', 'disabled', '>' ),
     532                        ),
     533                        array(
     534                                '',
     535                                false,
     536                        ),
     537                        array(
     538                                'a',
     539                                false,
     540                        ),
     541                        array(
     542                                '<a>',
     543                                array( '<a', '>' ),
     544                        ),
     545                        array(
     546                                '<a%%&&**>',
     547                                false,
     548                        ),
     549                        array(
     550                                '<a title="hello"disabled href=# id=\'my_id\'>',
     551                                array( '<a ', 'title="hello"', 'disabled ', 'href=# ', "id='my_id'", ">" ),
     552                        ),
     553                        array(
     554                                '<a     >',
     555                                array( '<a     ', '>' ),
     556                        ),
     557                        array(
     558                                '<a abcd=abcd"abcd">',
     559                                false,
     560                        ),
     561                        array(
     562                                "<a array[1]='z'z'z'z>",
     563                                false,
     564                        ),
     565                        array(
     566                                '<img title="hello" src="#" id="my_id" />',
     567                                array( '<img ', 'title="hello" ', 'src="#" ', 'id="my_id"', ' />' ),
     568                        ),
     569                );
     570        }
     571
     572        /**
     573         * Test new function wp_kses_one_attr().
     574         *
     575         * @dataProvider data_one_attr
     576         */
     577        function test_one_attr( $element, $input, $output ) {
     578                return $this->assertEquals( $output, wp_kses_one_attr( $input, $element ) );
     579        }
     580
     581        function data_one_attr() {
     582                return array(
     583                        array(
     584                                'a',
     585                                ' title="hello" ',
     586                                ' title="hello" ',
     587                        ),
     588                        array(
     589                                'a',
     590                                'title  =  "hello"',
     591                                'title="hello"',
     592                        ),
     593                        array(
     594                                'a',
     595                                "title='hello'",
     596                                "title='hello'",
     597                        ),
     598                        array(
     599                                'a',
     600                                'title=hello',
     601                                'title="hello"',
     602                        ),
     603                        array(
     604                                'a',
     605                                'href="javascript:alert(1)"',
     606                                'href="alert(1)"',
     607                        ),
     608                        array(
     609                                'a',
     610                                'style ="style "',
     611                                'style="style"',
     612                        ),
     613                        array(
     614                                'a',
     615                                'style="style "',
     616                                'style="style"',
     617                        ),
     618                        array(
     619                                'a',
     620                                'style ="style ="',
     621                                '',
     622                        ),
     623                        array(
     624                                'img',
     625                                'src="mypic.jpg"',
     626                                'src="mypic.jpg"',
     627                        ),
     628                        array(
     629                                'img',
     630                                'onerror=alert(1)',
     631                                '',
     632                        ),
     633                        array(
     634                                'img',
     635                                'title=>',
     636                                'title="&gt;"',
     637                        ),
     638                        array(
     639                                'img',
     640                                'title="&garbage";"',
     641                                'title="&amp;garbage&quot;;"',
     642                        ),
     643                );
     644        }
    467645}
  • trunk/tests/phpunit/tests/shortcode.php

    r33118 r33359  
    405405
    406406        /**
     407         * Check for bugginess using normal input with latest patches.
     408         *
     409         * @dataProvider data_escaping
     410         */
     411        function test_escaping( $input, $output ) {
     412                return $this->assertEquals( $output, do_shortcode( $input ) );
     413        }
     414
     415        function data_escaping() {
     416                return array(
     417                        array(
     418                                '<!--[if lt IE 7]>',
     419                                '<!--[if lt IE 7]>',
     420                        ),
     421                        array(
     422                                '[gallery title="<div>hello</div>"]',
     423                                '',
     424                        ),
     425                        array(
     426                                '[caption caption="test" width="2"]<div>hello</div>[/caption]',
     427                                '<div style="width: 12px" class="wp-caption alignnone"><div>hello</div><p class="wp-caption-text">test</p></div>',
     428                        ),
     429                        array(
     430                                '<div [gallery]>',
     431                                '<div >',
     432                        ),
     433                        array(
     434                                '<div [[gallery]]>',
     435                                '<div [gallery]>',
     436                        ),
     437                        array(
     438                                '[gallery]<div>Hello</div>[/gallery]',
     439                                '',
     440                        ),
     441                );
     442        }
     443
     444        /**
     445         * Check for bugginess using normal input with latest patches.
     446         *
     447         * @dataProvider data_escaping2
     448         */
     449        function test_escaping2( $input, $output ) {
     450                return $this->assertEquals( $output, strip_shortcodes( $input ) );
     451        }
     452
     453        function data_escaping2() {
     454                return array(
     455                        array(
     456                                '<!--[if lt IE 7]>',
     457                                '<!--[if lt IE 7]>',
     458                        ),
     459                        array(
     460                                '[gallery title="<div>hello</div>"]',
     461                                '',
     462                        ),
     463                        array(
     464                                '[caption caption="test" width="2"]<div>hello</div>[/caption]',
     465                                '',
     466                        ),
     467                        array(
     468                                '<div [gallery]>', // Shortcodes will never be stripped inside elements.
     469                                '<div [gallery]>',
     470                        ),
     471                        array(
     472                                '<div [[gallery]]>', // Shortcodes will never be stripped inside elements.
     473                                '<div [[gallery]]>',
     474                        ),
     475                        array(
     476                                '[gallery]<div>Hello</div>[/gallery]',
     477                                '',
     478                        ),
     479                );
     480        }
     481
     482        /**
    407483         * @ticket 26343
    408484         */
Note: See TracChangeset for help on using the changeset viewer.