Skip to content

Commit 7f27bab

Browse files
authored
feat: verify existence of subject when verifying JWT (#474)
* Add check to verify sub is set * Update CHANGELOG.md * Add tests
1 parent 6496752 commit 7f27bab

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Stop adding ?schema=openid to userinfo endpoint URL. #449
1111

12+
### Fixed
13+
- Check existence of subject when verifying JWT #474
14+
1215
## [1.0.1] - 2024-09-13
1316

1417
### Fixed

src/OpenIDConnectClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,11 @@ protected function validateIssuer(string $iss): bool
11911191
*/
11921192
protected function verifyJWTClaims($claims, ?string $accessToken = null): bool
11931193
{
1194+
// Verify that sub is set
1195+
if (!isset($claims->sub)) {
1196+
return false;
1197+
}
1198+
11941199
if(isset($claims->at_hash, $accessToken)) {
11951200
if(isset($this->getIdTokenHeader()->alg) && $this->getIdTokenHeader()->alg !== 'none') {
11961201
$bit = substr($this->getIdTokenHeader()->alg, 2, 3);

tests/OpenIDConnectClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ public function getIdTokenPayload()
4848
'sub' => 'sub',
4949
]);
5050
self::assertFalse($valid);
51+
52+
# sub not matching
53+
$valid = $client->testVerifyJWTClaims((object)[
54+
'aud' => ['client-id'],
55+
'iss' => 'issuer',
56+
'sub' => 'sub-invalid',
57+
]);
58+
self::assertFalse($valid);
59+
60+
# sub missing
61+
$valid = $client->testVerifyJWTClaims((object)[
62+
'aud' => ['client-id'],
63+
'iss' => 'issuer',
64+
]);
65+
self::assertFalse($valid);
5166
}
5267
public function testJWTDecode()
5368
{

0 commit comments

Comments
 (0)