Skip to content

Commit 01caa72

Browse files
committed
Grouped backports to the 4.7 branch.
- Query: Improve sanitization within `WP_Tax_Query`. - Query: Improve sanitization within `WP_Meta_Query`. - Upgrade/Install: Avoid using `unserialize()` unnecessarily. - Formatting: Correctly encode ASCII characters in post slugs. Merges [52454-52457] to the 4.7 branch. Props vortfu, dd32, ehtis, zieladam, whyisjake, xknown, peterwilsoncc, desrosj, iandunn. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@52476 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a3b1bac commit 01caa72

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

src/wp-admin/includes/upgrade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,8 @@ function upgrade_280() {
12451245
$start = 0;
12461246
while( $rows = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
12471247
foreach ( $rows as $row ) {
1248-
$value = $row->option_value;
1249-
if ( !@unserialize( $value ) )
1248+
$value = maybe_unserialize( $row->option_value );
1249+
if ( $value === $row->option_value )
12501250
$value = stripslashes( $value );
12511251
if ( $value !== $row->option_value ) {
12521252
update_option( $row->option_name, $value );

src/wp-includes/class-wp-meta-query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ protected function find_compatible_table_alias( $clause, $parent_query ) {
717717
$clause_compare = strtoupper( $clause['compare'] );
718718
$sibling_compare = strtoupper( $sibling['compare'] );
719719
if ( in_array( $clause_compare, $compatible_compares ) && in_array( $sibling_compare, $compatible_compares ) ) {
720-
$alias = $sibling['alias'];
720+
$alias = preg_replace( '/\W/', '_', $sibling['alias'] );
721721
break;
722722
}
723723
}

src/wp-includes/class-wp-tax-query.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ protected function find_compatible_table_alias( $clause, $parent_query ) {
543543

544544
// The sibling must both have compatible operator to share its alias.
545545
if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators ) ) {
546-
$alias = $sibling['alias'];
546+
$alias = preg_replace( '/\W/', '_', $sibling['alias'] );
547547
break;
548548
}
549549
}
@@ -573,7 +573,11 @@ private function clean_query( &$query ) {
573573
return;
574574
}
575575

576-
$query['terms'] = array_unique( (array) $query['terms'] );
576+
if ( 'slug' === $query['field'] || 'name' === $query['field'] ) {
577+
$query['terms'] = array_unique( (array) $query['terms'] );
578+
} else {
579+
$query['terms'] = wp_parse_id_list( $query['terms'] );
580+
}
577581

578582
if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
579583
$this->transform_query( $query, 'term_id' );

src/wp-includes/formatting.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,12 +1070,14 @@ function wp_check_invalid_utf8( $string, $strip = false ) {
10701070
* Encode the Unicode values to be used in the URI.
10711071
*
10721072
* @since 1.5.0
1073+
* @since 5.8.3 Added the `encode_ascii_characters` parameter.
10731074
*
1074-
* @param string $utf8_string
1075-
* @param int $length Max length of the string
1075+
* @param string $utf8_string String to encode.
1076+
* @param int $length Max length of the string
1077+
* @param bool $encode_ascii_characters Whether to encode ascii characters such as < " '
10761078
* @return string String with Unicode encoded for URI.
10771079
*/
1078-
function utf8_uri_encode( $utf8_string, $length = 0 ) {
1080+
function utf8_uri_encode( $utf8_string, $length = 0, $encode_ascii_characters = false ) {
10791081
$unicode = '';
10801082
$values = array();
10811083
$num_octets = 1;
@@ -1090,11 +1092,14 @@ function utf8_uri_encode( $utf8_string, $length = 0 ) {
10901092
$value = ord( $utf8_string[ $i ] );
10911093

10921094
if ( $value < 128 ) {
1093-
if ( $length && ( $unicode_length >= $length ) ) {
1095+
$char = chr( $value );
1096+
$encoded_char = $encode_ascii_characters ? rawurlencode( $char ) : $char;
1097+
$encoded_char_length = strlen( $encoded_char );
1098+
if ( $length && ( $unicode_length + $encoded_char_length ) > $length ) {
10941099
break;
10951100
}
1096-
$unicode .= chr( $value );
1097-
$unicode_length++;
1101+
$unicode .= $encoded_char;
1102+
$unicode_length += $encoded_char_length;
10981103
} else {
10991104
if ( count( $values ) == 0 ) {
11001105
if ( $value < 224 ) {

src/wp-includes/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3817,7 +3817,7 @@ function _truncate_post_slug( $slug, $length = 200 ) {
38173817
if ( $decoded_slug === $slug )
38183818
$slug = substr( $slug, 0, $length );
38193819
else
3820-
$slug = utf8_uri_encode( $decoded_slug, $length );
3820+
$slug = utf8_uri_encode( $decoded_slug, $length, true );
38213821
}
38223822

38233823
return rtrim( $slug, '-' );

0 commit comments

Comments
 (0)