Skip to content

A free open source IT asset/license management system

security: match only the configured OIDC username claim (no silent fallback)

Rod Christiansenadff156794eb
diff --git a/app/Services/Oidc/OidcUserResolver.php b/app/Services/Oidc/OidcUserResolver.php
index 360508ac12..eb1fd53282 100644
--- a/app/Services/Oidc/OidcUserResolver.php
+++ b/app/Services/Oidc/OidcUserResolver.php
@@ -46,15 +46,15 @@ class OidcUserResolver
protected function usernameFromClaims(array $claims): ?string
{
+ // Match ONLY the admin-configured claim. Falling back to other claims
+ // (upn/email) would let a token that lacks the trusted claim authenticate
+ // via a different, possibly less-trustworthy or differently-scoped claim
+ // -- an account-confusion / bypass vector. Provider differences belong in
+ // config (OIDC_API_USERNAME_CLAIM), not a silent fallback. Absent the
+ // configured claim, reject.
$claimName = config('oidc.username_claim', 'preferred_username');
- foreach ([$claimName, 'preferred_username', 'upn', 'email'] as $candidate) {
- if (! empty($claims[$candidate])) {
- return $claims[$candidate];
- }
- }
-
- return null;
+ return ! empty($claims[$claimName]) ? (string) $claims[$claimName] : null;
}
protected function provision(array $claims, string $username): ?User
diff --git a/tests/Feature/Authentication/OidcApiAuthTest.php b/tests/Feature/Authentication/OidcApiAuthTest.php
index 5dc52dcc1c..53b35ef8e8 100644
--- a/tests/Feature/Authentication/OidcApiAuthTest.php
+++ b/tests/Feature/Authentication/OidcApiAuthTest.php
@@ -163,6 +163,20 @@ class OidcApiAuthTest extends TestCase
->assertUnauthorized();
}
+ public function test_absent_configured_claim_is_not_matched_by_a_different_claim()
+ {
+ // username_claim is preferred_username. A token that lacks it must NOT
+ // authenticate by silently falling back to another claim (e.g. email),
+ // even when that other claim matches a real username.
+ User::factory()->create(['username' => '[email protected]', 'activated' => 1]);
+
+ $token = $this->token(['preferred_username' => null, 'email' => '[email protected]']);
+
+ $this->withHeaders($this->bearer($token))
+ ->getJson('/_test/oidc-whoami')
+ ->assertUnauthorized();
+ }
+
public function test_passport_token_still_authenticates_via_multiguard()
{
// The oidc guard must not break the existing Passport path: a route