From 034749e6f09e22f6667f2f6ab55b3f9e0f49b7bb Mon Sep 17 00:00:00 2001 From: AdamoLeKing <72097330+AdamoLeKing@users.noreply.github.com> Date: Tue, 6 Oct 2026 09:07:49 +0200 Subject: [PATCH 1/3] Fix: do not strip special characters from labels --- inc/container.class.php | 4 ++-- inc/dropdown.class.php | 2 +- inc/labeltranslation.class.php | 4 ++-- inc/toolbox.class.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 0bed98d9..0dd32717 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -621,7 +621,7 @@ public function prepareInputForUpdate($input) { // sanitize label only; name is intentionally left untouched here (see migration callers) if (isset($input['label']) && !empty($input['label'])) { - $input['label'] = PluginFieldsToolbox::sanitizeLabel((string) $input['label']); + $input['label'] = trim((string) $input['label']); } if (isset($input['itemtypes'])) { @@ -799,7 +799,7 @@ public static function generateTemplate($fields) $fields['name'] = PluginFieldsToolbox::sanitizeLabel((string) $fields['name']); $fields['id'] = (int) PluginFieldsToolbox::sanitizeLabel((string) $fields['id']); - $fields['label'] = PluginFieldsToolbox::sanitizeLabel((string) $fields['label']); + $fields['label'] = (string) $fields['label']; foreach ($itemtypes as $itemtype) { $sysname = self::getSystemName($itemtype, $fields['name']); diff --git a/inc/dropdown.class.php b/inc/dropdown.class.php index 3ec0d2eb..cd164876 100644 --- a/inc/dropdown.class.php +++ b/inc/dropdown.class.php @@ -116,7 +116,7 @@ public static function create($input) // Safe inputs $input['name'] = PluginFieldsToolbox::sanitizeLabel((string) $input['name']); $input['id'] = (int) PluginFieldsToolbox::sanitizeLabel((string) $input['id']); - $input['label'] = PluginFieldsToolbox::sanitizeLabel((string) $input['label']); + $input['label'] = (string) $input['label']; $classname = self::getClassname($input['name']); diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index 8b1ff9ac..1905c5e3 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -113,14 +113,14 @@ public static function getTypeName($nb = 0) public function prepareInputForUpdate($input) { - $input['label'] = PluginFieldsToolbox::sanitizeLabel((string) ($input['label'] ?? '')); + $input['label'] = trim((string) $input['label']); return $input; } public function prepareInputForAdd($input) { - $input['label'] = PluginFieldsToolbox::sanitizeLabel((string) ($input['label'] ?? '')); + $input['label'] = trim((string) $input['label']); return $input; } diff --git a/inc/toolbox.class.php b/inc/toolbox.class.php index f1922a6c..8f27daab 100644 --- a/inc/toolbox.class.php +++ b/inc/toolbox.class.php @@ -400,7 +400,7 @@ public static function sanitizeLabel(string $label): string */ public static function prepareLabel(array $input): array { - $input['label'] = self::sanitizeLabel((string) ($input['label'] ?? '')); + $input['label'] = trim((string) ($input['label'] ?? '')); $input['name'] = (new self())->getSystemNameFromLabel($input['label']); return $input; From 6bda2e6be5d70e617155a7f4a5afc092e98b0933 Mon Sep 17 00:00:00 2001 From: AdamoLeKing <72097330+AdamoLeKing@users.noreply.github.com> Date: Wed, 7 Oct 2026 13:34:35 +0200 Subject: [PATCH 2/3] test(labels): cover special characters in block and field labels --- tests/Units/LabelSpecialCharsTest.php | 235 ++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 tests/Units/LabelSpecialCharsTest.php diff --git a/tests/Units/LabelSpecialCharsTest.php b/tests/Units/LabelSpecialCharsTest.php new file mode 100644 index 00000000..0fd5e1c5 --- /dev/null +++ b/tests/Units/LabelSpecialCharsTest.php @@ -0,0 +1,235 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2013-2023 by Fields plugin team. + * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html + * @link https://github.com/pluginsGLPI/fields + * ------------------------------------------------------------------------- + */ + +declare(strict_types=1); + +namespace GlpiPlugin\Field\Tests\Units; + +use Computer; +use Glpi\Tests\DbTestCase; +use Glpi\Tests\GLPITestCase; +use GlpiPlugin\Field\Tests\FieldTestTrait; +use PHPUnit\Framework\Attributes\DataProvider; +use PluginFieldsContainer; +use PluginFieldsDropdown; +use PluginFieldsField; +use PluginFieldsLabelTranslation; + +require_once __DIR__ . '/../FieldTestCase.php'; + +/** + * Labels are free text: they must be stored and displayed as typed by the user, + * while system names (tables, columns, classes, files) stay restricted to safe chars. + */ +final class LabelSpecialCharsTest extends DbTestCase +{ + use FieldTestTrait; + + public function setUp(): void + { + GLPITestCase::setUp(); + $this->login(); + } + + public function tearDown(): void + { + $this->tearDownFieldTest(); + GLPITestCase::tearDown(); + } + + public static function provideLabels(): iterable + { + yield 'apostrophe' => [ + 'label' => "N° d'inventaire", + ]; + + yield 'slash' => [ + 'label' => 'Site / Bâtiment', + ]; + + yield 'parentheses and ampersand' => [ + 'label' => 'Contrat (R&D)', + ]; + + yield 'double quotes' => [ + 'label' => 'Le "bon" champ', + ]; + + yield 'accents and punctuation' => [ + 'label' => 'Échéance : été, hiver ?', + ]; + + yield 'html markup' => [ + 'label' => 'Important', + ]; + + yield 'php code injection attempt' => [ + 'label' => "x'); die('pwned'); //", + ]; + } + + #[DataProvider('provideLabels')] + public function testContainerLabelKeepsSpecialChars(string $label): void + { + $container = $this->createFieldContainer([ + 'label' => $label, + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $this->assertTrue($container->getFromDB($container->getID())); + $this->assertSame($label, $container->fields['label']); + + // System name is still restricted to identifier safe chars + $this->assertMatchesRegularExpression('/^[a-z]+$/', $container->fields['name']); + + // Displayed label (translation) is kept as typed + $this->assertSame( + $label, + PluginFieldsLabelTranslation::getLabelFor([ + 'itemtype' => PluginFieldsContainer::class, + 'id' => $container->getID(), + ]), + ); + } + + #[DataProvider('provideLabels')] + public function testFieldLabelKeepsSpecialChars(string $label): void + { + $container = $this->createFieldContainer([ + 'label' => 'Label special chars ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $field = $this->createField([ + 'label' => $label, + 'type' => 'text', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + ]); + + $this->assertTrue($field->getFromDB($field->getID())); + $this->assertSame($label, $field->fields['label']); + + // System name is still restricted to identifier safe chars + $this->assertMatchesRegularExpression('/^[a-z0-9_]+$/', $field->fields['name']); + + // Displayed label (translation) is kept as typed + $this->assertSame( + $label, + PluginFieldsLabelTranslation::getLabelFor([ + 'itemtype' => PluginFieldsField::class, + 'id' => $field->getID(), + ]), + ); + } + + #[DataProvider('provideLabels')] + public function testTranslationUpdateKeepsSpecialChars(string $label): void + { + $container = $this->createFieldContainer([ + 'label' => 'Label translation ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $field = $this->createField([ + 'label' => 'Initial label', + 'type' => 'text', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + ]); + + $translations = (new PluginFieldsLabelTranslation())->find([ + 'itemtype' => PluginFieldsField::class, + 'items_id' => $field->getID(), + 'language' => $_SESSION['glpilanguage'], + ]); + $this->assertCount(1, $translations); + + $translation = new PluginFieldsLabelTranslation(); + $this->assertTrue($translation->update([ + 'id' => array_key_first($translations), + 'label' => $label, + ])); + + $this->assertSame( + $label, + PluginFieldsLabelTranslation::getLabelFor([ + 'itemtype' => PluginFieldsField::class, + 'id' => $field->getID(), + ]), + ); + } + + #[DataProvider('provideLabels')] + public function testDropdownClassIsSafelyGeneratedWithSpecialCharsInLabel(string $label): void + { + $container = $this->createFieldContainer([ + 'label' => 'Label dropdown ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $field = $this->createField([ + 'label' => $label, + 'type' => 'dropdown', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + ]); + + $classname = PluginFieldsDropdown::getClassname($field->fields['name']); + + // The label is injected in the generated class file as an exported string literal only + $class_file = PLUGINFIELDS_CLASS_PATH . '/' . $field->fields['name'] . 'dropdown.class.php'; + $this->assertFileExists($class_file); + $this->assertStringContainsString(var_export($label, true), (string) file_get_contents($class_file)); + + // The generated class is valid, loadable and returns the label as typed + $this->assertTrue(class_exists($classname)); + $this->assertSame($label, $classname::getTypeName()); + } +} \ No newline at end of file From 96aa29623c29454c448e440ca070edd2a2fba181 Mon Sep 17 00:00:00 2001 From: AdamoLeKing <72097330+AdamoLeKing@users.noreply.github.com> Date: Fri, 13 Nov 2026 09:54:04 +0100 Subject: [PATCH 3/3] fix(labels): address review feedback - only trim the translation label when the key is present, so partial updates do not overwrite it with an empty string - update comment and docblock to match the trim-only behaviour - add a test for a translation update without label key --- inc/container.class.php | 2 +- inc/labeltranslation.class.php | 11 +++++--- inc/toolbox.class.php | 6 +++-- tests/Units/LabelSpecialCharsTest.php | 36 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/inc/container.class.php b/inc/container.class.php index 2b70fa87..9e934df3 100644 --- a/inc/container.class.php +++ b/inc/container.class.php @@ -619,7 +619,7 @@ public function defineTabs($options = []) public function prepareInputForUpdate($input) { - // sanitize label only; name is intentionally left untouched here (see migration callers) + // trim label only (free text, escaped on output); name is intentionally left untouched here (see migration callers) if (isset($input['label']) && !empty($input['label'])) { $input['label'] = trim((string) $input['label']); } diff --git a/inc/labeltranslation.class.php b/inc/labeltranslation.class.php index c319ac0b..1964d7c1 100644 --- a/inc/labeltranslation.class.php +++ b/inc/labeltranslation.class.php @@ -113,19 +113,24 @@ public static function getTypeName($nb = 0) public function prepareInputForUpdate($input) { - $input['label'] = trim((string) $input['label']); + // Labels are free text, stored as data and escaped on output: only trim them. + if (isset($input['label'])) { + $input['label'] = trim((string) $input['label']); + } return $input; } public function prepareInputForAdd($input) { - $input['label'] = trim((string) $input['label']); + // Labels are free text, stored as data and escaped on output: only trim them. + if (isset($input['label'])) { + $input['label'] = trim((string) $input['label']); + } return $input; } - public static function createForItem(CommonDBTM $item) { $translation = new PluginFieldsLabelTranslation(); diff --git a/inc/toolbox.class.php b/inc/toolbox.class.php index 8f27daab..f874e381 100644 --- a/inc/toolbox.class.php +++ b/inc/toolbox.class.php @@ -392,11 +392,13 @@ public static function sanitizeLabel(string $label): string } /** - * Sanitize the label in $input and derive the system name from it. + * Trim the label in $input and derive the system name from it. + * Labels are free text (escaped on output); only the derived system name + * is restricted to identifier safe characters. * Centralises the prepare-label pattern used in prepareInputForAdd/Update. * * @param array $input Input array with at least a 'label' key. - * @return array Updated input with sanitized 'label' and derived 'name'. + * @return array Updated input with trimmed 'label' and derived 'name'. */ public static function prepareLabel(array $input): array { diff --git a/tests/Units/LabelSpecialCharsTest.php b/tests/Units/LabelSpecialCharsTest.php index 0fd5e1c5..973f0536 100644 --- a/tests/Units/LabelSpecialCharsTest.php +++ b/tests/Units/LabelSpecialCharsTest.php @@ -232,4 +232,40 @@ public function testDropdownClassIsSafelyGeneratedWithSpecialCharsInLabel(string $this->assertTrue(class_exists($classname)); $this->assertSame($label, $classname::getTypeName()); } + + public function testTranslationPartialUpdateKeepsLabel(): void + { + $container = $this->createFieldContainer([ + 'label' => 'Label partial update ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + + $field = $this->createField([ + 'label' => "N° d'inventaire", + 'type' => 'text', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + ]); + + $translations = (new PluginFieldsLabelTranslation())->find([ + 'itemtype' => PluginFieldsField::class, + 'items_id' => $field->getID(), + ]); + $this->assertCount(1, $translations); + + // Update without 'label' key (e.g. language only, API partial update) + $translation = new PluginFieldsLabelTranslation(); + $this->assertTrue($translation->update([ + 'id' => array_key_first($translations), + 'language' => 'fr_FR', + ])); + + $this->assertTrue($translation->getFromDB(array_key_first($translations))); + $this->assertSame("N° d'inventaire", $translation->fields['label']); + } } \ No newline at end of file