Skip to content

feat: make plugin CSP compliant - #1350

Merged
Rom1-B merged 2 commits into
11.0/bugfixesfrom
csp-compliance
Nov 27, 2026
Merged

Rom1-B merged 2 commits into
11.0/bugfixesfrom
csp-compliance

Conversation

@Rom1-B

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

Copy link
Copy Markdown
Contributor

Summary

The plugin no longer relies on inline event handlers, javascript: URLs or <script> tags printed from PHP, so it keeps working under the Content Security Policy sent by GLPI.
Behaviour is unchanged: the same functions are called, the listeners are attached from <script> blocks printed right after the markup.
The plugin declares csp_compliant in plugin_version_fields().

Converted patterns

  • inc/container.class.php (showFormSubtype()): two <script> tags printed from PHP are now built with Html::scriptBlock().
  • inc/field.class.php (showForm() field list): the "Add a new field" link used href="javascript:viewAddField...()"; it is now href="#" with a click listener (preventDefault()) attached in the script block that defines viewAddField....
  • inc/labeltranslation.class.php (showTranslations()):
    • the "Add a new translation" link used href="javascript:addTranslation...()"; it is now href="#" with a click listener attached in the same script block.
    • the translation rows used an onClick attribute; the row now has an id and the listener is attached in the per-row script block.
  • setup.php: 'csp_compliant' => true added to plugin_version_fields().
  • CHANGELOG.md: entry under Unreleased.

Not changed: literal <script> blocks in the Twig templates (templates/forms/status_override.html.twig, templates/forms/status_overrides.html.twig, templates/container_display_conditions.html.twig, templates/question_type_administration.html.twig), which receive the nonce at compile time.

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 an item with a container displayed in a tab block, then click "Add a new field": the field form loads under the link and no console error appears.
  2. On an item with a label translation block, click "Add a new translation": the translation form loads in the block.
  3. In the same block, click a translation row: the edit form loads. Clicking the row checkbox still selects it and also opens the edit form, as before.
  4. Open the container form with a "domtab" type and pick a subtype: the subtype dropdown shows and hides the tab selection as before.

@Rom1-B
Rom1-B marked this pull request as ready for review November 19, 2026 10:06
@Rom1-B
Rom1-B requested review from a team, Mary-Clb, MyvTsv and RomainLvr and removed request for a team November 19, 2026 10:06
@Rom1-B
Rom1-B merged commit 8a6ae60 into 11.0/bugfixes Nov 27, 2026
7 checks passed
@Rom1-B
Rom1-B deleted the csp-compliance branch November 27, 2026 14:59
@glpi-project

glpi-project Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Backport-action backported this pull request in workflow run 46020875092.

Target Status
main ⚠️ Drafted with conflicts #1378

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.

2 participants