Changeset 33380 for branches/4.1/src/wp-includes/kses.php
- Timestamp:
- 07/23/2015 04:36:55 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.1/src/wp-includes/kses.php
r30726 r33380 529 529 530 530 /** 531 * Filters one attribute only and ensures its value is allowed. 532 * 533 * This function has the advantage of being more secure than esc_attr() and can 534 * escape data in some situations where wp_kses() must strip the whole attribute. 535 * 536 * @since 4.2.3 537 * 538 * @param string $string The 'whole' attribute, including name and value. 539 * @param string $element The element name to which the attribute belongs. 540 * @return string Filtered attribute. 541 */ 542 function wp_kses_one_attr( $string, $element ) { 543 $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action'); 544 $allowed_html = wp_kses_allowed_html( 'post' ); 545 $allowed_protocols = wp_allowed_protocols(); 546 $string = wp_kses_no_null( $string, array( 'slash_zero' => 'keep' ) ); 547 $string = wp_kses_js_entities( $string ); 548 $string = wp_kses_normalize_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 and angle braces. 585 $value = htmlspecialchars( $value, ENT_QUOTES, null, false ); 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 /** 531 607 * Return a list of allowed tags and attributes for a given context. 532 608 * … … 748 824 # in $attr2 749 825 $attr2 = ''; 750 751 $allowed_attr = $allowed_html[strtolower($element)]; 752 foreach ($attrarr as $arreach) { 753 if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) ) 754 continue; # the attribute is not allowed 755 756 $current = $allowed_attr[strtolower($arreach['name'])]; 757 if ( $current == '' ) 758 continue; # the attribute is not allowed 759 760 if ( strtolower( $arreach['name'] ) == 'style' ) { 761 $orig_value = $arreach['value']; 762 $value = safecss_filter_attr( $orig_value ); 763 764 if ( empty( $value ) ) 765 continue; 766 767 $arreach['value'] = $value; 768 $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] ); 826 foreach ( $attrarr as $arreach ) { 827 if ( wp_kses_attr_check( $arreach['name'], $arreach['value'], $arreach['whole'], $arreach['vless'], $element, $allowed_html ) ) { 828 $attr2 .= ' '.$arreach['whole']; 769 829 } 770 771 if ( ! is_array($current) ) { 772 $attr2 .= ' '.$arreach['whole']; 773 # there are no checks 774 775 } else { 776 # there are some checks 777 $ok = true; 778 foreach ($current as $currkey => $currval) { 779 if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) { 780 $ok = false; 781 break; 782 } 783 } 784 785 if ( $ok ) 786 $attr2 .= ' '.$arreach['whole']; # it passed them 787 } # if !is_array($current) 788 } # foreach 830 } 789 831 790 832 # Remove any "<" or ">" characters … … 792 834 793 835 return "<$element$attr2$xhtml_slash>"; 836 } 837 838 /** 839 * Determine whether an attribute is allowed. 840 * 841 * @since 4.2.3 842 * 843 * @param string $name The attribute name. Returns empty string when not allowed. 844 * @param string $value The attribute value. Returns a filtered value. 845 * @param string $whole The name=value input. Returns filtered input. 846 * @param string $vless 'y' when attribute like "enabled", otherwise 'n'. 847 * @param string $element The name of the element to which this attribute belongs. 848 * @param array $allowed_html The full list of allowed elements and attributes. 849 * @return bool Is the attribute allowed? 850 */ 851 function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) { 852 $allowed_attr = $allowed_html[strtolower( $element )]; 853 854 $name_low = strtolower( $name ); 855 if ( ! isset( $allowed_attr[$name_low] ) || '' == $allowed_attr[$name_low] ) { 856 $name = $value = $whole = ''; 857 return false; 858 } 859 860 if ( 'style' == $name_low ) { 861 $new_value = safecss_filter_attr( $value ); 862 863 if ( empty( $new_value ) ) { 864 $name = $value = $whole = ''; 865 return false; 866 } 867 868 $whole = str_replace( $value, $new_value, $whole ); 869 $value = $new_value; 870 } 871 872 if ( is_array( $allowed_attr[$name_low] ) ) { 873 // there are some checks 874 foreach ( $allowed_attr[$name_low] as $currkey => $currval ) { 875 if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) { 876 $name = $value = $whole = ''; 877 return false; 878 } 879 } 880 } 881 882 return true; 794 883 } 795 884 … … 921 1010 922 1011 return $attrarr; 1012 } 1013 1014 /** 1015 * Finds all attributes of an HTML element. 1016 * 1017 * Does not modify input. May return "evil" output. 1018 * 1019 * Based on wp_kses_split2() and wp_kses_attr() 1020 * 1021 * @since 4.2.3 1022 * 1023 * @param string $element HTML element/tag 1024 * @return array|bool List of attributes found in $element. Returns false on failure. 1025 */ 1026 function wp_kses_attr_parse( $element ) { 1027 $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches); 1028 if ( 1 !== $valid ) { 1029 return false; 1030 } 1031 1032 $begin = $matches[1]; 1033 $slash = $matches[2]; 1034 $elname = $matches[3]; 1035 $attr = $matches[4]; 1036 $end = $matches[5]; 1037 1038 if ( '' !== $slash ) { 1039 // Closing elements do not get parsed. 1040 return false; 1041 } 1042 1043 // Is there a closing XHTML slash at the end of the attributes? 1044 if ( 1 === preg_match( '%\s*/\s*$%', $attr, $matches ) ) { 1045 $xhtml_slash = $matches[0]; 1046 $attr = substr( $attr, 0, -strlen( $xhtml_slash ) ); 1047 } else { 1048 $xhtml_slash = ''; 1049 } 1050 1051 // Split it 1052 $attrarr = wp_kses_hair_parse( $attr ); 1053 if ( false === $attrarr ) { 1054 return false; 1055 } 1056 1057 // Make sure all input is returned by adding front and back matter. 1058 array_unshift( $attrarr, $begin . $slash . $elname ); 1059 array_push( $attrarr, $xhtml_slash . $end ); 1060 1061 return $attrarr; 1062 } 1063 1064 /** 1065 * Builds an attribute list from string containing attributes. 1066 * 1067 * Does not modify input. May return "evil" output. 1068 * In case of unexpected input, returns false instead of stripping things. 1069 * 1070 * Based on wp_kses_hair() but does not return a multi-dimensional array. 1071 * 1072 * @since 4.2.3 1073 * 1074 * @param string $attr Attribute list from HTML element to closing HTML element tag 1075 * @return array|bool List of attributes found in $attr. Returns false on failure. 1076 */ 1077 function wp_kses_hair_parse( $attr ) { 1078 if ( '' === $attr ) { 1079 return array(); 1080 } 1081 1082 $regex = 1083 '(?:' 1084 . '[-a-zA-Z:]+' // Attribute name. 1085 . '|' 1086 . '\[\[?[^\[\]]+\]\]?' // Shortcode in the name position implies unfiltered_html. 1087 . ')' 1088 . '(?:' // Attribute value. 1089 . '\s*=\s*' // All values begin with '=' 1090 . '(?:' 1091 . '"[^"]*"' // Double-quoted 1092 . '|' 1093 . "'[^']*'" // Single-quoted 1094 . '|' 1095 . '[^\s"\']+' // Non-quoted 1096 . '(?:\s|$)' // Must have a space 1097 . ')' 1098 . '|' 1099 . '(?:\s|$)' // If attribute has no value, space is required. 1100 . ')' 1101 . '\s*'; // Trailing space is optional except as mentioned above. 1102 1103 // Although it is possible to reduce this procedure to a single regexp, 1104 // we must run that regexp twice to get exactly the expected result. 1105 1106 $validation = "%^($regex)+$%"; 1107 $extraction = "%$regex%"; 1108 1109 if ( 1 === preg_match( $validation, $attr ) ) { 1110 preg_match_all( $extraction, $attr, $attrarr ); 1111 return $attrarr[0]; 1112 } else { 1113 return false; 1114 } 923 1115 } 924 1116
Note: See TracChangeset
for help on using the changeset viewer.