Code comment

This commit is contained in:
Laurent Destailleur 2026-06-09 10:51:43 +02:00
parent c7b0f1e10e
commit a911cc2299
3 changed files with 30 additions and 6 deletions

View file

@ -425,8 +425,8 @@ $dolibarr_cron_allow_cli='0';
// By default for security purpose, it is not possible to add or edit PHP dynamic content into a website (even if user has permission for this). To be able
// to edit PHP content in website page, you must first allow this globally, by setting this parameter to 1 or 2.
// 0 will disallow use of dynamic PHP inside the website features. Only HTML and js will be possible.
// 1 will allow custom PHP inside website features, but only if dangerous PHP system functions are disabled (using the PHP disable_functions parameter)
// 2 will allow custom PHP inside website features even if PHP remote execution features like "exec" are enabled (this may be dangerous if you don't have a RCE protection like SELinux or Apparmor).
// 1 will allow custom PHP inside website features, but only if all RCE PHP functions in the "exec" family are disabled (using the PHP disable_functions parameter)
// 2 will allow custom PHP inside website features, even if RCE PHP functions in the "exec" family are enabled (this may be dangerous if you don't have a RCE protection like SELinux or Apparmor).
// Default value: '0'
// Examples: '0', '1' or '2'
//

View file

@ -10158,6 +10158,27 @@ function dol_concat($text1, $text2)
return $text1.$text2;
}
/**
* Add a function to replace array_map with allowed callback
*
* @param callable $callback Function
* @param array<mixed,mixed> $array Array
* @return array<mixed,mixed> The array after the array_map
*/
function safeArrayMap(callable $callback, array $array)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException("Les callbacks sont désactivés.");
}
// Vérifie que $callback est une fonction "sûre" (ex: dans une liste blanche)
$allowed_callbacks = ['strtolower', 'strtoupper', 'intval'];
if (is_string($callback) && !in_array($callback, $allowed_callbacks, true)) {
throw new InvalidArgumentException("Callback non autorisé.");
}
return array_map($callback, $array);
}
/**
* Return array of possible common substitutions. This includes several families like: 'system', 'mycompany', 'object', 'objectamount', 'date', 'user'
*
@ -11597,6 +11618,7 @@ function dol_htmloutput_errors($mesgstring = '', $mesgarray = array(), $keepembe
/**
* Advanced sort array by the value of a given key, which produces ascending (default) or descending
* output and uses optionally natural case insensitive sorting (which can be optionally case sensitive as well).
* In dynamic code, this function is allowed when usort is not because it allows callable parameters.
*
* @phpstan-template T of array<string|int,mixed>
* @phan-template T of array<string|int,mixed>
@ -12213,7 +12235,7 @@ function dol_eval_new($s)
* '1' (most common use)=Accept only simple string with char 'a-z0-9\s^$_+-.*>&|=!?():"\',/@';',
* '2' (used for example for the compute property of extrafields)=Accept also '<[]'
* @return void|string Nothing or return result of eval (even if type can be int, it is safer to assume string and find all potential typing issues as abs(dol_eval(...)).
* If we return a string on error, it must start with 'Bad call of ...' or 'Bad string syntax to evaluate...' !!!
* If we return a string on error, it must start with 'Bad call of'... or 'Bad string syntax'... or 'Exception'... !!!
* @see verifCond(), checkPHPCode() to see sanitizing rules that should be very close.
* @phan-suppress PhanPluginUnsafeEval
*/
@ -12229,7 +12251,7 @@ function dol_eval_standard($s, $hideerrors = 1, $onlysimplestring = '1')
global $objectoffield; // To allow the use of $objectoffield in computed fields
global $object;
// Old variables (deprecated)
// Old variables (deprecated since v23)
if (getDolGlobalString('MAIN_ALLOW_OLD_VAR_OBJ_IN_DOL_EVAL')) {
global $obj; // To get $obj used into list when dol_eval() is used for computed fields and $obj is not yet $objectoffield
}
@ -12513,7 +12535,7 @@ function dol_eval_standard($s, $hideerrors = 1, $onlysimplestring = '1')
ob_start(); // An evaluation has no reason to output data
$isObBufferActive = true;
$tmps = $hideerrors ? @eval('return ' . $s . ';') : eval('return ' . $s . ';');
$tmpo = ob_get_clean();
$tmpo = ob_get_clean(); // This close the buffer
$isObBufferActive = false;
if ($tmpo) {
print 'Bad string syntax to evaluate. Some data were output when it should not when evaluating: ' . $s;
@ -12522,7 +12544,7 @@ function dol_eval_standard($s, $hideerrors = 1, $onlysimplestring = '1')
} catch (Error $e) {
if ($isObBufferActive) {
// Clean up buffer which was left behind due to exception.
$tmpo = ob_get_clean();
$tmpo = ob_get_clean(); // This close the buffer
$isObBufferActive = false;
}
$error = 'dol_eval try/catch error for string: ' . $s . ' - Error: ';

View file

@ -968,6 +968,8 @@ function showWebsiteTemplates(Website $website, int $refresh)
* Check that the new string $phpfullcodestring contains only php code (including <php tag)
* - Block if user has no permission to change PHP code.
* - Block also if bad code found in the new string.
* This does not check for evil callable function (like dol_eval_standard could do), because php concat should be allowed so obfuscation is always possible so
* detecting callable function can't be guaranteed. For this reason, application is protected by a global variable $dolibarr_website_allow_custom_php.
*
* @param string $phpfullcodestringold PHP old string (before the change). For example "<?php echo 'a' ?><php echo 'b' ?>"
* @param string $phpfullcodestring PHP new string. For example "<?php echo 'a' ?><php echo 'c' ?>"