Skip to content

security: move inline event handlers to CSP-safe bindings - #152

Merged
TheWitness merged 4 commits into
developfrom
fix/csp-inline-handlers
Oct 8, 2026
Merged

TheWitness merged 4 commits into
developfrom
fix/csp-inline-handlers

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Summary

Removes the plugin's remaining inline event-handler attributes so pages comply with Cacti's Content-Security-Policy script-src-attr directive. Part of the fleet-wide CSP inline-handler cleanup.

Changes

  • Cancel / Return buttons (reportit.php, templates.php) — switch from onClick='cactiReturnTo(...)' to the CSP-safe cactiReturnTo class, with data-url carrying the return target when one was passed (including the concatenated templates.php?tab=variables&id=... targets).
  • Filter "rows" selects (reportit.php, templates.php, view.php) — drop inline onChange='applyFilter()' and bind via change() in each page's ready block. view.php already bound #rows; reportit/templates now do too.
  • Template calc-syntax links (html_calc_syntax in lib/funct_html.php) — drop inline onClick=add_to_calc(...) for a reportitAddCalc class + data-calc-name attribute, bound by a delegated handler in templates.php (where add_to_calc is defined).

Gate / tests

lib/funct_html.php is a measured source file, so a new tests/Unit/ReportitCalcSyntaxTest.php exercises html_calc_syntax() (seeding $calc_var_names) to keep the changed line covered. The three web-UI entry points (reportit.php, templates.php, view.php) are added to the patch-coverage allowlist. No __()/__esc() calls changed, so the check-i18n-pot.php gate passes without regenerating cacti.pot.

Compatibility

Cacti 1.2.31+ binds the cactiReturnTo class automatically. The README documents a one-time applySkin() snippet for earlier releases. No shim is baked into core or the plugin.

Cacti's Content-Security-Policy script-src-attr directive blocks inline
event-handler attributes. This converts the plugin's remaining inline
handlers:

- Confirmation-page Cancel/Return buttons (reportit.php, templates.php) switch
  from onClick='cactiReturnTo(...)' to the CSP-safe cactiReturnTo class, with a
  data-url carrying the return target where one was passed.
- The filter "rows" selects (reportit, templates, view) drop their inline
  onChange='applyFilter()' and are bound via change() in each page's ready
  block (view.php already bound #rows; reportit/templates now do too).
- The template calc-syntax links (html_calc_syntax in lib/funct_html.php) drop
  their inline onClick=add_to_calc(...) for a reportitAddCalc class + data-
  calc-name attribute, bound by a delegated handler in templates.php.

lib/funct_html.php is measured; a new ReportitCalcSyntaxTest exercises
html_calc_syntax() so the changed line stays covered. The three web-UI entry
points are added to the patch-coverage allowlist. No i18n calls changed, so
cacti.pot is untouched. Cacti 1.2.31+ binds the cactiReturnTo class
automatically; the README documents a one-time applySkin() snippet for earlier
releases.

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

Unresolved event-binding and test-initialization defects block approval.

3 open findings
What changed in this PR

Updates ReportIt’s UI event handling to comply with Cacti’s Content Security Policy.

Changes:

  • Replaces inline Cancel, filter, and calculation-link handlers with JavaScript bindings.
  • Adds calculation-markup coverage and exempts UI entry points from patch coverage.
  • Documents compatibility requirements and the security changes.
File Description
view.php Removes the inline rows-change handler.
tests/​Unit/​ReportitCalcSyntaxTest.php Adds CSP-safe calculation-markup assertions.
tests/​bin/​patch-coverage.php Allowlists three UI entry points.
templates.php Updates Cancel buttons, filters, and calculation-link bindings.
reportit.php Updates Cancel buttons and filter bindings.
README.md Documents the Cancel-button compatibility workaround.
lib/​funct_html.php Emits calculation-link classes and data attributes.
CHANGELOG.md Records the CSP cleanup.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread templates.php Outdated
Comment thread tests/Unit/ReportitCalcSyntaxTest.php
Comment thread README.md Outdated
setup.php only defines REPORTIT_BASE_PATH at runtime inside reportit_define_constants() (realm/DB work), not on bare require, so the test errored with 'Undefined constant REPORTIT_BASE_PATH' before any assertion ran. Define the same value (dirname(__DIR__, 2) == CACTI_PATH_BASE/plugins/reportit) as a fallback, matching how PreparedStatementConsistencyTest resolves paths.
Address Copilot review on PR #152:

- templates.php: the metric-editor ready block re-runs on every AJAX page load (applySkin) and binds to the persistent document, so the delegated reportitAddCalc click handler accumulated and appended a token multiple times per click after reopening the editor. Namespace the handler and off() it before on().

- README.md: the .cactiReturnTo binding lives in Cacti 1.2.x include/layout.js but the refactored 1.3.0/develop layout.js dropped it, so framing the workaround as 'below 1.2.31' left develop/1.3 users with inactive Cancel buttons. Describe the workaround by binding availability instead.

@bmfmancini bmfmancini left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed; cactiReturnTo conversion uses data-url plus the class correctly, Continue buttons still submit. Checks pass.

@TheWitness
TheWitness merged commit e3762ce into develop Oct 8, 2026
3 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.

4 participants