-
Notifications
You must be signed in to change notification settings - Fork 18
bugfix: trim xml import payload value before emptiness check #272
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
1
commit into
Cacti:develop
Choose a base branch
from
somethingwithproof:bug/import-text-trim-check
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.
Open
Changes from all commits
Commits
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
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
68 changes: 68 additions & 0 deletions
68
tests/regression/issue269_import_text_branch_logic_test.php
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,68 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * Regression test for issue #269 -- branch-logic invariants. | ||
| * | ||
| * These assertions verify the structural properties that make whitespace-only | ||
| * input fall through to the file-upload branch instead of the textbox branch, | ||
| * and that a non-empty payload is assigned to $xml_data without further | ||
| * modification. Pure source inspection: the functions themselves cannot be | ||
| * called in isolation because they depend on the Cacti runtime. | ||
| */ | ||
|
|
||
| $root = dirname(__DIR__, 2); | ||
| $targets = array( | ||
| 'alert_import' => $root . '/syslog_alerts.php', | ||
| 'removal_import' => $root . '/syslog_removal.php', | ||
| 'report_import' => $root . '/syslog_reports.php', | ||
| ); | ||
|
|
||
| foreach ($targets as $func => $target) { | ||
| $content = file_get_contents($target); | ||
|
|
||
| if ($content === false) { | ||
| fwrite(STDERR, "Failed to load $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| /* | ||
| * 1. The request variable must be captured into a local first. | ||
| * Whitespace-only input falls through only because trim() is applied | ||
| * to the local; if the assignment were missing the condition would | ||
| * be wrong. | ||
| */ | ||
| if (!preg_match('/\$import_text\s*=\s*get_nfilter_request_var\s*\(\s*\'import_text\'\s*\)/', $content)) { | ||
| fwrite(STDERR, "$func: \$import_text assignment via get_nfilter_request_var missing in $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| /* | ||
| * 2. The branch condition must trim the local variable, not the raw | ||
| * request call. This is what makes whitespace-only values fall | ||
| * through to the file-upload branch. | ||
| */ | ||
| if (!preg_match('/trim\s*\(\s*\$import_text\s*\)\s*!=\s*\'\'/', $content)) { | ||
| fwrite(STDERR, "$func: trim(\$import_text) != '' condition missing in $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| /* | ||
| * 3. Inside the textbox branch, $xml_data must be assigned the | ||
| * untrimmed local. A non-empty payload is preserved as-is. | ||
| */ | ||
| if (!preg_match('/\$xml_data\s*=\s*\$import_text\s*;/', $content)) { | ||
| fwrite(STDERR, "$func: \$xml_data = \$import_text assignment missing in $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| /* | ||
| * 4. The file-upload branch must still exist (elseif on $_FILES). | ||
| * Ensures the fallback path was not accidentally removed. | ||
| */ | ||
| if (!preg_match('/elseif\s*\(\s*\(\s*\$_FILES\s*\[/', $content)) { | ||
| fwrite(STDERR, "$func: \$_FILES elseif branch missing in $target\n"); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| echo "issue269_import_text_branch_logic_test passed\n"; |
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,45 @@ | ||
| <?php | ||
|
|
||
| $root = dirname(__DIR__, 2); | ||
| $targets = array( | ||
| $root . '/syslog_alerts.php', | ||
| $root . '/syslog_reports.php', | ||
| $root . '/syslog_removal.php' | ||
| ); | ||
|
|
||
| $legacy = "trim(get_nfilter_request_var('import_text') != '')"; | ||
|
|
||
| foreach ($targets as $target) { | ||
| $content = file_get_contents($target); | ||
|
|
||
| if ($content === false) { | ||
| fwrite(STDERR, "Failed to load $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| if (strpos($content, $legacy) !== false) { | ||
| fwrite(STDERR, "Legacy import_text trim/comparison bug remains in $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $fixedPattern = '/trim\s*\(\s*\$import_text\s*\)\s*!=\s*\'\'/'; | ||
| if (!preg_match($fixedPattern, $content)) { | ||
| fwrite(STDERR, "Fixed import_text trim/comparison check missing in $target\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| /* After the local $import_text assignment, there must be no second | ||
| get_nfilter_request_var('import_text') call. A duplicate call | ||
| would bypass the cached local variable. */ | ||
| $needle = "\$import_text = get_nfilter_request_var('import_text')"; | ||
| $assignPos = strpos($content, $needle); | ||
| if ($assignPos !== false) { | ||
| $afterAssign = substr($content, $assignPos + strlen($needle)); | ||
| if (preg_match('/get_nfilter_request_var\s*\(\s*\'import_text\'\s*\)/', $afterAssign)) { | ||
| fwrite(STDERR, "Redundant get_nfilter_request_var('import_text') call after local assignment in $target\n"); | ||
| exit(1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| echo "issue269_import_text_trim_check_test passed\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.
This regression test hard-codes exact source-code substrings for the import check, so harmless formatting/refactors (spacing, different but equivalent condition, storing into a variable first) will fail the test even if the behavior is correct. Consider switching to a more flexible pattern match (e.g., regex tolerant of whitespace/parentheses) or a behavioral test that exercises the import handlers with whitespace-only input.
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.
Fixed -- switched to preg_match() for the fixed-pattern check.