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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions includes/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)";
}

}
}

if ( $args['record__not_in'] ) {
$record__not_in = implode( ',', array_filter( (array) $args['record__not_in'], 'is_numeric' ) );
if ( strlen( $record__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN ($record__not_in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.ID NOT IN (%s)", $record__not_in );
}
}

Expand All @@ -154,28 +154,28 @@ public function query( $args ) {
if ( $args['record_parent__in'] ) {
$record_parent__in = implode( ',', array_filter( (array) $args['record_parent__in'], 'is_numeric' ) );
if ( strlen( $record_parent__in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN ($record_parent__in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.parent IN (%s)", $record_parent__in );
}
}

if ( $args['record_parent__not_in'] ) {
$record_parent__not_in = implode( ',', array_filter( (array) $args['record_parent__not_in'], 'is_numeric' ) );
if ( strlen( $record_parent__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN ($record_parent__not_in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.parent NOT IN (%s)", $record_parent__not_in );
}
}

if ( $args[ 'author__in' ] ) {
$author__in = implode( ',', array_filter( (array)$args[ 'author__in' ], 'is_numeric' ) );
if ( $author__in ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN ($author__in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.author IN (%s)", $author__in );
}
}

if ( $args[ 'author__not_in' ] ) {
$author__not_in = implode( ',', array_filter( (array)$args[ 'author__not_in' ], 'is_numeric' ) );
if ( strlen( $author__not_in ) ) {
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN ($author__not_in)", '' );
$where .= $wpdb->prepare( " AND $wpdb->stream.author NOT IN (%s)", $author__not_in );
}
}
if ( $args[ 'ip__in' ] ) {
Expand Down