Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security] [Core] Harden StringUtils against mb.func_overload #13984

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/Symfony/Component/Security/Core/Util/StringUtils.php
Expand Up @@ -45,8 +45,8 @@ public static function equals($knownString, $userInput)
return hash_equals($knownString, $userInput);
}

$knownLen = strlen($knownString);
$userLen = strlen($userInput);
$knownLen = self::safeStrlen($knownString);
$userLen = self::safeStrlen($userInput);

// Extend the known string to avoid uninitialized string offsets
$knownString .= $userInput;
Expand All @@ -63,4 +63,26 @@ public static function equals($knownString, $userInput)
// They are only identical strings if $result is exactly 0...
return 0 === $result;
}

/**
* Return the number of bytes in a string
*
* @param string $string The string whose length we wish to obtain
* @return int
*/
public static function safeStrlen($string)
{
// Premature optimization
// Since this cannot be changed at runtime, we can cache it
static $func_exists = null;
if ($func_exists === null) {
$func_exists = function_exists('mb_strlen');
}

if ($func_exists) {
return mb_strlen($string, '8bit');
}

return strlen($string);
}
}