From ee8ded7c5e2445696f1002d0dc50af07e53ba655 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 29 Mar 2026 20:31:54 +0200 Subject: [PATCH] Fix #GHSA-5cfw-655w-vqp8 --- htdocs/core/lib/functions.lib.php | 11 +++++-- test/phpunit/SecurityTest.php | 51 ++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index e4d610ce32e..c208581a422 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -12302,9 +12302,14 @@ function dol_eval_standard($s, $hideerrors = 1, $onlysimplestring = '1') } } } else { - if ($reg[1] == 'ReflectionFunction') { - dol_syslog('Bad string syntax to evaluate: Class ReflectionFunction is not allowed. ' . $s, LOG_WARNING); - return 'Bad string syntax to evaluate. Class ReflectionFunction is not allowed. ' . $s; + if (!class_exists($reg[1])) { + dol_syslog('Bad string syntax to evaluate: Class "'.$reg[1].'" does not exist. ' . $s, LOG_WARNING); + return 'Bad string syntax to evaluate. Class "'.$reg[1].'" does not exist. ' . $s; + } + $parents = class_parents($reg[1]); // Get list of parent classes of class we want to check + if (!in_array('CommonObject', $parents)) { // Only classes that inherit CommonObject are ok. This forbid dangerous classes like ReflectionFunction, SplFileObject, ... + dol_syslog('Bad string syntax to evaluate: Class "'.$reg[1].'" is not allowed because only classes extended CommonObject can be used in dynamic evaluation. ' . $s, LOG_WARNING); + return 'Bad string syntax to evaluate. Class "'.$reg[1].'" is not allowed because only classes extended CommonObject can be used in dynamic evaluation. ' . $s; } } } diff --git a/test/phpunit/SecurityTest.php b/test/phpunit/SecurityTest.php index b9718530281..f8d152e2b09 100644 --- a/test/phpunit/SecurityTest.php +++ b/test/phpunit/SecurityTest.php @@ -653,10 +653,39 @@ class SecurityTest extends CommonClassTest print "result2 = ".$result."\n"; $this->assertFalse($result); - $s = '((($var1 = new ClassThatDoesNotExists($db)) && ($var1->fetchNoCompute($objectoffield->fk_product) > 0)) ? \'1\' : \'0\')'; - $result3a = dol_eval($s, 1, 1, '2'); - print "result3a = ".$result3a."\n"; - $this->assertStringContainsString('Exception during evaluation: '.$s, $result3a); + if (empty($dolibarr_main_restrict_eval_methods)) { // Old mode + $s = '(($var1 = ne SyntaxErrorOnNew($db)))'; + $result3a = dol_eval($s, 1, 1, '2'); + print "result3a = ".$result3a."\n"; + $this->assertStringContainsString('Exception during evaluation', $result3a, 'The string was not detected as evil : '.$s); + + $s = '((($var1 = new ClassThatDoesNotExists($db)) && ($var1->fetchNoCompute($objectoffield->fk_product) > 0)) ? \'1\' : \'0\')'; + $result3c = dol_eval($s, 1, 1, '2'); + print "result3c = ".$result3c."\n"; + $this->assertStringContainsString('Exception during evaluation', $result3c, 'The string was not detected as evil : '.$s); + + $s = '((($var1 = new SimpleXMLElement()) ? \'1\' : \'0\')'; + $result3d= dol_eval($s, 1, 1, '2'); + print "result3d = ".$result."\n"; + $this->assertStringContainsString('Exception during evaluation', $result3d, 'The string was not detected as evil : '.$s); + } else { // New mode for v23+ + $s = '(($var1 = ne SyntaxErrorOnNew($db)))'; + $result3a = dol_eval($s, 1, 1, '2'); + print "result3a = ".$result3a."\n"; + $this->assertStringContainsString('Bad string syntax to evaluate.', $result3a, 'The string was not detected as evil'); + + $s = '((($var1 = new ClassThatDoesNotExists($db)) && ($var1->fetchNoCompute($objectoffield->fk_product) > 0)) ? \'1\' : \'0\')'; + $result3c = dol_eval($s, 1, 1, '2'); + print "result3c = ".$result3c."\n"; + $this->assertStringContainsString('Bad string syntax to evaluate', $result3c, 'The string was not detected as evil'); + + $s = '((($var1 = new SimpleXMLElement()) ? \'1\' : \'0\')'; + $result3d= dol_eval($s, 1, 1, '2'); + print "result3d = ".$result."\n"; + $this->assertStringContainsString('Bad string syntax to evaluate', $result3d, 'The string was not detected as evil'); + } + + // This next one are okfor syntax and allowance $s = '((($var1 = new Project($db)) && ($var1->fetchNoCompute($objectoffield->fk_product) > 0)) ? \'1\' : \'0\')'; $result3b = dol_eval($s, 1, 1, '2'); @@ -673,10 +702,6 @@ class SecurityTest extends CommonClassTest print "result4 = ".$result."\n"; $this->assertEquals('Parent project not found', $result, 'Test 4'); - $result = dol_eval('1==\x01', 1, 0); // Check that we can't make dol_eval on string containing \ char. - print "result5 = ".$result."\n"; - $this->assertStringContainsString('Bad string syntax to evaluate (found chars that are not chars for a simple one line clean eval string)', $result); - $s = '4 < 5'; $result = (string) dol_eval($s, 1, 1, '2'); print "result6 = ".$result."\n"; @@ -707,11 +732,21 @@ class SecurityTest extends CommonClassTest print "result = ".$result."\n"; $this->assertStringContainsString('Bad string syntax to evaluate', $result, 'Test 4 < 5 - The string was not detected as evil'); + $s = '1==\x01'; + $result = dol_eval($s, 1, 1, '1'); // Check that we can't make dol_eval on string containing \ char. + print "result5 = ".$result."\n"; + $this->assertStringContainsString('Bad string syntax to evaluate (found chars that are not chars for a simple one line clean eval string)', $result); + $s = 'new abc->invoke(\'whoami\')'; $result = (string) dol_eval($s, 1, 1, '2'); print "result = ".$result."\n"; $this->assertStringContainsString('Bad string syntax to evaluate', $result, 'The string was not detected as evil'); + /* $s = 'new ReflectionFunction(\'abc\')'; + $result = (string) dol_eval($s, 1, 1, '2'); + print "result = ".$result."\n"; + $this->assertStringContainsString('Bad string syntax to evaluate', $result, 'The string was not detected as evil'); + */ $s = 'new ReflectionFunction(\'abc\')'; $result = (string) dol_eval($s, 1, 1, '2'); print "result = ".$result."\n";