Skip to content

Commit 42d2e85

Browse files
committed
Enhance SQL query handling in duplicate merging logic
- Improved the construction of SQL queries by ensuring consistent escaping of values, particularly for LIKE operations, to enhance security and prevent SQL injection vulnerabilities. - Updated the handling of dynamic post types in SQL queries for better clarity and maintainability.
1 parent bf37fd9 commit 42d2e85

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

dt-contacts/duplicates-merging.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,17 +896,18 @@ private static function query_for_duplicate_searches_v2( $post_type, $post_id, b
896896
foreach ( $search_values as $search_val ){
897897
$val = ( strpos( $search_val, '^' ) === 0 ) ? substr( $search_val, 1 ) : $search_val;
898898
$op = ( strpos( $search_val, '^' ) === 0 ) ? '=' : 'LIKE';
899-
$esc_val = esc_sql( $val );
900899
if ( $op === 'LIKE' ){
901-
$esc_val = '%' . $esc_val . '%';
900+
$esc_val = '%' . esc_sql( $wpdb->esc_like( $val ) ) . '%';
901+
} else {
902+
$esc_val = esc_sql( $val );
902903
}
903904
if ( !empty( $all_sql ) ){
904905
$all_sql .= ' UNION ';
905906
}
906907
$all_sql .= 'SELECT p.ID, p.post_title, \'post_title\' as field, p.post_title as value
907908
FROM ' . $wpdb->posts . ' p
908909
JOIN ' . $wpdb->postmeta . ' pm ON ( p.ID = pm.post_id AND pm.meta_key = \'type\' AND pm.meta_value = \'access\' )
909-
WHERE p.post_type = \'contacts\' AND p.post_title ' . $op . ' \'' . $esc_val . '\'
910+
WHERE p.post_type = \'' . esc_sql( $post_type ) . '\' AND p.post_title ' . $op . ' \'' . $esc_val . '\'
910911
AND p.ID != ' . (int) $post_id;
911912
}
912913
} else if ( $field_type === 'communication_channel' ){
@@ -931,9 +932,10 @@ private static function query_for_duplicate_searches_v2( $post_type, $post_id, b
931932
foreach ( $search_values as $search_val ){
932933
$val = ( strpos( $search_val, '^' ) === 0 ) ? substr( $search_val, 1 ) : $search_val;
933934
$op = ( strpos( $search_val, '^' ) === 0 ) ? '=' : 'LIKE';
934-
$esc_val = esc_sql( $val );
935935
if ( $op === 'LIKE' ){
936-
$esc_val = '%' . $esc_val . '%';
936+
$esc_val = '%' . esc_sql( $wpdb->esc_like( $val ) ) . '%';
937+
} else {
938+
$esc_val = esc_sql( $val );
937939
}
938940
if ( !empty( $all_sql ) ){
939941
$all_sql .= ' UNION ';

0 commit comments

Comments
 (0)