From 6af2577ee1f0b38b2cbb206181d6275dffe199c3 Mon Sep 17 00:00:00 2001 From: Romain Lecouvreur <102067890+RomainLvr@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:52:54 +0100 Subject: [PATCH] BACKPORT-CONFLICT --- CHANGELOG.md | 23 +++++ ajax/dropdownChooseField.php | 6 ++ ajax/dropdownMandatory.php | 6 ++ front/clientinjection.form.php | 18 +++- front/export.pdf.php | 16 ++- inc/clientinjection.class.php | 13 ++- inc/info.class.php | 17 +++- inc/infocollection.class.php | 11 ++- inc/mapping.class.php | 34 ++++--- inc/mappingcollection.class.php | 11 ++- inc/model.class.php | 12 ++- inc/modelcsv.class.php | 12 ++- inc/profile.class.php | 26 ++--- tests/unit/AdditionalInformationTest.php | 19 ++++ .../ClientInjectionEscapeCsvFormulaTest.php | 2 + .../ClientInjectionWriteErrorsCsvTest.php | 4 +- tests/unit/ModelIsFileCorrectTest.php | 97 +++++++++++++++++++ 17 files changed, 270 insertions(+), 57 deletions(-) create mode 100644 tests/unit/ModelIsFileCorrectTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 745effba..534f272e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,33 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [2.17.0] - 2026-11-13 +### Fixed + +- Fix handling of additional information values in the injection form +- Fix model checks on the injection upload and the PDF report export +- Fix rights used by the model field dropdowns +- Fix CSV export of headers and of the model sample file +- Fix file name display in the PDF report header +- Fix temporary error file not being removed when its download fails +- Use the query builder for the remaining queries +- Fix models left in a deleted entity: declare their entity relation so it is checked and replaced on entity deletion, and move models already left in a deleted entity to the root entity + ### Added +<<<<<<< HEAD - Add Transifex translations link to README - Inject generic assets, including their type, model, comment and custom fields +======= +- Make the plugin compatible with the Content Security Policy + +## [2.15.13] - 2026-11-13 + +### Fixed + +- Fix the uploaded file name being lost after the client injection upload step +- Fix models mapped to additional fields (Fields plugin) becoming inaccessible and their values being rejected on injection +- Fix entity injection failing when additional fields (Fields plugin) are mapped +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) - Fix additional information sharing a field name across itemtypes (e.g. two device designations) overwriting each other, and show the itemtype next to each field - Stop flagging a network port injection as partial when no connection to a network equipment port is requested - Made several generic warnings more explicit. They explain why the mapping failed. diff --git a/ajax/dropdownChooseField.php b/ajax/dropdownChooseField.php index 4ccde6e0..6d87b2eb 100644 --- a/ajax/dropdownChooseField.php +++ b/ajax/dropdownChooseField.php @@ -34,5 +34,11 @@ Html::header_nocache(); } +<<<<<<< HEAD Session::checkCentralAccess(); +======= +Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); +// Reads the session only: release it so that the calls of a tab run in parallel +Session::writeClose(); +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) PluginDatainjectionInjectionType::dropdownFields($_POST); diff --git a/ajax/dropdownMandatory.php b/ajax/dropdownMandatory.php index 1dc0e8f4..cc61ab17 100644 --- a/ajax/dropdownMandatory.php +++ b/ajax/dropdownMandatory.php @@ -34,5 +34,11 @@ Html::header_nocache(); } +<<<<<<< HEAD Session::checkCentralAccess(); +======= +Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); +// Reads the session only: release it so that the calls of a tab run in parallel +Session::writeClose(); +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) PluginDatainjectionInjectionType::showMandatoryCheckbox($_POST); diff --git a/front/clientinjection.form.php b/front/clientinjection.form.php index 32161117..bd2c36ff 100644 --- a/front/clientinjection.form.php +++ b/front/clientinjection.form.php @@ -28,7 +28,14 @@ * ------------------------------------------------------------------------- */ +<<<<<<< HEAD Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); +======= +use Glpi\Exception\Http\AccessDeniedHttpException; +use Glpi\Exception\Http\BadRequestHttpException; + +Session::checkRight("plugin_datainjection_use", READ); +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) Html::header( __('Data injection', 'datainjection'), @@ -43,7 +50,16 @@ PluginDatainjectionClientInjection::showInjectionForm($model, $_SESSION['glpiactive_entity']); } elseif (isset($_POST['upload'])) { $model = new PluginDatainjectionModel(); - $model->can($_POST['id'], READ); + $models_id = (int) ($_POST['id'] ?? 0); + if ($models_id <= 0) { + throw new BadRequestHttpException(); + } + + //Injection users may lack the model management right: only check the model itself is visible + if (!$model->getFromDB($models_id) || !$model->canViewItem()) { + throw new AccessDeniedHttpException(); + } + $_SESSION['datainjection']['infos'] = ($_POST['info'] ?? []); //If additional informations provided : check if mandatory infos are present diff --git a/front/export.pdf.php b/front/export.pdf.php index 2c82c7d5..4f4baed5 100644 --- a/front/export.pdf.php +++ b/front/export.pdf.php @@ -28,6 +28,20 @@ * ------------------------------------------------------------------------- */ +use Glpi\Exception\Http\AccessDeniedHttpException; +use Glpi\Exception\Http\BadRequestHttpException; + Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); -PluginDatainjectionModel::exportAsPDF($_GET['models_id']); +$models_id = (int) ($_GET['models_id'] ?? 0); +if ($models_id <= 0) { + throw new BadRequestHttpException(); +} + +//Injection users may lack the model management right: only check the model itself is visible +$model = new PluginDatainjectionModel(); +if (!$model->getFromDB($models_id) || !$model->canViewItem()) { + throw new AccessDeniedHttpException(); +} + +PluginDatainjectionModel::exportAsPDF($models_id); diff --git a/inc/clientinjection.class.php b/inc/clientinjection.class.php index c39fdd01..53b07e84 100644 --- a/inc/clientinjection.class.php +++ b/inc/clientinjection.class.php @@ -328,9 +328,9 @@ public static function showResultsForm(PluginDatainjectionModel $model) TemplateRenderer::getInstance()->display('@datainjection/clientinjection_result.html.twig', $data); } - private static function escapeCsvFormula(mixed $value): mixed + public static function escapeCsvFormula(mixed $value): mixed { - if (is_string($value) && isset($value[0]) && in_array($value[0], ['=', '+', '-', '@'], true)) { + if (is_string($value) && isset($value[0]) && in_array($value[0], ['=', '+', '-', '@', "\t", "\r"], true)) { return "'" . $value; } @@ -351,7 +351,7 @@ private static function writeErrorsCsv(string $dir, array $error_lines, array $h $tmpfile = fopen($file, 'w'); if ($headers !== []) { - fputcsv($tmpfile, $headers, $delimiter); + fputcsv($tmpfile, array_map(self::escapeCsvFormula(...), $headers), $delimiter); } foreach ($error_lines as $line) { @@ -422,8 +422,11 @@ public static function exportErrorsInCSV() header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Expires: 0'); - readfile($file); - unlink($file); + try { + readfile($file); + } finally { + unlink($file); + } } } } diff --git a/inc/info.class.php b/inc/info.class.php index b989165d..793353f9 100644 --- a/inc/info.class.php +++ b/inc/info.class.php @@ -360,9 +360,9 @@ public static function showAdditionalInformation( $value = ($option['default'] ?? ''); } - echo sprintf(""; @@ -406,7 +406,7 @@ public static function showAdditionalInformation( break; case 'multiline_text': - echo sprintf("", $name, $value); + echo sprintf("", htmlescape($name), htmlescape((string) $value)); break; case 'dropdown_integer': @@ -491,6 +491,7 @@ public static function dropdownTemplates($name, $table) $values = [0 => Dropdown::EMPTY_VALUE]; +<<<<<<< HEAD $result = $DB->request([ 'SELECT' => ['id', 'template_name'], 'FROM' => $table, @@ -501,6 +502,16 @@ public static function dropdownTemplates($name, $table) ]); foreach ($result as $data) { +======= + $iterator = $DB->request([ + 'SELECT' => ['id', 'template_name'], + 'FROM' => $table, + 'WHERE' => ['is_template' => 1] + getEntitiesRestrictCriteria($table), + 'ORDER' => 'template_name', + ]); + + foreach ($iterator as $data) { +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) $values[$data['id']] = $data['template_name']; } diff --git a/inc/infocollection.class.php b/inc/infocollection.class.php index 6b825df2..02c4474d 100644 --- a/inc/infocollection.class.php +++ b/inc/infocollection.class.php @@ -45,12 +45,13 @@ public function load($models_id) /** @var DBmysql $DB */ global $DB; - $query = "SELECT * - FROM `glpi_plugin_datainjection_infos` - WHERE `models_id` = '" . $models_id . "' - ORDER BY `itemtype` ASC"; + $iterator = $DB->request([ + 'FROM' => 'glpi_plugin_datainjection_infos', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'itemtype ASC', + ]); - foreach ($DB->doQuery($query) as $data) { + foreach ($iterator as $data) { $infos = new PluginDatainjectionInfo(); $infos->fields = $data; $this->infosCollection[] = $infos; diff --git a/inc/mapping.class.php b/inc/mapping.class.php index 20dfa36e..3e3e2ecf 100644 --- a/inc/mapping.class.php +++ b/inc/mapping.class.php @@ -29,6 +29,7 @@ */ use Glpi\Application\View\TemplateRenderer; +use Glpi\DBAL\QueryExpression; use function Safe\ob_get_clean; use function Safe\ob_start; @@ -148,15 +149,18 @@ public static function getSeveralMappedField($models_id) global $DB; $several = []; - $query = "SELECT `value`, - COUNT(*) AS counter - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '" . $models_id . "' - AND `value` NOT IN ('none') - GROUP BY `value` - HAVING `counter` > 1"; - - foreach ($DB->doQuery($query) as $mapping) { + $iterator = $DB->request([ + 'SELECT' => ['value', new QueryExpression('COUNT(*) AS counter')], + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => [ + 'models_id' => (int) $models_id, + 'NOT' => ['value' => 'none'], + ], + 'GROUPBY' => 'value', + 'HAVING' => ['counter' => ['>', 1]], + ]); + + foreach ($iterator as $mapping) { $several[] = $mapping['value']; } @@ -173,11 +177,13 @@ public static function getMappingsSortedByRank($models_id) global $DB; $mappings = []; - $query = "SELECT `name` - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '" . $models_id . "' - ORDER BY `rank` ASC"; - foreach ($DB->doQuery($query) as $data) { + $iterator = $DB->request([ + 'SELECT' => 'name', + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'rank ASC', + ]); + foreach ($iterator as $data) { $mappings[] = $data['name']; } diff --git a/inc/mappingcollection.class.php b/inc/mappingcollection.class.php index 71cb8bfa..4a480fc7 100644 --- a/inc/mappingcollection.class.php +++ b/inc/mappingcollection.class.php @@ -44,14 +44,15 @@ public function load($models_id) /** @var DBmysql $DB */ global $DB; - $sql = "SELECT * - FROM `glpi_plugin_datainjection_mappings` - WHERE `models_id` = '{$models_id}' - ORDER BY `rank` ASC"; + $iterator = $DB->request([ + 'FROM' => 'glpi_plugin_datainjection_mappings', + 'WHERE' => ['models_id' => (int) $models_id], + 'ORDER' => 'rank ASC', + ]); $this->mappingCollection = []; - foreach ($data = $DB->doQuery($sql) as $data) { + foreach ($iterator as $data) { $mapping = new PluginDatainjectionMapping(); $mapping->fields = $data; $this->mappingCollection[] = $mapping; diff --git a/inc/model.class.php b/inc/model.class.php index d719329e..0a039991 100644 --- a/inc/model.class.php +++ b/inc/model.class.php @@ -1168,8 +1168,13 @@ public function isFileCorrect() ), ); $name_from_db = trim(mb_strtoupper((string) $mapping->getName(), 'UTF-8')); + // Sample and error files are exported with formula-escaped headers + $escaped_name_from_db = trim(mb_strtoupper( + (string) PluginDatainjectionClientInjection::escapeCsvFormula((string) $mapping->getName()), + 'UTF-8', + )); - if ($name_from_db !== $name_from_file) { + if ($name_from_db !== $name_from_file && $escaped_name_from_db !== $name_from_file) { if ($error['error_message'] == '') { $error['error_message'] = __('At least one column is incorrect', 'datainjection'); } @@ -1496,8 +1501,13 @@ public static function exportAsPDF($models_id) $pdf->setHeader( sprintf( __('%1$s (%2$s)'), +<<<<<<< HEAD __('Data injection report', 'datainjection') . ' - ' . PluginDatainjectionSession::getParam('file_name') . '', +======= + __('Data injection report', 'datainjection') . ' - ' + . PluginDatainjectionSession::getParam('file_name'), +>>>>>>> c784dc8 (Fix - Injection form checks and CSV exports (#717)) $model->getName(), ), ); diff --git a/inc/modelcsv.class.php b/inc/modelcsv.class.php index 8994778d..d732c3a4 100644 --- a/inc/modelcsv.class.php +++ b/inc/modelcsv.class.php @@ -30,6 +30,12 @@ use Glpi\Application\View\TemplateRenderer; +use function Safe\fclose; +use function Safe\fopen; +use function Safe\fputcsv; +use function Safe\rewind; +use function Safe\stream_get_contents; + /** * ------------------------------------------------------------------------- * DataInjection plugin for GLPI @@ -118,7 +124,11 @@ public function showSample(PluginDatainjectionModel $model) { $headers = PluginDatainjectionMapping::getMappingsSortedByRank($model->fields['id']); - $sample = '"' . implode('"' . $this->getDelimiter() . '"', $headers) . "\"\n"; + $handle = fopen('php://temp', 'r+'); + fputcsv($handle, array_map(PluginDatainjectionClientInjection::escapeCsvFormula(...), $headers), $this->getDelimiter()); + rewind($handle); + $sample = stream_get_contents($handle); + fclose($handle); header( 'Content-disposition: attachment; filename="' . str_replace( diff --git a/inc/profile.class.php b/inc/profile.class.php index ac57751b..b0608c8c 100644 --- a/inc/profile.class.php +++ b/inc/profile.class.php @@ -48,21 +48,6 @@ public static function getAllRights() ]; } - /** - * Clean profiles_id from plugin's profile table - * - * @param int $ID - **/ - public function cleanProfiles($ID) - { - /** @var DBmysql $DB */ - global $DB; - $query = "DELETE FROM `glpi_profiles` - WHERE `profiles_id`='{$ID}' - AND `name` LIKE '%plugin_datainjection%'"; - $DB->doQuery($query); - } - public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { @@ -147,10 +132,13 @@ public static function migrateProfiles() $profiles = getAllDataFromTable('glpi_plugin_datainjection_profiles'); foreach ($profiles as $id => $profile) { - $query = "SELECT `id` FROM `glpi_profiles` WHERE `name`='" . $profile['name'] . "'"; - $result = $DB->doQuery($query); - if ($DB->numrows($result) == 1) { - $id = $DB->result($result, 0, 'id'); + $iterator = $DB->request([ + 'SELECT' => 'id', + 'FROM' => 'glpi_profiles', + 'WHERE' => ['name' => $profile['name']], + ]); + if (count($iterator) === 1) { + $id = $iterator->current()['id']; switch ($profile['model']) { case 'r': $value = READ; diff --git a/tests/unit/AdditionalInformationTest.php b/tests/unit/AdditionalInformationTest.php index f366d82f..12fc5e99 100644 --- a/tests/unit/AdditionalInformationTest.php +++ b/tests/unit/AdditionalInformationTest.php @@ -131,4 +131,23 @@ public function testFormInputIsNamedAfterItemtypeAndField(): void self::assertStringContainsString("name='info[DeviceProcessor][designation]'", $html); self::assertStringContainsString(DeviceProcessor::getTypeName(1) . ' - ', $html); } + + public function testPostedValueIsEscaped(): void + { + $this->login(); + + $info = new PluginDatainjectionInfo(); + $info->fields = ['itemtype' => 'DeviceProcessor', 'value' => 'designation', 'is_mandatory' => 0]; + + $_SESSION['datainjection']['infos'] = ['DeviceProcessor' => ['designation' => "'>"]]; + + ob_start(); + PluginDatainjectionInfo::displayAdditionalInformation($info); + $html = ob_get_clean(); + + unset($_SESSION['datainjection']['infos']); + + self::assertStringNotContainsString('