Skip to content

Commit 1b25c5c

Browse files
committed
handle errors when parsing JWT header at the end of the code flow
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 2a7590c commit 1b25c5c

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

lib/Controller/LoginController.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,23 @@ public function code(string $state = '', string $code = '', string $scope = '',
471471
$this->logger->warning('Received code response: ' . json_encode($data, JSON_THROW_ON_ERROR));
472472
$this->eventDispatcher->dispatchTyped(new TokenObtainedEvent($data, $provider, $discovery));
473473

474-
// TODO: proper error handling
475474
$idTokenRaw = $data['id_token'];
476475
if ($usePrivateKeyJwt) {
477-
// we could check the header there
478476
// if kid is our private JWK, we have a JWE to decrypt
479477
// if typ=JWT, we have a classic JWT to decode
480478
$jwtParts = explode('.', $idTokenRaw, 3);
481-
$jwtHeader = json_decode(JWT::urlsafeB64Decode($jwtParts[0]), true);
482-
$this->logger->warning('JWT HEADER', ['jwt_header' => $jwtHeader]);
479+
try {
480+
$jwtHeader = json_decode(JWT::urlsafeB64Decode($jwtParts[0]), true, flags: JSON_THROW_ON_ERROR);
481+
} catch (\JsonException $e) {
482+
$this->logger->error('Malformed JWT id token header', ['exception' => $e]);
483+
$message = $this->l10n->t('Failed to decode JWT id token header');
484+
return $this->buildErrorTemplateResponse($message, Http::STATUS_BAD_REQUEST, throttle: false);
485+
} catch (\Exception|\Throwable $e) {
486+
$this->logger->error('Impossible to decode JWT id token header', ['exception' => $e]);
487+
$message = $this->l10n->t('Failed to decode JWT id token header');
488+
return $this->buildErrorTemplateResponse($message, Http::STATUS_BAD_REQUEST, throttle: false);
489+
}
490+
$this->logger->debug('JWT HEADER', ['jwt_header' => $jwtHeader]);
483491
if (isset($jwtHeader['typ']) && $jwtHeader['typ'] === 'JWT') {
484492
// we have a JWT, do nothing
485493
} elseif (isset($jwtHeader['cty']) && $jwtHeader['cty'] === 'JWT') {

0 commit comments

Comments
 (0)