From bca8555b952a9eac649aff6b23351fee079cd2be Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Tue, 17 Nov 2026 10:46:58 +0100 Subject: [PATCH 1/5] Fix - Injection form checks and CSV exports --- CHANGELOG.md | 12 +++++++ ajax/dropdownChooseField.php | 2 +- ajax/dropdownMandatory.php | 2 +- front/clientinjection.form.php | 2 +- front/export.pdf.php | 5 ++- inc/clientinjection.class.php | 13 ++++--- inc/info.class.php | 19 ++++++----- inc/infocollection.class.php | 11 +++--- inc/mapping.class.php | 34 +++++++++++-------- inc/mappingcollection.class.php | 11 +++--- inc/model.class.php | 4 +-- inc/modelcsv.class.php | 12 ++++++- inc/profile.class.php | 26 ++++---------- tests/unit/AdditionalInformationTest.php | 19 +++++++++++ .../ClientInjectionEscapeCsvFormulaTest.php | 2 ++ .../ClientInjectionWriteErrorsCsvTest.php | 4 +-- 16 files changed, 112 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9da8f898..47e4cb59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,18 @@ 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] + +### 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 + ## [2.15.13] - 2026-11-13 ### Fixed diff --git a/ajax/dropdownChooseField.php b/ajax/dropdownChooseField.php index 4ccde6e0..9a17b266 100644 --- a/ajax/dropdownChooseField.php +++ b/ajax/dropdownChooseField.php @@ -34,5 +34,5 @@ Html::header_nocache(); } -Session::checkCentralAccess(); +Session::checkRight(PluginDatainjectionModel::$rightname, READ); PluginDatainjectionInjectionType::dropdownFields($_POST); diff --git a/ajax/dropdownMandatory.php b/ajax/dropdownMandatory.php index 1dc0e8f4..d5c7a440 100644 --- a/ajax/dropdownMandatory.php +++ b/ajax/dropdownMandatory.php @@ -34,5 +34,5 @@ Html::header_nocache(); } -Session::checkCentralAccess(); +Session::checkRight(PluginDatainjectionModel::$rightname, READ); PluginDatainjectionInjectionType::showMandatoryCheckbox($_POST); diff --git a/front/clientinjection.form.php b/front/clientinjection.form.php index 4b4bd5d8..e968b9ff 100644 --- a/front/clientinjection.form.php +++ b/front/clientinjection.form.php @@ -43,7 +43,7 @@ PluginDatainjectionClientInjection::showInjectionForm($model, $_SESSION['glpiactive_entity']); } elseif (isset($_POST['upload'])) { $model = new PluginDatainjectionModel(); - $model->can($_POST['id'], READ); + $model->check((int) $_POST['id'], READ); $_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..4f2d3d24 100644 --- a/front/export.pdf.php +++ b/front/export.pdf.php @@ -30,4 +30,7 @@ Session::checkRight(PluginDatainjectionClientInjection::$rightname, READ); -PluginDatainjectionModel::exportAsPDF($_GET['models_id']); +$models_id = (int) $_GET['models_id']; +(new PluginDatainjectionModel())->check($models_id, READ); + +PluginDatainjectionModel::exportAsPDF($models_id); diff --git a/inc/clientinjection.class.php b/inc/clientinjection.class.php index a8a12128..67ef2b9f 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 899eb3dc..c73316ac 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,13 +491,14 @@ public static function dropdownTemplates($name, $table) $values = [0 => Dropdown::EMPTY_VALUE]; - $sql = "SELECT `id`, `template_name` - FROM `" . $table . "` - WHERE `is_template`=1 " . - getEntitiesRestrictRequest(' AND ', $table) . - "ORDER BY `template_name`"; + $iterator = $DB->request([ + 'SELECT' => ['id', 'template_name'], + 'FROM' => $table, + 'WHERE' => ['is_template' => 1] + getEntitiesRestrictCriteria($table), + 'ORDER' => 'template_name', + ]); - foreach ($DB->doQuery($sql) as $data) { + foreach ($iterator as $data) { $values[$data['id']] = $data['template_name']; } diff --git a/inc/infocollection.class.php b/inc/infocollection.class.php index efbc6096..0945567a 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 d9a3b495..275b6934 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 c49b87af..4f249645 100644 --- a/inc/model.class.php +++ b/inc/model.class.php @@ -1500,8 +1500,8 @@ public static function exportAsPDF($models_id) sprintf( __('%1$s (%2$s)'), __('Data injection report', 'datainjection') . ' - ' . - PluginDatainjectionSession::getParam('file_name') . '', - $model->getName(), + htmlescape((string) PluginDatainjectionSession::getParam('file_name')) . '', + htmlescape($model->getName()), ), ); $pdf->newPage(); diff --git a/inc/modelcsv.class.php b/inc/modelcsv.class.php index 8666a76b..2067f3a4 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 @@ -119,7 +125,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 bd657c27..e2bace97 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('