Skip to content

fix(sql): migrate interpolated queries to prepared statements#210

Closed
somethingwithproof wants to merge 1 commit intoCacti:developfrom
somethingwithproof:fix/prepared-statements
Closed

fix(sql): migrate interpolated queries to prepared statements#210
somethingwithproof wants to merge 1 commit intoCacti:developfrom
somethingwithproof:fix/prepared-statements

Conversation

@somethingwithproof
Copy link
Copy Markdown

Migrate remaining non-prepared SQL queries that interpolate PHP variables to use prepared statement variants (db_execute_prepared, db_fetch_cell_prepared, etc.). Static SQL without variable interpolation is left as-is.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Copilot AI review requested due to automatic review settings April 9, 2026 04:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the plugin’s SQL-hardening effort by migrating remaining variable-interpolated DELETE queries to prepared-statement variants, reducing SQL injection risk in device-removal cleanup code.

Changes:

  • Replaced interpolated DELETE ... IN(...) queries with db_execute_prepared(...) calls.
  • Added dynamic placeholder generation (?, ?, ...) and sanitized device IDs via intval before binding.

Comment on lines +322 to +324
db_execute_prepared('DELETE FROM plugin_monitor_notify_history WHERE host_id IN(' . implode(',', array_fill(0, cacti_count($devices), '?')) . ')', array_values(array_map('intval', $devices)));
db_execute_prepared('DELETE FROM plugin_monitor_reboot_history WHERE host_id IN(' . implode(',', array_fill(0, cacti_count($devices), '?')) . ')', array_values(array_map('intval', $devices)));
db_execute_prepared('DELETE FROM plugin_monitor_uptime WHERE host_id IN(' . implode(',', array_fill(0, cacti_count($devices), '?')) . ')', array_values(array_map('intval', $devices)));
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three DELETEs duplicate the same placeholder and parameter list construction. Consider computing $deviceIds once (sanitized/reindexed) and deriving the IN placeholder string from that array, then reuse for all three queries; it reduces repetition and avoids any risk of placeholder/param count drift if the sanitization logic changes.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid DRY opportunity. Will extract to a helper in a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants