Make WordPress Core

Changeset 41496 for trunk


Ignore:
Timestamp:
09/19/2017 05:55:33 PM (7 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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r41483 r41496  
    12531253        $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
    12541254        $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
     1255        $query = preg_replace( '/%(?:%|$|([^dsF]))/', '%%\\1', $query ); // escape any unescaped percents
    12551256        array_walk( $args, array( $this, 'escape_by_ref' ) );
    12561257        return @vsprintf( $query, $args );
     
    28222823
    28232824                    if ( is_array( $value['length'] ) ) {
    2824                         $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING $connection_charset )", $value['value'], $value['length']['length'] );
     2825                        $length = sprintf( '%.0f', $value['length']['length'] );
     2826                        $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), $length ) USING $connection_charset )", $value['value'] );
    28252827                    } else if ( 'binary' !== $charset ) {
    28262828                        // If we don't have a length, there's no need to convert binary - it will always return the same result.
  • trunk/tests/phpunit/tests/db.php

    r41470 r41496  
    274274    }
    275275
     276
    276277    /**
    277278     * Test that SQL modes are set correctly
     
    11161117        $this->assertSame( 'utf8_general_ci', $result['collate'] );
    11171118    }
     1119
     1120    /**
     1121     *
     1122     */
     1123    function test_prepare_with_unescaped_percents() {
     1124        global $wpdb;
     1125
     1126        $sql = $wpdb->prepare( '%d %1$d %%% %', 1 );
     1127        $this->assertEquals( '1 %1$d %% %', $sql );
     1128    }
    11181129}
Note: See TracChangeset for help on using the changeset viewer.