Skip to content

fix(thold): validate a new threshold before its first save (#691) - #898

Merged
somethingwithproof merged 3 commits into
Cacti:developfrom
bmfmancini:fix/691-validate-new-threshold
Dec 3, 2026
Merged

somethingwithproof merged 3 commits into
Cacti:developfrom
bmfmancini:fix/691-validate-new-threshold

Conversation

@bmfmancini

Copy link
Copy Markdown
Member

Fixes #691.

thold_validate_save() returned true for any threshold with an empty id. A threshold created from Console > Data Sources (Template name link) was therefore saved without any checks: High Alert below Low Alert, or no alert limit at all, went straight into thold_data.

The skip was there for the non-templated wizard. Its 'Create' step posts only the device, graph and data source, saves an empty threshold, and then opens that threshold in the edit form. Validating that post fails with "Unknown Threshold Type!".

Change

  • thold_validate_save() takes a new $skip_new argument, and the empty-id skip applies only when the caller passes it.
  • save_thold() passes it only when the post has no thold_type. The edit form always posts that field, and the wizard's step never does. The one case where the form disables thold_type (Template Enabled checked) returns through the template branch before validation.
  • A failed first save used to redirect to thold.php?action=edit&id= with no id, and thold_edit() exits on that, which gives a blank page. save_thold() now returns false and sets a return URL by reference. thold.php does the redirect. The new thold_edit_url() reopens a new threshold from its data source, graph and device.
  • thold.php sends any other false from save_thold() (Permission Denied, missing graph item) to the threshold list, so the error is shown. Before, those also ended on the blank page.
  • thold_create_from_template() loses its validation call. id is always 0 there, so the old skip always applied and the header()/exit branch was dead. The values come from a template that was validated when it was saved, and the function runs from device automation and cli_thresholds.php, where a redirect is meaningless.
  • CHANGELOG entry under develop.

The values a user typed are not refilled after a rejected save. Editing an existing threshold already behaves that way. Refilling through sess_field_values could be a follow-up.

Testing

tests/Unit/TholdValidateNewThresholdTest.php (9 tests) covers:

  • a new threshold with impossible or missing limits is rejected, and a valid one is accepted;
  • $skip_new applies only to a new threshold and never to templates;
  • thold_edit_url() for a saved and an unsaved threshold;
  • save_thold() rejects an impossible new threshold without calling sql_save() and returns the data-source edit URL;
  • save_thold() still saves the wizard's post without thold_type.

Run in php:8.3-cli with pcov, Cacti vendor tree:

Check Result
New tests 9 passed
Full suite 623 passed, 1 skipped, 1 failed. The failure is NotificationQueueClaimTest::default process identity probe…, which fails the same way on unmodified develop in that container.
tests/bin/patch-coverage.php against develop 100.00% (10/10 lines)

Not exercised in a live Cacti.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

thold_validate_save() returned true for any threshold with an empty id, so a
threshold created from Console > Data Sources could be saved with a High
Alert below its Low Alert, or with no limits at all. The skip existed for
the non-templated wizard, whose 'Create' step posts only the device, graph
and data source and saves an empty threshold.

The skip is now an explicit $skip_new argument. save_thold() passes it only
for a post without the edit form's thold_type, which is the wizard's step.

A failed first save used to redirect to "edit&id=" with no id, which is a
blank page. save_thold() now returns false and sets the edit URL to return
to, and thold.php does the redirect. thold_edit_url() reopens a new
threshold from its data source, graph and device.

thold_create_from_template() loses its validation call: the old skip always
applied there (id is always 0), so the header()/exit branch was dead, and
it runs from device automation and the CLI.
Copilot AI balanced review requested due to automatic review settings December 2, 2026 22:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A missing thold_type still allows submitted threshold values to reach sql_save(), persisting invalid limits and an empty type instead of limiting the exception to the wizard’s value-free creation request.

1 open issue
What changed in this PR

Addresses #691 by validating new thresholds before saving, while preserving the wizard’s initial creation flow.

Changes:

  • Makes validation skipping explicit for new thresholds.
  • Returns failed saves to an appropriate edit or list page.
  • Adds nine regression tests and a changelog entry.
File Description
thold.php Handles save failures and return redirects.
tests/​Unit/​TholdValidateNewThresholdTest.php Tests validation, wizard saves, and edit URLs.
includes/​functions.php Updates validation and builds failure return URLs; removes redundant template validation.
CHANGELOG.md Documents the validation and redirect fixes.

Review effort: Balanced

Comment thread includes/functions.php Resolved
TheWitness and others added 2 commits December 2, 2026 23:08
…ost (Cacti#691)

Omitting thold_type alone no longer marks a new-threshold save as the
wizard's empty first save. Validation is skipped only when no threshold
type, limit, trigger or baseline value was submitted, and a missing or
empty thold_type is now rejected as an unknown type when validated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…acti#691)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

@somethingwithproof
somethingwithproof merged commit 8feb981 into Cacti:develop Dec 3, 2026
8 checks passed
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.

Creating thold from console->data sources allows create thold without validation

5 participants