Make WordPress Core

Changeset 41505


Ignore:
Timestamp:
09/19/2017 06:41:11 PM (9 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 4.0 branch.

Location:
branches/4.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.0

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

    r41492 r41505  
    12241224                $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
    12251225                $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
     1226                $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
    12261227                array_walk( $args, array( $this, 'escape_by_ref' ) );
    12271228                return @vsprintf( $query, $args );
  • branches/4.0/tests/phpunit/tests/db.php

    r41479 r41505  
    808808                return 'fake_col_charset';
    809809        }
     810
     811        /**
     812         *
     813         */
     814        function test_prepare_with_unescaped_percents() {
     815                global $wpdb;
     816
     817                $sql = $wpdb->prepare( '%d %1$d %%% %', 1 );
     818                $this->assertEquals( '1 %1$d %% %', $sql );
     819        }
    810820}
    811821
Note: See TracChangeset for help on using the changeset viewer.