Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use placeholders in prepared SQL statements #367

Closed
wants to merge 2 commits into from

Conversation

frankiejarrett
Copy link
Contributor

Fixes #354

Originally, I was just going to remove the prepare statement altogether (see e4155a9).

Then I decided to just keep the prepare and enter a placeholder (see 941ea29).

Either way is justifiable I think, just a matter of preference and preparing just brings another layer of comfort for the paranoid.

@@ -136,14 +136,14 @@ public function query( $args ) {
if ( $args['record__in'] ) {
$record__in = implode( ',', array_filter( (array) $args['record__in'], 'is_numeric' ) );
if ( $record__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN ($record__in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.ID IN (%s)", $record__in );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fjarrett this won't work. It will result in something like:

$where .= " AND $wpdb->stream.ID IN (\"1,2,3,4,5,6\")";

What needs to be done instead is this:

$record__in = array_filter( (array) $args['record__in'], 'is_numeric' );
if ( ! empty( $record__in ) ) {
    $record__in_sql = join( ', ', array_map( function( $record_id ) use ( $wpdb ) {
        return $wpdb->prepare( '%d', $record_id );
    }, $record__in ) );
    $where .= " AND $wpdb->stream.ID IN ($record__in_sql)";
}

@frankiejarrett
Copy link
Contributor Author

@westonruter Oh whoops, it seems @faishal has already done this in #362. Somehow I missed that! Closing this PR...

@frankiejarrett frankiejarrett deleted the issue-354 branch March 25, 2014 18:34
@powelski
Copy link

@westonruter @fjarrett By the way, that way of forcing placeholders is not very smart: https://github.com/WordPress/WordPress/blob/master/wp-includes/wp-db.php#L1143-L1145 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants