Skip to content

hardening: migrate cycle SQL helpers to prepared variants#26

Open
somethingwithproof wants to merge 3 commits intoCacti:developfrom
somethingwithproof:fix/prepared-sql-25
Open

hardening: migrate cycle SQL helpers to prepared variants#26
somethingwithproof wants to merge 3 commits intoCacti:developfrom
somethingwithproof:fix/prepared-sql-25

Conversation

@somethingwithproof
Copy link

Summary

  • migrate selected plugin_cycle SQL helper calls to prepared variants
  • convert default tree lookups in cycle.php to prepared fetches
  • convert setup-time plugin/user realm lookups and realm mutation queries in setup.php to prepared helpers
  • keep plugin behavior unchanged while removing SQL value interpolation in upgrade migration paths

Tests

  • php -l cycle.php
  • php -l setup.php
  • php -l tests/test_prepared_statements.php
  • php tests/test_prepared_statements.php

Closes #25

Copilot AI review requested due to automatic review settings March 15, 2026 22:59
Copy link

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 hardens the cycle plugin’s DB usage by migrating several SQL helper calls to prepared-statement variants and adds a small regression script to ensure the migration sticks.

Changes:

  • Replace raw db_fetch_*/db_execute calls with prepared variants in setup.php upgrade paths.
  • Convert default graph_tree lookups in cycle.php to db_fetch_cell_prepared.
  • Add tests/test_prepared_statements.php to assert prepared-helper usage via simple source-code checks.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
cycle.php Switch default tree lookups to db_fetch_cell_prepared.
setup.php Convert plugin/realm/user-realm queries to prepared helpers during upgrade/migration.
tests/test_prepared_statements.php Add regression checks to detect reintroduction of raw DB helpers in key sites.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

setup.php Outdated
Comment on lines +94 to +101
db_execute_prepared('INSERT INTO user_auth_realm
(realm_id, user_id) VALUES (?, ?)
ON DUPLICATE KEY UPDATE realm_id=VALUES(realm_id)',
array($user, $u['user_id']));
db_execute_prepared('DELETE FROM user_auth_realm
WHERE user_id = ?
AND realm_id = ?',
array($u['user_id'], $user));
Copy link
Author

Choose a reason for hiding this comment

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

Addressed in ecb390d: delete now targets the legacy realm id (42) after insert/update into the new realm, preserving intended migration behavior.


$cycle_contents = file_get_contents($cycle_file);
$setup_contents = file_get_contents($setup_file);

Copy link
Author

Choose a reason for hiding this comment

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

Addressed in ecb390d: added explicit readability checks and safe fallbacks before regex assertions.

);
assert_true(
'cycle.php no longer uses raw tree-id db_fetch_cell lookup',
strpos($cycle_contents, "db_fetch_cell('SELECT id FROM graph_tree ORDER BY name LIMIT 1')") === false
Copy link
Author

Choose a reason for hiding this comment

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

Addressed in ecb390d: replaced the exact raw-tree string check with a regex pattern that tolerates formatting differences.

@somethingwithproof somethingwithproof changed the title security: migrate cycle SQL helpers to prepared variants hardening: migrate cycle SQL helpers to prepared variants Mar 15, 2026
@somethingwithproof
Copy link
Author

somethingwithproof commented Mar 16, 2026

Incorporated follow-up feedback in 564c686. Added follow-up parity checks to lock legacy realm migration semantics (legacy id 42 migration and prepared delete/update flow).

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.

hardening: migrate cycle SQL helpers to prepared variants

2 participants