Skip to content

Commit 2f669cd

Browse files
authored
Merge pull request #161 from BeAPI/issue/72315
add prepare missing wpdb / improve sql security
2 parents 3a63ac8 + 9ab96d7 commit 2f669cd

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

classes/cli/migration.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@ private function get_blog_ids_with_meta_key() {
2727
switch_to_blog( $blog->blog_id );
2828

2929
// Table exists ?
30-
if ( $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->postmeta'" ) != $wpdb->postmeta ) {
30+
if ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->postmeta ) ) !== $wpdb->postmeta ) {
3131
restore_current_blog();
3232
continue;
3333
}
3434

35-
$selects[] = "(
36-
SELECT pm.post_id AS post_id, pm.meta_value AS meta_value, {$blog->blog_id} AS blog_id
37-
FROM {$wpdb->postmeta} AS pm
38-
WHERE 1 = 1
39-
AND pm.meta_key = '_origin_key'
40-
)";
35+
$blog_id = (int) $blog->blog_id; // Ensure the blog_id is an integer
36+
$meta_key = '_origin_key'; // Define the meta_key explicitly
37+
38+
// Use prepare to ensure safe query construction
39+
$selects[] = $wpdb->prepare( "(
40+
SELECT pm.post_id AS post_id, pm.meta_value AS meta_value, %d AS blog_id
41+
FROM {$wpdb->postmeta} AS pm
42+
WHERE pm.meta_key = %s
43+
)", $blog_id, $meta_key );
4144

4245
restore_current_blog();
4346
}
4447

45-
// Make an union, group doublons with concat
46-
$query = ' SELECT post_id, meta_value, blog_id FROM ( ';
47-
$query .= implode( ' UNION ALL ', $selects );
48-
$query .= ' ) AS wp ';
48+
$union_all_query = implode( ' UNION ALL ', $selects );
4949

50-
return $wpdb->get_results( $query );
50+
return $wpdb->get_results( "SELECT post_id, meta_value, blog_id FROM ( $union_all_query ) AS wp" );
5151
}
5252

5353
/**

classes/query.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public static function posts_join( $join, WP_Query $query ) {
4444

4545
$join_type = $query->get( 'bea_csf_filter' ) === 'local-only' ? 'LEFT' : 'INNER';
4646

47-
$join .= " $join_type JOIN $wpdb->bea_csf_relations AS bcr ON ( $wpdb->posts.ID = bcr.receiver_id AND bcr.receiver_blog_id = " . get_current_blog_id() . ' ) ';
47+
// Get current blog ID safely
48+
$current_blog_id = (int) get_current_blog_id();
49+
50+
// Prepare the join SQL
51+
$join .= $wpdb->prepare(
52+
" $join_type JOIN {$wpdb->bea_csf_relations} AS bcr ON ({$wpdb->posts}.ID = bcr.receiver_id AND bcr.receiver_blog_id = %d) ",
53+
$current_blog_id
54+
);
4855

4956
return $join;
5057
}

0 commit comments

Comments
 (0)