fix: refuse a token whose header makes php-jwt throw TypeError

php-jwt 7 passes the header kid to a ?string parameter, so a token with a
non-string kid throws TypeError before the signature is checked. TypeError
is not an Exception, so validateToken() let it through and the request got a
500. Catch Throwable around JWT::decode() so it is refused like any other
bad token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Isaac ConnorandClaude Opus 5.5 committed 2026-11-10 08:11:42 -05:00
1 parent eae9e851ba
commit c3cb7a15dc
2 files changed
+32 -1

No files matched your search

+30
View File
@@ -52,6 +52,36 @@ $none = rtrim(strtr(base64_encode('{"alg":"none","typ":"JWT"}'), '+/', '-_'), '=
rtrim(strtr(base64_encode('{"user":"admin","type":"access"}'), '+/', '-_'), '=').'.';
check('alg none is refused', is_string(decodes($none, $secret)), true);
// php-jwt 7 passes the header's kid to a ?string parameter, so a kid that is not a
// string throws TypeError, which is not an Exception, before the signature is checked.
// validateToken() must turn that into a refusal, not a 500.
$b64 = function ($s) { return rtrim(strtr(base64_encode($s), '+/', '-_'), '='); };
$bad_kid = $b64('{"alg":"HS256","typ":"JWT","kid":[]}').'.'.$b64('{"user":"admin","type":"access"}').'.x';
try {
JWT::decode($bad_kid, new Key($secret, 'HS256'));
$thrown = 'nothing';
} catch (Throwable $e) {
$thrown = get_class($e);
}
check('a non-string kid makes php-jwt throw TypeError', $thrown, 'TypeError');
eval('namespace ZM; function Error($m) {}');
define('ZM_AUTH_HASH_SECRET', $secret);
define('ZM_AUTH_HASH_SECRET_MIN_LENGTH', 32);
function authHashSecretIsSet() { return true; }
if (!preg_match('/^function validateToken\(.*?^}$/ms', file_get_contents(__DIR__.'/../../web/includes/auth.php'), $m)) {
echo "FAIL validateToken not found in auth.php\n";
exit(1);
}
eval('use Firebase\JWT\JWT; use Firebase\JWT\Key; '.$m[0]);
try {
$result = validateToken($bad_kid);
$result = $result[0];
} catch (Throwable $e) {
$result = get_class($e);
}
check('validateToken() refuses a token with a non-string kid', $result, false);
// Every decode in the web code and these tests must use the Key form.
$v5_calls = array();
foreach (array_merge(array('web/includes/auth.php', 'web/api/app/Controller/HostController.php'),
+2 -1
View File
@@ -352,7 +352,8 @@ function validateToken($token, $allowed_token_type='access') {
//if (ZM_AUTH_HASH_IPS) $key .= $_SERVER['REMOTE_ADDR'];
try {
$decoded_token = JWT::decode($token, new Key($key, 'HS256'));
} catch (Exception $e) {
} catch (Throwable $e) {
// Throwable, not Exception: php-jwt throws TypeError on a malformed header (a non-string kid).
ZM\Error("Unable to authenticate user. error decoding JWT token:".$e->getMessage());
return array(false, $e->getMessage());
}