Skip to content

Commit 3948117

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 eeea7e9 commit 3948117

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
@@ -503,15 +503,23 @@ public function code(string $state = '', string $code = '', string $scope = '',
503503
$this->logger->debug('Received code response: ' . json_encode($data, JSON_THROW_ON_ERROR));
504504
$this->eventDispatcher->dispatchTyped(new TokenObtainedEvent($data, $provider, $discovery));
505505

506-
// TODO: proper error handling
507506
$idTokenRaw = $data['id_token'];
508507
if ($usePrivateKeyJwt) {
509-
// we could check the header there
510508
// if kid is our private JWK, we have a JWE to decrypt
511509
// if typ=JWT, we have a classic JWT to decode
512510
$jwtParts = explode('.', $idTokenRaw, 3);
513-
$jwtHeader = json_decode(JWT::urlsafeB64Decode($jwtParts[0]), true);
514-
$this->logger->warning('JWT HEADER', ['jwt_header' => $jwtHeader]);
511+
try {
512+
$jwtHeader = json_decode(JWT::urlsafeB64Decode($jwtParts[0]), true, flags: JSON_THROW_ON_ERROR);
513+
} catch (\JsonException $e) {
514+
$this->logger->error('Malformed JWT id token header', ['exception' => $e]);
515+
$message = $this->l10n->t('Failed to decode JWT id token header');
516+
return $this->buildErrorTemplateResponse($message, Http::STATUS_BAD_REQUEST, throttle: false);
517+
} catch (\Exception|\Throwable $e) {
518+
$this->logger->error('Impossible to decode JWT id token header', ['exception' => $e]);
519+
$message = $this->l10n->t('Failed to decode JWT id token header');
520+
return $this->buildErrorTemplateResponse($message, Http::STATUS_BAD_REQUEST, throttle: false);
521+
}
522+
$this->logger->debug('JWT HEADER', ['jwt_header' => $jwtHeader]);
515523
if (isset($jwtHeader['typ']) && $jwtHeader['typ'] === 'JWT') {
516524
// we have a JWT, do nothing
517525
} elseif (isset($jwtHeader['cty']) && $jwtHeader['cty'] === 'JWT') {

0 commit comments

Comments
 (0)