diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 4ab8d11e24..067249a510 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -3754,6 +3754,11 @@ function _deep_replace( $search, $subject ) { * Sometimes, spot-escaping is required or useful. One example * is preparing an array for use in an IN clause. * + * NOTE: Since 4.8.3, '%' characters will be replaced with a placeholder string, + * this prevents certain SQLi attacks from taking place. This change in behaviour + * may cause issues for code that expects the return value of esc_sql() to be useable + * for other purposes. + * * @since 2.8.0 * * @global wpdb $wpdb WordPress database abstraction object. diff --git a/wp-includes/meta.php b/wp-includes/meta.php index a9e8f987f9..1aca31a62c 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -364,12 +364,11 @@ function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $d return false; if ( $delete_all ) { - $value_clause = ''; if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { - $value_clause = $wpdb->prepare( " AND meta_value = %s", $meta_value ); + $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) ); + } else { + $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); } - - $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s $value_clause", $meta_key ) ); } /** diff --git a/wp-includes/post.php b/wp-includes/post.php index d82e8a4d40..1dbf0c9867 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4317,10 +4317,10 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { $page_path = str_replace('%2F', '/', $page_path); $page_path = str_replace('%20', ' ', $page_path); $parts = explode( '/', trim( $page_path, '/' ) ); - $parts = esc_sql( $parts ); $parts = array_map( 'sanitize_title_for_query', $parts ); + $escaped_parts = esc_sql( $parts ); - $in_string = "'" . implode( "','", $parts ) . "'"; + $in_string = "'" . implode( "','", $escaped_parts ) . "'"; if ( is_array( $post_type ) ) { $post_types = $post_type; diff --git a/wp-includes/version.php b/wp-includes/version.php index ada4e25d77..14841aab4c 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9-RC1-42050'; +$wp_version = '4.9-RC1-42056'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 3f7d5ac8b2..e7ec314e7b 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -1106,20 +1106,22 @@ class wpdb { function _real_escape( $string ) { if ( $this->dbh ) { if ( $this->use_mysqli ) { - return mysqli_real_escape_string( $this->dbh, $string ); + $escaped = mysqli_real_escape_string( $this->dbh, $string ); } else { - return mysql_real_escape_string( $string, $this->dbh ); + $escaped = mysql_real_escape_string( $string, $this->dbh ); } + } else { + $class = get_class( $this ); + if ( function_exists( '__' ) ) { + /* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */ + _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); + } else { + _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); + } + $escaped = addslashes( $string ); } - $class = get_class( $this ); - if ( function_exists( '__' ) ) { - /* translators: %s: database access abstraction class, usually wpdb or a class extending wpdb */ - _doing_it_wrong( $class, sprintf( __( '%s must set a database connection for use with escaping.' ), $class ), '3.6.0' ); - } else { - _doing_it_wrong( $class, sprintf( '%s must set a database connection for use with escaping.', $class ), '3.6.0' ); - } - return addslashes( $string ); + return $this->add_placeholder_escape( $escaped ); } /** @@ -1201,14 +1203,14 @@ class wpdb { * * All placeholders MUST be left unquoted in the query string. A corresponding argument MUST be passed for each placeholder. * + * For compatibility with old behavior, numbered or formatted string placeholders (eg, %1$s, %5s) will not have quotes + * added by this function, so should be passed with appropriate quotes around them for your usage. + * * Literal percentage signs (%) in the query string must be written as %%. Percentage wildcards (for example, * to use in LIKE syntax) must be passed via a substitution argument containing the complete LIKE string, these * cannot be inserted directly in the query string. Also see {@see esc_like()}. * - * This method DOES NOT support sign, padding, alignment, width or precision specifiers. - * This method DOES NOT support argument numbering or swapping. - * - * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination + * Arguments may be passed as individual arguments to the method, or as a single array containing all arguments. A combination * of the two is not supported. * * Examples: @@ -1225,8 +1227,9 @@ class wpdb { * @return string|void Sanitized query string, if there is a query to prepare. */ public function prepare( $query, $args ) { - if ( is_null( $query ) ) + if ( is_null( $query ) ) { return; + } // This is not meant to be foolproof -- but it will catch obviously incorrect usage. if ( strpos( $query, '%' ) === false ) { @@ -1237,8 +1240,10 @@ class wpdb { $args = func_get_args(); array_shift( $args ); - // If args were passed as an array (as in vsprintf), move them up + // If args were passed as an array (as in vsprintf), move them up. + $passed_as_array = false; if ( is_array( $args[0] ) && count( $args ) == 1 ) { + $passed_as_array = true; $args = $args[0]; } @@ -1249,28 +1254,62 @@ class wpdb { } } - $query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it - $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting - $query = preg_replace( '|(?add_placeholder_escape( $query ); } /** @@ -1893,6 +1932,64 @@ class wpdb { } } + /** + * Generates and returns a placeholder escape string for use in queries returned by ::prepare(). + * + * @since 4.8.3 + * + * @return string String to escape placeholders. + */ + public function placeholder_escape() { + static $placeholder; + + if ( ! $placeholder ) { + // If ext/hash is not present, compat.php's hash_hmac() does not support sha256. + $algo = function_exists( 'hash' ) ? 'sha256' : 'sha1'; + // Old WP installs may not have AUTH_SALT defined. + $salt = defined( 'AUTH_SALT' ) ? AUTH_SALT : rand(); + + $placeholder = '{' . hash_hmac( $algo, uniqid( $salt, true ), $salt ) . '}'; + } + + /* + * Add the filter to remove the placeholder escaper. Uses priority 0, so that anything + * else attached to this filter will recieve the query with the placeholder string removed. + */ + if ( ! has_filter( 'query', array( $this, 'remove_placeholder_escape' ) ) ) { + add_filter( 'query', array( $this, 'remove_placeholder_escape' ), 0 ); + } + + return $placeholder; + } + + /** + * Adds a placeholder escape string, to escape anything that resembles a printf() placeholder. + * + * @since 4.8.3 + * + * @param string $query The query to escape. + * @return string The query with the placeholder escape string inserted where necessary. + */ + public function add_placeholder_escape( $query ) { + /* + * To prevent returning anything that even vaguely resembles a placeholder, + * we clobber every % we can find. + */ + return str_replace( '%', $this->placeholder_escape(), $query ); + } + + /** + * Removes the placeholder escape strings from a query. + * + * @since 4.8.3 + * + * @param string $query The query from which the placeholder will be removed. + * @return string The query with the placeholder removed. + */ + public function remove_placeholder_escape( $query ) { + return str_replace( $this->placeholder_escape(), '%', $query ); + } + /** * Insert a row into a table. * @@ -2064,7 +2161,7 @@ class wpdb { $conditions = implode( ' AND ', $conditions ); $sql = "UPDATE `$table` SET $fields WHERE $conditions"; - + $this->check_current_query = false; return $this->query( $this->prepare( $sql, $values ) ); }