Repository navigation
feat: make plugin CSP compliant - #402
Conversation
668cc7b to
b5a07f8
Compare
Html::script() defaults its cache key to GLPI_VERSION, so a plugin-only upgrade served stale JS files.
jdurand-teclib
left a comment
There was a problem hiding this comment.
We should check that PluginTagDropdown actually exists to avoid some errors in console and display issues.
For example, I created a form with a Tag question and opened it as a user. The listed tags in the question are appearing without a background color, and an error is displayed in the console
The problem is that PluginTagDropdown object is loaded through ADD_JAVASCRIPT in the page footer, so it is not existing yet, when dropdown_init.js does it's work.
I made several suggestions that are fixing the issue.
| * ------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| PluginTagDropdown.initMarkers(document); |
There was a problem hiding this comment.
Suggested fix for dropdown init issue 3/3
| PluginTagDropdown.initMarkers(document); | |
| window.PluginTagDropdown?.initMarkers(document); |
| }); | ||
| } | ||
| }; | ||
|
|
There was a problem hiding this comment.
Suggested fix for dropdown init issue 2/3
| // Markers rendered inline before the footer (e.g. /Form/Render) ran dropdown_init.js before this file existed. | |
| window.PluginTagDropdown.initMarkers(document); |
| (function () { | ||
| 'use strict'; | ||
|
|
||
| // Loaded once per page through ADD_JAVASCRIPT (setup.php); dropdown_init.js calls initMarkers(). |
There was a problem hiding this comment.
Suggested fix for dropdown init issue 1/3
| // Loaded once per page through ADD_JAVASCRIPT (setup.php); dropdown_init.js calls initMarkers(). | |
| // Loaded once per page through ADD_JAVASCRIPT (setup.php), in the page footer. | |
| // dropdown_init.js handles markers inserted later (AJAX); markers already in the page are handled below. |
|
Backport-action backported this pull request in workflow run 45965908851.
|
Declare the plugin compatible with the Content Security Policy sent by GLPI.
Searches for inline event handlers,
javascript:URLs,<script>tags printed from PHP or created from JavaScript,on_changeandon*option keys and deprecated confirmation helpers returned no hit in the plugin code, so only'csp_compliant' => trueis added toplugin_version_tag().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 theadd_javascripthook, with data passed throughdata-*attributes: no<script>is written in templates, and the scripts get the nonce on CSP-enabled GLPI.Checklist before requesting a review
Description
Manual test checklist
Keep the browser console open: a CSP violation is reported as
Refused to execute inline event handlerorRefused to execute inline script.