refactor: improve SQL query preparation in get_all_for_export#59
Conversation
Modify the get_all_for_export function to always use $wpdb->prepare for SQL query preparation, improving consistency and reducing the risk of SQL injection. Previously, the function used a conditional approach where $wpdb->prepare would not be called if no $values were present. This change ensures that query preparation is uniform, which enhances security and maintains best practices in database interaction.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors get_all_for_export to always use $wpdb->prepare for building the SQL query before execution, ensuring consistent prepared-statement usage regardless of whether there are bound values. Sequence diagram for updated get_all_for_export query preparationsequenceDiagram
participant Caller
participant Sva_DB as Sva_DB
participant wpdb as wpdb
Caller->>Sva_DB: get_all_for_export(args)
Sva_DB->>Sva_DB: build $sql, $values
alt values present
Sva_DB->>wpdb: prepare(sql, values...)
wpdb-->>Sva_DB: prepared
else no values
Sva_DB->>wpdb: prepare(sql)
wpdb-->>Sva_DB: prepared
end
Sva_DB->>wpdb: get_results(prepared, ARRAY_A)
wpdb-->>Sva_DB: rows
Sva_DB-->>Caller: rows[]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Warning Review limit reached
More reviews will be available in 11 minutes and 17 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more credits in the billing tab to continue. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Calling
$wpdb->prepare( $sql )when there are no placeholders or values does not add any safety and may be misleading; consider either keeping the direct$wpdb->get_results( $sql, ARRAY_A )path or refactoring the query to actually use placeholders for all dynamic parts. - Since
$table,$orderby,$order, and$where_sqlare still interpolated directly into$sql, this refactor doesn't materially change the SQL injection surface; if the goal is improved safety, consider moving those interpolated pieces into prepared placeholders where possible.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Calling `$wpdb->prepare( $sql )` when there are no placeholders or values does not add any safety and may be misleading; consider either keeping the direct `$wpdb->get_results( $sql, ARRAY_A )` path or refactoring the query to actually use placeholders for all dynamic parts.
- Since `$table`, `$orderby`, `$order`, and `$where_sql` are still interpolated directly into `$sql`, this refactor doesn't materially change the SQL injection surface; if the goal is improved safety, consider moving those interpolated pieces into prepared placeholders where possible.
## Individual Comments
### Comment 1
<location path="includes/class-sva-db.php" line_range="396-398" />
<code_context>
- } else {
- $rows = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- }
+ $prepared = $values
+ ? $wpdb->prepare( $sql, ...$values ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ : $wpdb->prepare( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $rows = $wpdb->get_results( $prepared, ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
</code_context>
<issue_to_address>
**issue (bug_risk):** Calling `$wpdb->prepare( $sql )` with no placeholders/args can trigger `_doing_it_wrong()` and is unnecessary work.
This new `: $wpdb->prepare( $sql )` branch changes behavior from the previous version, which passed raw `$sql` when `$values` was empty. In WordPress, calling `prepare()` with only `$query` will:
- Trigger `_doing_it_wrong()` if the query contains any `%` (including in `LIKE '%foo%'` or column names), where previously there was no notice.
- Otherwise just return the query unchanged, adding overhead without added safety when there are no placeholders.
To preserve existing behavior and avoid `_doing_it_wrong()` while still centralizing `get_results()`, consider keeping `prepare()` conditional on `$values` being non-empty:
```php
$prepared = $values
? $wpdb->prepare( $sql, ...$values ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
: $sql; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
🔍 WordPress Plugin Check Report
📊 Report
|
| 📍 Line | 🔖 Check | 💬 Message |
|---|---|---|
397 |
PluginCheck.Security.DirectDB.UnescapedDBParameter | Unescaped parameter $sql used in $wpdb->get_results()\n$sql assigned unsafely at line 394. |
399 |
PluginCheck.Security.DirectDB.UnescapedDBParameter | Unescaped parameter $sql used in $wpdb->get_results()\n$sql assigned unsafely at line 394. |
🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check
Simplify the query preparation logic in the get_all_for_export function by eliminating the separate assignment of the $prepared variable. This change directly prepares the SQL query in the conditional block where $values are checked, streamlining the code and improving readability. The refactoring ensures that prepared statements are used consistently while maintaining the same functionality.
|



📑 Description
Modify the get_all_for_export function to always use $wpdb->prepare for SQL query preparation, improving consistency and reducing the risk of SQL injection. Previously, the function used a conditional approach where $wpdb->prepare would not be called if no $values were present. This change ensures that query preparation is uniform, which enhances security and maintains best practices in database interaction.
✅ Checks
☢️ Does this introduce a breaking change?
Summary by Sourcery
Enhancements: