From f18c8a788780ecb93b41215f322ff6dd369bf49e Mon Sep 17 00:00:00 2001 From: RomainLvr Date: Fri, 26 Jun 2026 14:20:03 +0200 Subject: [PATCH] Fix - Prevent TypeError in CheckboxesField::regex() when stored value is invalid JSON --- inc/field/checkboxesfield.class.php | 3 +- inc/field/ldapselectfield.class.php | 2 +- .../Formcreator/Field/CheckboxesField.php | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/inc/field/checkboxesfield.class.php b/inc/field/checkboxesfield.class.php index fb44ba339..a3e784fe6 100644 --- a/inc/field/checkboxesfield.class.php +++ b/inc/field/checkboxesfield.class.php @@ -141,9 +141,10 @@ public function serializeValue(PluginFormcreatorFormAnswer $formanswer): string } public function deserializeValue($value) { - $this->value = ($value !== null && $value !== '') + $decoded = ($value !== null && $value !== '') ? json_decode($value) : []; + $this->value = is_array($decoded) ? $decoded : []; } public function getValueForDesign(): string { diff --git a/inc/field/ldapselectfield.class.php b/inc/field/ldapselectfield.class.php index a34d7a84f..285c43373 100644 --- a/inc/field/ldapselectfield.class.php +++ b/inc/field/ldapselectfield.class.php @@ -221,7 +221,7 @@ public function lessThan($value): bool { } public function regex($value): bool { - return (preg_grep($value, $this->value)) ? true : false; + return preg_match($value, $this->value) ? true : false; } public function isPublicFormCompatible(): bool { diff --git a/tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php b/tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php index cd1080797..4b591d88a 100644 --- a/tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php +++ b/tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php @@ -602,6 +602,47 @@ public function testCanRequire() { $this->boolean($output)->isTrue(); } + public function providerRegex() { + yield 'valid json value matches regex' => [ + 'value' => '["foo","bar"]', + 'pattern' => '/foo/', + 'expected' => true, + ]; + yield 'valid json value does not match regex' => [ + 'value' => '["foo","bar"]', + 'pattern' => '/baz/', + 'expected' => false, + ]; + yield 'corrupted json value does not throw and returns false' => [ + 'value' => 'not_valid_json', + 'pattern' => '/foo/', + 'expected' => false, + ]; + yield 'null-decoded json value does not throw and returns false' => [ + 'value' => 'null', + 'pattern' => '/foo/', + 'expected' => false, + ]; + } + + /** + * @dataProvider providerRegex + */ + public function testRegex($value, $pattern, $expected) { + $instance = $this->newTestedInstance($this->getQuestion([ + 'fieldtype' => 'checkboxes', + 'values' => implode('\r\n', ['foo', 'bar']), + '_parameters' => [ + 'checkboxes' => [ + 'range' => ['range_min' => '', 'range_max' => ''], + ], + ], + ])); + $instance->deserializeValue($value); + $output = $instance->regex($pattern); + $this->boolean($output)->isEqualTo($expected); + } + public function providerGetValueForApi() { return [ [