Make WordPress Core


Ignore:
Timestamp:
07/23/2015 04:49:25 AM (10 years ago)
Author:
pento
Message:

Shortcodes: Improve the reliablity of shortcodes inside HTML tags.

Merge of [33359] to the 4.0 branch.

Props miqrogroove.

See #15694.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.0/src/wp-includes/kses.php

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