diff --git a/CHANGELOG.md b/CHANGELOG.md index a36637bf..5bdfea78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ --- 5.0 --- +* issue#458: Initialize and validate the active tab before saving view defaults, avoiding a TypeError that disables Syslog and stops processing +* issue#459: Explain that SQL Expression rule previews search only queued incoming messages, so an empty preview is not mistaken for an alert-processing failure + * issue#456: Avoid PHP 8.6 deprecation warnings during schema checks by using object IDs for the connection cache key * issue#440: Remember only a successful table creation (not updates of existing tables) when avoiding repeated schema operations, so schema drift on an existing table, such as a narrowed owner column, is still detected and repaired within the same request diff --git a/includes/functions.php b/includes/functions.php index 0e1ccd30..66532312 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -3840,6 +3840,9 @@ function syslog_rule_preview(array $rule, string $rule_type = 'alert', int $rows } $result = ['error' => '', 'count' => (int) $count, 'rows' => $out]; + if ($rule_type === 'alert' && $type === 'sql') { + $result['notice'] = __('SQL Expression previews check only queued incoming messages. Already processed messages are not searched. Testing does not send alarms.', 'syslog'); + } if ($history) { $result['history_limit'] = SYSLOG_RULE_PREVIEW_HISTORY_ROWS; } diff --git a/js/functions.js b/js/functions.js index b58aff74..13a13498 100644 --- a/js/functions.js +++ b/js/functions.js @@ -1446,6 +1446,10 @@ function testSyslogRule(formSelector, dialogSelector, title) { (result.count === 1 ? '1 matching message' : result.count + ' matching messages') ))); + if (result.notice) { + container.append($('
').text(result.notice)); + } + if (result.history_limit) { container.append($('').text('No matches in incoming data. Results are from the most recent ' + result.history_limit + ' stored messages. Testing does not send alarms.')); } diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index dbf51131..40bb1b94 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -3582,3 +3582,7 @@ msgstr "" #: syslog_saved_searches.php msgid "Choose a field, operator, and value. Use AND, OR, NOT, or Group to build the query. Changes apply to this shared template for all users." msgstr "" + +#: includes/functions.php +msgid "SQL Expression previews check only queued incoming messages. Already processed messages are not searched. Testing does not send alarms." +msgstr "" diff --git a/syslog.php b/syslog.php index a14a701b..02486528 100644 --- a/syslog.php +++ b/syslog.php @@ -69,11 +69,6 @@ exit; } -if (get_request_var('action') == 'save') { - save_settings(); - exit; -} - if (get_request_var('action') == 'saved_search_save') { header('Content-Type: application/json; charset=UTF-8'); print saved_search_save(); @@ -154,6 +149,11 @@ $_SESSION['sess_syslog_tab'] = $current_tab; } +if (get_request_var('action') == 'save') { + save_settings(); + exit; +} + // validate the syslog post/get/request information syslog_request_validation($current_tab); @@ -2824,8 +2824,6 @@ function syslog_messages(string $tab = 'syslog'): void { function save_settings(): void { global $current_tab; -// syslog_request_validation($current_tab); - $variables = [ 'rows', 'refresh', diff --git a/tests/E2E/playwright/browser-save-defaults.js b/tests/E2E/playwright/browser-save-defaults.js new file mode 100644 index 00000000..16828c35 --- /dev/null +++ b/tests/E2E/playwright/browser-save-defaults.js @@ -0,0 +1,40 @@ +// Exercise the authenticated save action that used to disable the plugin. +const { withSession, fail, url } = require('./session'); + +withSession(async (page) => { + const target = url('plugins/syslog/syslog.php'); + await page.goto(`${target}?tab=syslog&removal=-1&rows=30&refresh=60`, { waitUntil: 'networkidle' }); + await page.locator('#syslog_view_options summary').click(); + + const saved = page.waitForResponse((response) => response.url() === target && response.request().method() === 'POST'); + await page.locator('#save').click(); + const response = await saved; + + if (response.status() !== 200 || (await response.text()).trim() !== '') { + fail(`Saving view defaults failed: ${response.status()} ${await response.text()}`); + } + + // Old clients can omit the tab; the save must use the active session tab. + await page.goto(`${target}?tab=alerts`, { waitUntil: 'networkidle' }); + const fallback = await page.evaluate(async (target) => { + const response = await fetch(target, { + method: 'POST', + body: new URLSearchParams({ action: 'save', rows: '30', refresh: '60', removal: '-1', __csrf_magic: csrfMagicToken }) + }); + return { status: response.status, body: await response.text() }; + }, target); + + if (fallback.status !== 200 || fallback.body.trim() !== '') { + fail(`Saving without a tab failed: ${JSON.stringify(fallback)}`); + } + + await page.goto(`${target}?tab=syslog`, { waitUntil: 'networkidle' }); + if (await page.locator('#save').count() !== 1) { + fail('Syslog is unavailable after saving view defaults'); + } + + console.log('Save view defaults and session-tab fallback passed'); +}).catch((error) => { + console.error(error.stack || String(error)); + process.exit(1); +}); diff --git a/tests/E2E/playwright/run-e2e.sh b/tests/E2E/playwright/run-e2e.sh index 6ab535cf..7dde6a60 100755 --- a/tests/E2E/playwright/run-e2e.sh +++ b/tests/E2E/playwright/run-e2e.sh @@ -116,6 +116,12 @@ test_syslog_empty_page() { browser_check "plugins/syslog/syslog.php" "Syslog" "No Syslog Messages" "Unprocessed Messages: 0" } +test_save_view_defaults() { + node_check browser-save-defaults.js + assert_equals "$(db_scalar "SELECT value FROM settings_user WHERE user_id=1 AND name='syslog_removal'")" "-1" "main-record defaults should persist" + assert_equals "$(db_scalar "SELECT status FROM plugin_config WHERE directory='syslog'")" "1" "saving defaults must leave Syslog enabled" +} + test_alerts_page() { browser_check "plugins/syslog/syslog_alerts.php" "Console > Syslog Alerts" "No Syslog Alerts Defined" } @@ -328,6 +334,7 @@ normalize_admin run_test "1 login console" test_login_console run_test "2 syslog empty page" test_syslog_empty_page +run_test "Save view defaults" test_save_view_defaults run_test "3 alerts page" test_alerts_page run_test "4 removal page" test_removal_page run_test "5 reports page" test_reports_page diff --git a/tests/Unit/RulePreviewShapeTest.php b/tests/Unit/RulePreviewShapeTest.php index a3a4babb..a8680a42 100644 --- a/tests/Unit/RulePreviewShapeTest.php +++ b/tests/Unit/RulePreviewShapeTest.php @@ -223,4 +223,23 @@ function preview_setup(): void { // The hand written expression is embedded as-is; that is the // existing trusted-admin model for stored rules. expect(str_contains($calls[0]['sql'], "LIKE '%keepalive%'"))->toBeTrue(); -}); \ No newline at end of file +}); + +it('explains the incoming-only scope of SQL alert previews', function ($count) { + syslog_load_plugin_source('includes/functions.php'); + preview_setup(); + + $calls = []; + preview_capture($calls, ['count' => (string) $count]); + + $preview = syslog_rule_preview(['type' => 'sql', 'message' => "message LIKE '%SYS-5-CONFIG_I: Configured from%'"], 'alert'); + + expect($preview['error'])->toBe('') + ->and($preview['count'])->toBe($count) + ->and($preview['notice'])->toBe('SQL Expression previews check only queued incoming messages. Already processed messages are not searched. Testing does not send alarms.') + ->and($calls)->toHaveCount(2); + + foreach ($calls as $call) { + expect($call['sql'])->toContain('`syslogdb`.`syslog_incoming`'); + } +})->with([0, 1]);