-
Notifications
You must be signed in to change notification settings - Fork 39
hardening: migrate monitor SQL helpers to prepared variants #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
somethingwithproof
wants to merge
3
commits into
Cacti:develop
Choose a base branch
from
somethingwithproof:fix/prepared-monitor-sql-207
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
|
|
||
| function assert_contains(string $haystack, string $needle, string $message): void { | ||
| if (strpos($haystack, $needle) === false) { | ||
| fwrite(STDERR, $message . PHP_EOL); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| function assert_regex(string $pattern, string $subject, string $message): void { | ||
| if (!preg_match($pattern, $subject)) { | ||
| fwrite(STDERR, $message . PHP_EOL); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| function assert_not_contains(string $haystack, string $needle, string $message): void { | ||
| if (strpos($haystack, $needle) !== false) { | ||
| fwrite(STDERR, $message . PHP_EOL); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| $setup = file_get_contents(__DIR__ . '/../setup.php'); | ||
| if ($setup === false) { | ||
| fwrite(STDERR, "Unable to read setup.php\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| 'db_fetch_cell_prepared(', | ||
| 'Expected plugin version lookup to use db_fetch_cell_prepared().' | ||
| ); | ||
|
|
||
| assert_regex( | ||
| "/SELECT\\s+version\\s+FROM\\s+plugin_config\\s+WHERE\\s+directory\\s*=\\s*\\?/s", | ||
| $setup, | ||
| 'Expected version lookup SQL to query plugin_config with directory placeholder.' | ||
| ); | ||
|
|
||
| assert_regex( | ||
| "/db_fetch_cell_prepared\\s*\\(.*\\[\\s*'monitor'\\s*\\]/s", | ||
| $setup, | ||
| 'Expected monitor directory value to be bound as a prepared parameter.' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| "db_execute_prepared('DROP TABLE IF EXISTS plugin_monitor_notify_history', []);", | ||
| 'Expected uninstall notify-history drop to use db_execute_prepared() with empty bindings.' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| "db_execute_prepared('DROP TABLE IF EXISTS plugin_monitor_reboot_history', []);", | ||
| 'Expected uninstall reboot-history drop to use db_execute_prepared().' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| "db_execute_prepared('DROP TABLE IF EXISTS plugin_monitor_uptime', []);", | ||
| 'Expected uninstall uptime drop to use db_execute_prepared().' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| '$placeholders = implode(\',\', array_fill(0, cacti_sizeof($devices), \'?\'));', | ||
| 'Expected monitor_device_remove() to build placeholder list for IN clause.' | ||
| ); | ||
| assert_contains( | ||
| $setup, | ||
| 'if (!cacti_sizeof($devices)) {', | ||
| 'Expected monitor_device_remove() to short-circuit empty device lists.' | ||
| ); | ||
| assert_contains( | ||
| $setup, | ||
| 'array_map(\'intval\', $devices);', | ||
| 'Expected monitor_device_remove() to normalize device ids before prepared deletes.' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| 'db_execute_prepared("DELETE FROM plugin_monitor_notify_history WHERE host_id IN($placeholders)", $devices);', | ||
| 'Expected notify history delete to use db_execute_prepared() with placeholders.' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| 'db_execute_prepared("DELETE FROM plugin_monitor_reboot_history WHERE host_id IN($placeholders)", $devices);', | ||
| 'Expected reboot history delete to use db_execute_prepared() with placeholders.' | ||
| ); | ||
|
|
||
| assert_contains( | ||
| $setup, | ||
| 'db_execute_prepared("DELETE FROM plugin_monitor_uptime WHERE host_id IN($placeholders)", $devices);', | ||
| 'Expected uptime delete to use db_execute_prepared() with placeholders.' | ||
| ); | ||
|
|
||
| assert_not_contains( | ||
| $setup, | ||
| 'db_execute(\'DELETE FROM plugin_monitor_notify_history WHERE host_id IN(\' . implode(\',\', $devices) . \')\');', | ||
| 'Raw notify-history delete should not remain.' | ||
| ); | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in |
||
| assert_not_contains( | ||
| $setup, | ||
| 'db_execute(\'DELETE FROM plugin_monitor_reboot_history WHERE host_id IN(\' . implode(\',\', $devices) . \')\');', | ||
| 'Raw reboot-history delete should not remain.' | ||
| ); | ||
|
|
||
| assert_not_contains( | ||
| $setup, | ||
| 'db_execute(\'DELETE FROM plugin_monitor_uptime WHERE host_id IN(\' . implode(\',\', $devices) . \')\');', | ||
| 'Raw uptime delete should not remain.' | ||
| ); | ||
|
|
||
| echo "OK\n"; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in
0e8344b: added a CI step inplugin-ci-workflow.ymlto executetests/test_*.phpwhen present.