Fix #GHSA-5cfw-655w-vqp8

This commit is contained in:
Laurent Destailleur 2026-03-29 20:31:54 +02:00
parent 0523b3ecf9
commit ee8ded7c5e
2 changed files with 51 additions and 11 deletions

View file

@ -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;
}
}
}

View file

@ -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";