Skip to content

Commit 90e4d32

Browse files
committed
# fix: Prevent SQL injection in whereIn clause
## Description Previously, the `whereIn` method in the `Select` statement directly interpolated values into the SQL query string by wrapping them in single quotes. This approach is highly vulnerable to SQL injection, as malicious input in the `$value` array could alter the query's intent and potentially lead to unauthorized data access or manipulation. This commit addresses this critical security vulnerability by switching to a prepared statement approach for the `WHERE IN` clause. Instead of direct string concatenation, values are now added to the internal `$this->params` array, and database-specific placeholders are generated using `Utilities::get_placeholder()`. This ensures that all values are properly escaped and bound by the underlying database driver, effectively preventing SQL injection attacks. ## Changes in the codebase - **`src/Statement/Select.php`**: - In the `whereIn` method, the anonymous function responsible for formatting the list of items for the `IN` clause has been modified. - The line `return "'" . $item . "'";` has been replaced. - Each `$item` from the input `$value` array is now first added to the `$this->params[]` array, making it available for parameter binding. - `Utilities::get_placeholder($this->db, $item)` is now used to generate a database-agnostic placeholder for each item, ensuring safe parameterization regardless of the database system.
1 parent c788ad7 commit 90e4d32

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "codesvault/howdy-qb",
33
"description": "Mysql Query Builder for WordPress",
4-
"version": "1.7.0",
4+
"version": "1.7.1",
55
"minimum-stability": "stable",
66
"scripts": {
77
"test": "phpunit",

src/Statement/Select.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public function andNot(string $column, ?string $operator = null, $value = null):
115115
public function whereIn(string $column, ...$value): self
116116
{
117117
$list = implode(', ', array_map(function($item) {
118-
return "'" . $item . "'";
118+
$this->params[] = $item;
119+
return Utilities::get_placeholder($this->db, $item);
119120
}, $value));
120121

121122
$this->sql['whereIn'][] = 'WHERE ' . $column . " IN ($list)";

0 commit comments

Comments
 (0)