Skip to content

security: move inline event handlers into jQuery ready handlers (CSP) - #394

Merged
TheWitness merged 1 commit into
developfrom
fix/csp-nonce-inline-handlers
Oct 16, 2026
Merged

TheWitness merged 1 commit into
developfrom
fix/csp-nonce-inline-handlers

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Summary

Moves every inline event-handler attribute in the plugin into $(function() {}) handlers so pages work under a Content-Security-Policy that blocks inline handlers.

Changes

  • Filter selects / checkbox (onChange='applyFilter()', onClick='applyFilter()') on the devices, device types, sites, MAC auth/watch, vendor MACs, SNMP options, ARP, devices, 802.1x, interfaces, IPs and MACs views, plus the shared sites filter in lib/mactrack_functions.php: handlers are now bound in each page's existing $(function() {}) block.
  • Return / Cancel buttons (onClick='cactiReturnTo()', goTo(...)): now use a mactrackReturn class handled by a delegated handler in js/mactrack.js (the SNMP page binds its own in its inline script).
  • Device rescan icon (onClick='scan_device(id)'): now class='mactrackRescanDevice' data-device-id, handled by a delegated handler in js/mactrack.js, so rows re-rendered by the enable/disable AJAX calls keep working.
  • Utilities refresh controls: the select's onChange and the Refresh button's onClick are now bound in the page script. The select previously also reloaded on click (opening the dropdown); it now reloads on change only.
  • Nonce: every inline <script> already used plugin_mactrack_csp_nonce(); none were missing it.
  • Fixed a stray '> rendered after the "Return" label in mactrack_devices.php.
  • Added tests/Security/InlineHandlersTest.php to guard against inline handler attributes / javascript: URLs and un-nonced <script> tags.
  • CHANGELOG updated.

Backport safety

  • plugin_mactrack_csp_nonce() returns '' on Cacti releases without CactiSecureHeaders.
  • Only jQuery APIs available in Cacti 1.2.x are used (.on(), .attr(), delegated $(document).on()).
  • No new PHP syntax or Cacti APIs.

Validation

  • php -l on all changed PHP files.
  • The regression test's scan was run standalone: 56 first-party PHP/JS files, no offenders.
  • The Pest suite was not run locally because the local Cacti vendor tree is incomplete; CI will run it.
  • No browser smoke test of the filters or buttons.

Replace every inline onClick/onChange attribute (filter selects, Return/Cancel
buttons, device rescan icon, utilities refresh controls) with handlers bound
inside $(function() {}) blocks. Markup rendered by shared or AJAX-refreshed
code uses delegated handlers in js/mactrack.js. All inline <script> tags
already carry plugin_mactrack_csp_nonce(), which falls back to an empty string
on Cacti releases without CactiSecureHeaders, so the change is backport safe.

Also drops a stray quote/bracket from the Return button in mactrack_devices.php
and adds a regression test guarding against inline handlers and un-nonced
script tags.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 16, 2026 15:21

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.

🔵 Needs a closer look

CSP-enforced controls and AJAX navigation still need live Cacti browser validation, and the full test suite remains unverified.

0 open findings

What changed in this PR

Updates Device Tracking controls to support Content Security Policy (CSP) restrictions on inline event handlers.

Changes:

  • Moves filter and button actions into jQuery handlers, with delegated handling for shared and AJAX-refreshed controls.
  • Corrects utilities refresh behavior and stray Return-button text.
  • Adds CSP regression checks and documents the changes.
File Description
tests/​Security/​InlineHandlersTest.php Checks inline handlers, JavaScript URLs, and script nonces.
mactrack_view_macs.php Migrates MAC filters and return buttons.
mactrack_view_ips.php Migrates IP filters.
mactrack_view_interfaces.php Migrates interface filters and totals checkbox.
mactrack_view_dot1x.php Migrates session filters.
mactrack_view_devices.php Migrates device-view filters.
mactrack_view_arp.php Migrates ARP filters.
mactrack_vendormacs.php Migrates row-count filtering.
mactrack_utilities.php Binds refresh selection and button separately.
mactrack_snmp.php Migrates return buttons and row-count filtering.
mactrack_sites.php Migrates return buttons.
mactrack_macwatch.php Migrates filters and return buttons.
mactrack_macauth.php Migrates filters and return buttons.
mactrack_devices.php Migrates controls and corrects Return text.
mactrack_device_types.php Migrates filters and return buttons.
lib/​mactrack_functions.php Updates shared filters and rescan markup.
js/​mactrack.js Adds delegated rescan and return handlers.
CHANGELOG.md Documents CSP compatibility changes.

🧠 Review effort: Balanced


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

@TheWitness
TheWitness merged commit 0448446 into develop Oct 16, 2026
3 of 6 checks passed
@TheWitness
TheWitness deleted the fix/csp-nonce-inline-handlers branch October 16, 2026 15:28
TheWitness added a commit that referenced this pull request Oct 16, 2026
The coverage gate for #394 reported 0/5 changed lines in
lib/mactrack_functions.php: the rescan icon in mactrack_format_device_row()
and the filter selects in mactrack_site_filter(). Add unit tests that render
both and assert the class/data-attribute hooks, the absence of inline
onClick/onChange attributes, and the $(function() {}) bindings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

3 participants