Skip to content

feat: make plugin CSP compliant - #393

Merged
Rom1-B merged 3 commits into
support/1.3.0from
csp-compliance
Nov 27, 2026
Merged

Rom1-B merged 3 commits into
support/1.3.0from
csp-compliance

Conversation

@Rom1-B

@Rom1-B Rom1-B commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Make the plugin compatible with the GLPI Content Security Policy by removing inline event handlers and javascript: links.
Behaviour is unchanged: same URLs, same request fields, same confirmation message.

Converted patterns

  • src/Source_Zone.php: the "Download enabled" toggle was a javascript:void(0) link with an onclick (categories a and c). It is now a <a href="#" role="button"> button with a data-carbon-toggle-zone attribute, and the two existing Html::scriptBlock() scripts bind a click listener to these buttons.
  • src/Config.php: Html::getConfirmationOnActionScript() (deprecated, category b) is removed. The Twig variables now carry the confirmation message, the action URL and the JSON fields.
  • templates/config.html.twig: the two "Reset impacts" buttons use data-carbon-confirm, data-carbon-submit-get-link and data-carbon-submit-get-link-fields instead of onclick (category a). The unused {% set reset_args %} lines are removed, as they would shadow the context variable.
  • templates/environmentalimpact-item.html.twig: the four submitGetLink() onclick handlers (usage and embodied "Reset data" and "Calculate data") use data-carbon-submit-get-link and data-carbon-submit-get-link-fields, with the fields built by Twig json_encode (category a).
  • setup.php: 'csp_compliant' => true added to plugin_version_carbon().

Other hits from the searches are not handlers: the <script> tags in templates/**/*.twig are literal template text (nonce added by the template engine), and the Html::scriptBlock() calls already emit a nonce.

Compatibility

The change works on GLPI releases with and without the Content Security Policy (glpi-project/glpi#26020 and its follow-up PRs): it relies on no CSP-only core feature, so the minimum GLPI version is unchanged.

Inline scripts are moved to JS files loaded with Html::script() or the add_javascript hook, with data passed through data-* attributes: no <script> is written in templates, and the scripts get the nonce on CSP-enabled GLPI.

Manual test checklist

Keep the browser console open. A CSP violation shows as Refused to execute inline event handler or Refused to execute inline script.

  1. Open a Carbon source item, go to the zones tab, click Yes/No in the "Download enabled" column. The value toggles and the tab reloads with no console error. Repeat on a Carbon zone item, sources tab.
  2. Open the Carbon plugin configuration page. Click "Reset usage impacts": a confirmation dialog appears. Cancel does nothing. Confirm resets the usage impacts. Repeat with "Reset embodied impacts".
  3. Open the environmental impact tab of a computer, a monitor and a network equipment. "Calculate data" runs the calculation and "Reset data" (when shown) resets the data, as before.

@Rom1-B
Rom1-B marked this pull request as ready for review November 19, 2026 10:04
@Rom1-B
Rom1-B requested review from a team, Lainow, Mary-Clb and MyvTsv and removed request for a team November 19, 2026 10:04
@btry

btry commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

This PR needs to be ported to 12.0/bugfixes (versions 1.4.x)

@Rom1-B
Rom1-B requested a review from Lainow November 24, 2026 08:53
Html::script() defaults its cache key to GLPI_VERSION, so a plugin-only upgrade served stale JS files.
@Rom1-B
Rom1-B merged commit dad58e4 into support/1.3.0 Nov 27, 2026
8 checks passed
@Rom1-B
Rom1-B deleted the csp-compliance branch November 27, 2026 11:18
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