X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FString.php;h=95eda6658d340e5bad3215a370b397249c0bbe93;hb=7138b10f7e376c96c28b057261394635021cd97b;hp=ef41bbf6163948d26a4f170d5b3e9c63b1c69b05;hpb=cc58f6c5756ba74b4a914e21bb011332d5d79c80;p=civicrm-core.git diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index ef41bbf616..95eda6658d 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -15,6 +15,9 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use function xKerman\Restricted\unserialize; +use xKerman\Restricted\UnserializeFailedException; + require_once 'HTML/QuickForm/Rule/Email.php'; /** @@ -920,4 +923,33 @@ class CRM_Utils_String { return array_values(array_unique($result)); } + /** + * Safely unserialize a string of scalar or array values (but not objects!) + * + * Use `xkerman/restricted-unserialize` to unserialize strings using PHP's + * serialization format. `restricted-unserialize` works like PHP's built-in + * `unserialize` function except that it does not deserialize object instances, + * making it immune to PHP Object Injection {@see https://www.owasp.org/index.php/PHP_Object_Injection} + * vulnerabilities. + * + * Note: When dealing with user inputs, it is generally recommended to use + * safe, standard data interchange formats such as JSON rather than PHP's + * serialization format when dealing with user input. + * + * @param string|NULL $string + * + * @return mixed + */ + public static function unserialize($string) { + if (!is_string($string)) { + return FALSE; + } + try { + return unserialize($string); + } + catch (UnserializeFailedException $e) { + return FALSE; + } + } + }