Make WordPress Core

Changeset 41506


Ignore:
Timestamp:
09/19/2017 06:42:25 PM (8 years ago)
Author:
aaroncampbell
Message:

Database: Hardening to bring wpdb::prepare() inline with documentation.

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

Merges [41496] to 3.9 branch.

Location:
branches/3.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.9

  • branches/3.9/src/wp-includes/wp-db.php

    r41493 r41506  
    12171217        $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
    12181218        $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
     1219        $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
    12191220        array_walk( $args, array( $this, 'escape_by_ref' ) );
    12201221        return @vsprintf( $query, $args );
  • branches/3.9/tests/phpunit/tests/db.php

    r41480 r41506  
    709709        return 'fake_col_charset';
    710710    }
     711
     712    /**
     713     *
     714     */
     715    function test_prepare_with_unescaped_percents() {
     716        global $wpdb;
     717
     718        $sql = $wpdb->prepare( '%d %1$d %%% %', 1 );
     719        $this->assertEquals( '1 %1$d %% %', $sql );
     720    }
    711721}
    712722
Note: See TracChangeset for help on using the changeset viewer.