Skip to content

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

Merged
Rom1-B merged 1 commit into
mainfrom
forward-port/279-to-main
Dec 1, 2026
Merged

Rom1-B merged 1 commit into
mainfrom
forward-port/279-to-main

Conversation

@glpi-project

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

Copy link
Copy Markdown
Contributor

Forward-port of #279 to main.

Checklist before requesting a review

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

Summary: the plugin no longer relies on inline event handlers, so it keeps working once GLPI sends the Content Security Policy (script-src 'nonce-<value>' 'unsafe-eval').
The behaviour is unchanged: the same JavaScript functions are called with the same arguments.

Converted patterns:

  • templates/tickets/consume.html.twig
    • onchange on the "Consume a voucher ?" slider (additional_attributes): removed, bound with addEventListener('change') in the template's own <script> block, calling showCreditDiv<rand>(this.value).
    • on_change on the voucher dropdown: removed, bound with jQuery .on('change') on #dropdown_plugin_credit_entities_id<rand> (select2 fires jQuery events), calling getQuantityRemaining<rand>(this.value).
  • templates/tickets/form.html.twig
    • on_change on the voucher dropdown: removed, bound with jQuery .on('change') on #dropdown_plugin_credit_entities_id<rand>, calling getQuantityRemainingForm<rand>(this.value, this). The dropdown now receives the template rand explicitly so the id is known.

No PHP output of <script> or handler attributes was found. The two <script> blocks are literal Twig, so the nonce is added 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. None should appear.

  1. Ticket, Processing form, "Consume a voucher ?" switch: toggling it shows and hides the voucher block, and enables and disables the quantity field.
  2. Same form, choose a voucher: the quantity field appears, is set to 1, and the "Maximum consumable quantity" tooltip shows the value returned by the AJAX call.
  3. Same form, choose the empty voucher entry: the quantity field is hidden and set to 0.
  4. Ticket, Credit tab, "Add a voucher to the ticket" form: choosing a voucher shows the quantity field with the same behaviour as step 2; choosing the empty entry hides it as in step 3.
  5. Submit the Credit tab form and a ticket solution with consumption: the consumption is recorded as before.
  6. Browser console: no CSP violation messages on those pages.

Screenshots (if appropriate):

@glpi-project

glpi-project Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

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

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

@glpi-project glpi-project Bot mentioned this pull request Nov 25, 2026
2 of 4 tasks
@Rom1-B
Rom1-B force-pushed the forward-port/279-to-main branch from d6efadc to 025d752 Compare November 25, 2026 14:22
@Rom1-B
Rom1-B marked this pull request as ready for review November 26, 2026 15:04
@Rom1-B
Rom1-B requested review from a team, Herafia, Mary-Clb and Rom1-B and removed request for a team November 26, 2026 15:04
@Rom1-B
Rom1-B merged commit ae7c457 into main Dec 1, 2026
7 checks passed
@Rom1-B
Rom1-B deleted the forward-port/279-to-main branch December 1, 2026 12:20
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