Skip to content

[main] feat: make plugin CSP compliant - #1378

Draft
glpi-project[bot] wants to merge 1 commit into
mainfrom
forward-port/1350-to-main
Draft

glpi-project[bot] wants to merge 1 commit into
mainfrom
forward-port/1350-to-main

Conversation

@glpi-project

@glpi-project glpi-project Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Forward-port of #1350 to main.

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.

@glpi-project

glpi-project Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin forward-port/1350-to-main
git worktree add --checkout .worktree/forward-port/1350-to-main forward-port/1350-to-main
cd .worktree/forward-port/1350-to-main
git reset --hard HEAD^
git cherry-pick -x 8a6ae60258d6210f0c1c3b141a8b41c13da8011f

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.

1 participant