Skip to content

Commit 485d4cb

Browse files
committed
Harden against mbstring.func_overload edge-cases
1 parent 62345ab commit 485d4cb

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Authentication/JWT.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ public static function verify($msg, $signature, $key, $method = 'HS256')
192192
case 'hash_hmac':
193193
default:
194194
$hash = hash_hmac($algo, $msg, $key, true);
195-
$len = min(strlen($signature), strlen($hash));
195+
$len = min(self::safeStrlen($signature), self::safeStrlen($hash));
196196

197197
$status = 0;
198198
for ($i = 0; $i < $len; $i++) {
199199
$status |= (ord($signature[$i]) ^ ord($hash[$i]));
200200
}
201-
$status |= (strlen($signature) ^ strlen($hash));
201+
$status |= (self::safeStrlen($signature) ^ self::safeStrlen($hash));
202202

203203
return ($status === 0);
204204
}
@@ -307,6 +307,20 @@ private static function handleJsonError($errno)
307307
);
308308
}
309309

310+
/**
311+
* Get the number of bytes in cryptographic strings.
312+
*
313+
* @param string
314+
* @return int
315+
*/
316+
private static function safeStrlen($str)
317+
{
318+
if (function_exists('mb_strlen')) {
319+
return mb_strlen($str, '8bit');
320+
}
321+
return strlen($str);
322+
}
323+
310324
/**
311325
* Set the only allowed method for this server.
312326
*

0 commit comments

Comments
 (0)