From e38ddbb718e043e6074d9bf21dbc9b2c22f8f1a3 Mon Sep 17 00:00:00 2001 From: Rom1-B <8530352+Rom1-B@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:02:00 +0100 Subject: [PATCH] feat: make plugin CSP compliant --- CHANGELOG.md | 6 +++++ front/config.form.php | 5 +--- hook.php | 6 ++--- inc/config.class.php | 63 +++++++++++++++++++++++++++++++++++-------- public/left.php | 3 ++- setup.php | 1 + 6 files changed, 65 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c69fde..559f9d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Changed + +- Make the plugin compatible with the Content Security Policy + ## [1.20.4] - 2026-11-05 ### Fixed diff --git a/front/config.form.php b/front/config.form.php index 824f815..269b4ff 100644 --- a/front/config.form.php +++ b/front/config.form.php @@ -55,10 +55,7 @@ } else { Html::header(__s('Setup'), $_SERVER['PHP_SELF'], 'config', 'plugin'); // close frame if open - echo " "; + echo Html::scriptBlock('if (top != self) top.location = self.location;'); } Html::footer(); diff --git a/hook.php b/hook.php index a706ba3..8c99417 100644 --- a/hook.php +++ b/hook.php @@ -285,7 +285,7 @@ function plugin_treeview_uninstall() function plugin_item_update_treeview($item) { if (in_array('locations_id', $item->updates)) { - echo ""; + echo Html::scriptBlock('parent.left.location.reload(true);'); } } @@ -295,7 +295,7 @@ function plugin_item_update_treeview($item) */ function plugin_treeview_reload($item) { - echo ""; + echo Html::scriptBlock('parent.left.location.reload(true);'); } @@ -306,6 +306,6 @@ function plugin_change_entity_Treeview() && (isset($_SESSION['glpi_plugin_treeview_loaded']) && $_SESSION['glpi_plugin_treeview_loaded'] == 1) ) { - echo ""; + echo Html::scriptBlock('parent.left.location.reload(true);'); } } diff --git a/inc/config.class.php b/inc/config.class.php index ebb8c0b..0fd993e 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -31,6 +31,8 @@ use Glpi\DBAL\QueryExpression; use function Safe\json_encode; +use function Safe\ob_get_clean; +use function Safe\ob_start; /** * Contains the display configuration of the treeview @@ -164,10 +166,8 @@ public function seeTreeview() $CFG_GLPI['root_doc'] . "/pics/favicon.ico' >\n"; // Must be always the top window - echo ''; + echo Html::scriptBlock('if (top != self) top.location = self.location;'); + echo ''; echo ""; echo ""; echo ""; @@ -185,10 +185,7 @@ public function seeTreeview() **/ public function hideTreeview() { - echo "'; + echo Html::scriptBlock('if (top != self) top.location = self.location;'); } /** @@ -205,13 +202,57 @@ public function buildTreeview() //necessary files needed for the tree to work. echo sprintf("", $treeview_url); - echo sprintf("", $treeview_url); + echo Html::script('/plugins/treeview/lib/dtree/dtree.js'); echo "
"; - echo "'; + echo Html::scriptBlock(ob_get_clean()); echo '
'; + + // dTree renders its links with inline handlers, rebind them once the tree is in the DOM + echo Html::scriptBlock(<<<'JS' + (function () { + var pattern = /^\s*javascript:\s*(\w+)\.([os])\((\d+)\);?\s*$/; + var takeCall = function (link, attribute) { + var match = (link.getAttribute(attribute) || '').match(pattern); + if (match !== null) { + link.removeAttribute(attribute); + } + return match; + }; + var tree = document.querySelector('.dtree'); + if (tree === null) { + return; + } + tree.querySelectorAll('a').forEach(function (link) { + var select = takeCall(link, 'onclick'); + var toggle = takeCall(link, 'href'); + if (toggle !== null) { + link.setAttribute('href', '#'); + } + link.addEventListener('click', function (event) { + if (toggle !== null) { + event.preventDefault(); + window[toggle[1]][toggle[2]](Number(toggle[3])); + } + if (select !== null) { + window[select[1]][select[2]](Number(select[3])); + } + }); + if (link.hasAttribute('onmouseover')) { + link.removeAttribute('onmouseover'); + link.removeAttribute('onmouseout'); + link.addEventListener('mouseover', function () { + window.status = link.textContent; + }); + link.addEventListener('mouseout', function () { + window.status = ''; + }); + } + }); + })(); + JS); } /** diff --git a/public/left.php b/public/left.php index befb9c4..f1eda82 100644 --- a/public/left.php +++ b/public/left.php @@ -50,9 +50,10 @@ echo ''; echo '
'; -echo ""; +echo ""; echo ""; echo ""; +echo Html::scriptBlock('document.getElementById("explorer_close_button").addEventListener("click", () => { parent.location.href = parent.right.location.href; });'); echo '
'; echo ''; diff --git a/setup.php b/setup.php index fe207f7..a5518cd 100644 --- a/setup.php +++ b/setup.php @@ -136,6 +136,7 @@ function plugin_version_treeview() 'license' => 'GPLv2+', 'author' => 'AL-Rubeiy Hussein, Xavier Caillaud, Nelly Mahu-Lasson', 'homepage' => 'https://forge.indepnet.net/projects/treeview', + 'csp_compliant' => true, 'requirements' => [ 'glpi' => [ 'min' => PLUGIN_TREEVIEW_MIN_GLPI,