Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/plugin-ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ jobs:
echo "Syntax errors found!"
exit 1
fi

- name: Run Plugin Regression Tests
run: |
cd ${{ github.workspace }}/cacti/plugins/syslog
if [ -d tests/regression ]; then
for test in tests/regression/*.php; do
[ -f "$test" ] || continue
php "$test"
done
fi


- name: Run Cacti Poller
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

--- develop ---

* issue#260: Replace eval-based callback execution in autocomplete handling
* issue: Making changes to support Cacti 1.3
* issue: Don't use MyISAM for non-analytical tables
* issue: The install advisor for Syslog was broken in current Cacti releases
Expand Down
21 changes: 20 additions & 1 deletion js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,25 @@ function initSyslogReports() {
* Autocomplete Form Callback Functions
* ======================================================================== */

/**
* Validate and invoke a named callback function specified as a string
* @param {string} onChange - Name of the global function to call (e.g. 'myCallback')
*/
function runSyslogAutocompleteOnChange(onChange) {
if (typeof onChange !== 'string') {
return;
}

var callbackName = onChange.trim().replace(/\(\)\s*$/, '');
if (!callbackName.match(/^[A-Za-z_$][A-Za-z0-9_$]*$/)) {
return;
}

if (typeof window[callbackName] === 'function') {
window[callbackName]();
}
}

/**
* Initialize autocomplete for form dropdown fields
* @param {string} formName - The name of the form field
Expand All @@ -591,7 +610,7 @@ function initSyslogAutocomplete(formName, callback, onChange) {
$('#' + formName).val(ui.item.value);
}
if (onChange) {
eval(onChange);
runSyslogAutocompleteOnChange(onChange);
}
}
}).css('border', 'none').css('background-color', 'transparent');
Expand Down
45 changes: 45 additions & 0 deletions tests/regression/issue260_remove_eval_callback_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

$javascript = file_get_contents(dirname(__DIR__, 2) . '/js/functions.js');

if ($javascript === false) {
fwrite(STDERR, "Failed to load js/functions.js\n");
exit(1);
}

if (strpos($javascript, 'eval(') !== false) {
fwrite(STDERR, "Unsafe eval() callback execution is still present.\n");
exit(1);
}

if (!preg_match('/function\s+runSyslogAutocompleteOnChange\s*\(\s*onChange\s*\)/', $javascript)) {
fwrite(STDERR, "Safe autocomplete callback helper is missing.\n");
exit(1);
}

if (strpos($javascript, "if (!callbackName.match(/^[A-Za-z_$][A-Za-z0-9_$]*$/))") === false) {
fwrite(STDERR, "Autocomplete callback name validation is missing.\n");
exit(1);
}

$hasCallbackTypeCheck = preg_match(
'/if\s*\(\s*typeof\s+window\s*\[\s*callbackName\s*\]\s*===\s*[\'"]{1}function[\'"]{1}\s*\)/',
$javascript
) === 1;

$hasCallbackInvocation = preg_match(
'/window\s*\[\s*callbackName\s*\]\s*\(/',
$javascript
) === 1;

if (!$hasCallbackTypeCheck || !$hasCallbackInvocation) {
fwrite(STDERR, "Expected function-reference callback execution is missing.\n");
exit(1);
}

if (strpos($javascript, 'runSyslogAutocompleteOnChange(onChange);') === false) {
fwrite(STDERR, "Autocomplete select handler is not using safe callback execution.\n");
exit(1);
}

echo "issue260_remove_eval_callback_test passed\n";