Skip to content

Commit

Permalink
Database: Hardening to bring wpdb::prepare() inline with documentat…
Browse files Browse the repository at this point in the history
…ion.

`wpdb::prepare()` supports %s, %d, and %F as placeholders in the query string. Any other non-escaped % will be escaped.

Merges [41496] to 4.1 branch.


Built from https://develop.svn.wordpress.org/branches/4.1@41504


git-svn-id: http://core.svn.wordpress.org/branches/4.1@41337 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
aaroncampbell committed Sep 19, 2017
1 parent 68b9288 commit 8933324
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion wp-includes/wp-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ public function prepare( $query, $args ) {
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
$query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
$query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
array_walk( $args, array( $this, 'escape_by_ref' ) );
return @vsprintf( $query, $args );
}
Expand Down Expand Up @@ -2672,7 +2673,8 @@ protected function strip_invalid_text( $data ) {
}

if ( is_array( $value['length'] ) ) {
$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] );
$length = sprintf( '%.0f', $value['length']['length'] );
$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING {$this->charset} )", $value['value'] );
} else if ( 'binary' !== $charset ) {
// If we don't have a length, there's no need to convert binary - it will always return the same result.
$queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING {$this->charset} )", $value['value'] );
Expand Down

0 comments on commit 8933324

Please sign in to comment.