Skip to content

Commit dc9c161

Browse files
Fix SQL injection vulnerability in posts_clauses method (#7)
* Fix SQL injection vulnerability in posts_clauses method - Sanitize post_parent parameter using absint() before use in SQL - Use $wpdb->prepare() with %d placeholder for post_parent value - Fix logic for unattached attachments (string '0' from request now works) - Harden taxonomy join by using sanitize_key() and $wpdb->prepare() Security: Prevents time-based SQL injection via query[post_parent] parameter that was exploitable by authenticated users (Author role and above). Co-Authored-By: yoren@shikadigital.co.jp <yorenchang@gmail.com> * Use $wpdb->prepare() for post_parent = 0 case for consistency Address review feedback: use prepared statement for the hardcoded 0 value to maintain consistency with the rest of the codebase. Co-Authored-By: yoren@shikadigital.co.jp <yorenchang@gmail.com> * Bump version to 0.9.2 and update changelog - Security enhancements Co-Authored-By: yoren@shikadigital.co.jp <yorenchang@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: yoren@shikadigital.co.jp <yorenchang@gmail.com>
1 parent 5d68e97 commit dc9c161

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

README.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Donate link: https://1fix.io/
55
Tags: media library, media, attachment
66
Requires at least: 3.5
77
Tested up to: 6.8.3
8-
Stable tag: 0.9.1
8+
Stable tag: 0.9.2
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -65,6 +65,9 @@ Please add the following code to the `functions.php` in your theme:
6565

6666
== Changelog ==
6767

68+
= 0.9.2 =
69+
* Security enhancements.
70+
6871
= 0.9.1 =
6972
* Fix: Prevent "Not unique table/alias: wp_postmeta" SQL error by aliasing the postmeta JOIN. Props [@mikemeinz](https://wordpress.org/support/users/mikemeinz/). See https://wordpress.org/support/topic/sql-syntax-error-26/
7073

media-search-enhanced.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Plugin Name: Media Search Enhanced
1515
* Plugin URI: https://1fix.io/media-search-enhanced
1616
* Description: Search through all fields in Media Library.
17-
* Version: 0.9.1
17+
* Version: 0.9.2
1818
* Author: 1fixdotio
1919
* Author URI: https://1fix.io
2020
* Text Domain: media-search-enhanced

public/class-media-search-enhanced.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Media_Search_Enhanced {
2828
*
2929
* @var string
3030
*/
31-
const VERSION = '0.9.1';
31+
const VERSION = '0.9.2';
3232

3333
/**
3434
*
@@ -153,11 +153,14 @@ public static function posts_clauses( $pieces ) {
153153
$pieces['where'] .= $wpdb->prepare( " AND t.element_type='post_attachment' AND t.language_code = %s", $lang );
154154
}
155155

156-
if ( ! empty( $vars['post_parent'] ) ) {
157-
$pieces['where'] .= " AND $wpdb->posts.post_parent = " . $vars['post_parent'];
158-
} elseif ( isset( $vars['post_parent'] ) && 0 === $vars['post_parent'] ) {
159-
// Get unattached attachments
160-
$pieces['where'] .= " AND $wpdb->posts.post_parent = 0";
156+
if ( isset( $vars['post_parent'] ) ) {
157+
$post_parent = absint( $vars['post_parent'] );
158+
if ( $post_parent > 0 ) {
159+
$pieces['where'] .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d", $post_parent );
160+
} elseif ( 0 === $post_parent ) {
161+
// Get unattached attachments
162+
$pieces['where'] .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d", 0 );
163+
}
161164
}
162165

163166
if ( ! empty( $vars['post_mime_type'] ) ) {
@@ -207,7 +210,8 @@ public static function posts_clauses( $pieces ) {
207210
if ( ! empty( $taxes ) ) {
208211
$on = array();
209212
foreach ( $taxes as $tax ) {
210-
$on[] = "ttax.taxonomy = '$tax'";
213+
$tax = sanitize_key( $tax );
214+
$on[] = $wpdb->prepare( "ttax.taxonomy = %s", $tax );
211215
}
212216
$on = '( ' . implode( ' OR ', $on ) . ' )';
213217

0 commit comments

Comments
 (0)