From a9105d82ef8fcb8e23f74cc80d63dd13d061d503 Mon Sep 17 00:00:00 2001 From: Patrick Allwood Date: Wed, 18 Nov 2026 11:09:45 +0000 Subject: [PATCH 1/4] test: Tests covering identity token validation weakness --- .../Endpoints/EndSession/EndSessionTests.cs | 56 +++++++++++++++++++ .../Introspection/IntrospectionTests.cs | 28 ++++++++++ .../Endpoints/Introspection/Setup/Clients.cs | 8 +++ .../EndSessionRequestValidatorTests.cs | 28 ++++++++++ .../Validation/IdentityTokenValidation.cs | 25 +++++++++ 5 files changed, 145 insertions(+) diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs index 5d0fa7f18..ac6e67674 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs @@ -19,6 +19,7 @@ using Open.IdentityServer; using Open.IdentityServer.Utility; using Xunit; +using System.IdentityModel.Tokens.Jwt; using static Open.IdentityServer.IdentityServerConstants; using QueryHelpers = Microsoft.AspNetCore.WebUtilities.QueryHelpers; @@ -97,6 +98,23 @@ public EndSessionTests() _mockPipeline.IdentityScopes.AddRange([ new IdentityResources.OpenId() ]); + + _mockPipeline.Clients.Add(new Client + { + ClientId = "orders", + ClientSecrets = { new Secret("secret".Sha256()) }, + AllowedGrantTypes = GrantTypes.ClientCredentials, + AllowedScopes = { "orders.read" }, + AccessTokenType = AccessTokenType.Jwt, + PostLogoutRedirectUris = { "https://attacker.example/post-logout" } + }); + + _mockPipeline.ApiResources.Add(new ApiResource + { + Name = "orders", + Scopes = { "orders.read" } + }); + _mockPipeline.ApiScopes.Add(new ApiScope { Name = "orders.read" }); _mockPipeline.Initialize(); } @@ -600,4 +618,42 @@ await _mockPipeline.BrowserClient.GetAsync( _mockPipeline.BackChannelMessageHandler.InvokeWasCalled.Should().BeTrue(); } + + [Fact] + [Trait("Category", Category)] + public async Task client_credentials_access_token_for_client_id_collision_should_require_logout_prompt() + { + await _mockPipeline.LoginAsync("bob"); + + var tokenResponse = await _mockPipeline.BackChannelClient!.RequestClientCredentialsTokenAsync( + new ClientCredentialsTokenRequest + { + Address = IdentityServerPipeline.TokenEndpoint, + ClientId = "orders", + ClientSecret = "secret", + Scope = "orders.read" + }, + TestContext.Current.CancellationToken); + + tokenResponse.IsError.Should().BeFalse(); + tokenResponse.AccessToken.Should().NotBeNullOrEmpty(); + + var jwt = new JwtSecurityTokenHandler().ReadJwtToken(tokenResponse.AccessToken!); + jwt.Audiences.Should().Contain("orders"); + jwt.Header.Typ.Should().Be("at+jwt"); + jwt.Payload.ContainsKey(JwtClaimTypes.Subject).Should().BeFalse(); + jwt.Payload.ContainsKey(JwtClaimTypes.SessionId).Should().BeFalse(); + + var endSessionUrl = IdentityServerPipeline.EndSessionEndpoint + + "?id_token_hint=" + Uri.EscapeDataString(tokenResponse.AccessToken!) + + "&post_logout_redirect_uri=" + + Uri.EscapeDataString("https://attacker.example/post-logout"); + + await _mockPipeline.BrowserClient!.GetAsync(endSessionUrl, TestContext.Current.CancellationToken); + + _mockPipeline.LogoutRequest.Should().NotBeNull(); + _mockPipeline.LogoutRequest.ShowSignoutPrompt.Should().BeTrue(); + _mockPipeline.LogoutRequest.ClientId.Should().BeNull(); + _mockPipeline.LogoutRequest.PostLogoutRedirectUri.Should().BeNull(); + } } \ No newline at end of file diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs index 7d466ad0e..7b8e01c9a 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs @@ -443,4 +443,32 @@ public async Task Valid_token_with_invalid_scopes_should_fail() introspectionResponse.IsActive.Should().Be(false); introspectionResponse.IsError.Should().Be(false); } + + [Fact] + [Trait("Category", Category)] + public async Task public_client_without_api_secret_cannot_introspect_reference_token() + { + var tokenResponse = await _client.RequestClientCredentialsTokenAsync( + new ClientCredentialsTokenRequest + { + Address = TokenEndpoint, + ClientId = "public.client", + Scope = "api1" + }, + TestContext.Current.CancellationToken); + + tokenResponse.IsError.Should().BeFalse(); + tokenResponse.AccessToken.Should().NotBeNullOrEmpty(); + + var response = await _client.PostAsync( + IntrospectionEndpoint, + new FormUrlEncodedContent(new Dictionary + { + ["client_id"] = "public.client", + ["token"] = tokenResponse.AccessToken! + }), + TestContext.Current.CancellationToken); + + response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); + } } \ No newline at end of file diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs index 2c44e7e40..9f2787e13 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs @@ -60,6 +60,14 @@ public static IEnumerable Get() AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, AllowedScopes = { "api1", "api2", "api3-a", "api3-b" }, AccessTokenType = AccessTokenType.Reference + }, + new Client + { + ClientId = "public.client", + RequireClientSecret = false, + AllowedGrantTypes = GrantTypes.ClientCredentials, + AllowedScopes = { "api1" }, + AccessTokenType = AccessTokenType.Reference } }; } diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/EndSessionRequestValidation/EndSessionRequestValidatorTests.cs b/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/EndSessionRequestValidation/EndSessionRequestValidatorTests.cs index 7d01944b0..19ac4284d 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/EndSessionRequestValidation/EndSessionRequestValidatorTests.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/EndSessionRequestValidation/EndSessionRequestValidatorTests.cs @@ -1,4 +1,5 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Modified by Rock Solid Knowledge Ltd. Copyright in modifications 2026, Rock Solid Knowledge Ltd. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. @@ -232,4 +233,31 @@ public async Task ValidateCallbackAsync_WhenCalled_ShouldTraceTelemetry() TelemetryConstants.TraceCategories.Validation, _subject, "ValidateCallbackAsync")); _trace.Verify(t => t.Dispose(), Times.Once); } + + [Fact] + public async Task id_token_hint_without_subject_or_session_should_be_ignored_and_show_signout_prompt() + { + _stubTokenValidator.IdentityTokenValidationResult = new TokenValidationResult + { + IsError = false, + Claims = new Claim[] { }, + Client = new Client { ClientId = "client" } + }; + _stubRedirectUriValidator.IsPostLogoutRedirectUriValid = true; + + var parameters = new NameValueCollection + { + ["id_token_hint"] = "id_token", + ["post_logout_redirect_uri"] = "https://client/signout-callback" + }; + + var result = await _subject.ValidateAsync(parameters, _user); + + result.IsError.Should().BeFalse(); + result.ValidatedRequest.Client.Should().BeNull(); + result.ValidatedRequest.PostLogOutUri.Should().BeNull(); + + var logoutRequest = new LogoutRequest(null, new LogoutMessage(result.ValidatedRequest)); + logoutRequest.ShowSignoutPrompt.Should().BeTrue(); + } } \ No newline at end of file diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/IdentityTokenValidation.cs b/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/IdentityTokenValidation.cs index 764cd60e0..f24288420 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/IdentityTokenValidation.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.UnitTests/Validation/IdentityTokenValidation.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using AwesomeAssertions; using Moq; +using Open.IdentityServer; +using Open.IdentityServer.Models; using Open.IdentityServer.Services; using Open.IdentityServer.UnitTests.Validation.Setup; using Xunit; @@ -107,4 +109,27 @@ public async Task IdentityToken_ShouldTraceTelemetry() TelemetryConstants.TraceCategories.Validation, validator, "ValidateIdentityTokenAsync")); trace.Verify(t => t.Dispose(), Times.Once); } + + [Fact] + [Trait("Category", Category)] + public async Task Signed_AccessToken_with_ClientId_Audience_ShouldNotValidateAs_IdentityToken() + { + var client = new Client { ClientId = "roclient" }; + var accessToken = TokenFactory.CreateAccessToken(client, "valid", 600, "read"); + accessToken.Audiences.Clear(); + accessToken.Audiences.Add(client.ClientId); + + var creator = Factory.CreateDefaultTokenCreator(); + var jwt = await creator.CreateTokenAsync(accessToken); + new JwtSecurityTokenHandler().ReadJwtToken(jwt).Header.Typ.Should().Be("at+jwt"); + + var validator = Factory.CreateTokenValidator(); + var identityResult = await validator.ValidateIdentityTokenAsync(jwt); + + identityResult.IsError.Should().BeTrue(); + identityResult.Error.Should().Be(OidcConstants.ProtectedResourceErrors.InvalidToken); + + var accessResult = await validator.ValidateAccessTokenAsync(jwt); + accessResult.IsError.Should().BeFalse(); + } } \ No newline at end of file From 1f5b1ca9342e7b79cc119ec878e7e124e5b663c7 Mon Sep 17 00:00:00 2001 From: Patrick Allwood Date: Wed, 18 Nov 2026 13:52:55 +0000 Subject: [PATCH 2/4] fix: Validate id_token_hint typ and subject id --- .../Options/IdentityServerOptions.cs | 5 ++++ .../Default/EndSessionRequestValidator.cs | 30 ++++++++++++------- .../src/Validation/Default/TokenValidator.cs | 23 ++++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/Open.IdentityServer/src/Configuration/DependencyInjection/Options/IdentityServerOptions.cs b/src/Open.IdentityServer/src/Configuration/DependencyInjection/Options/IdentityServerOptions.cs index 603281e43..2dc5e2043 100644 --- a/src/Open.IdentityServer/src/Configuration/DependencyInjection/Options/IdentityServerOptions.cs +++ b/src/Open.IdentityServer/src/Configuration/DependencyInjection/Options/IdentityServerOptions.cs @@ -33,6 +33,11 @@ public class IdentityServerOptions /// The JWT typ value. /// public string AccessTokenJwtType { get; set; } = "at+jwt"; + + /// + /// Gets or sets the accepted JWT typ header values for identity tokens. + /// + public ICollection IdentityTokenJwtTypes { get; set; } = ["JWT"]; /// /// Emits an aud claim with the format issuer/resources. That's needed for some older access token validation plumbing. Defaults to false. diff --git a/src/Open.IdentityServer/src/Validation/Default/EndSessionRequestValidator.cs b/src/Open.IdentityServer/src/Validation/Default/EndSessionRequestValidator.cs index d22db434f..251a3c56d 100644 --- a/src/Open.IdentityServer/src/Validation/Default/EndSessionRequestValidator.cs +++ b/src/Open.IdentityServer/src/Validation/Default/EndSessionRequestValidator.cs @@ -122,18 +122,29 @@ public async Task ValidateAsync(NameValueCollection }; var idTokenHint = parameters.Get(OidcConstants.EndSessionRequest.IdTokenHint); + TokenValidationResult tokenValidationResult = null; + if (idTokenHint.IsPresent()) { - // validate id_token - no need to validate token life time - var tokenValidationResult = await TokenValidator.ValidateIdentityTokenAsync(idTokenHint, null, false); - if (tokenValidationResult.IsError) + var hintValidationResult = await TokenValidator.ValidateIdentityTokenAsync(idTokenHint, null, false); + var hasSubject = hintValidationResult.Claims? + .Any(c => c.Type == JwtClaimTypes.Subject && c.Value.IsPresent()) + ?? false; + + if (!hintValidationResult.IsError && hasSubject) + { + tokenValidationResult = hintValidationResult; + } + else { - return Invalid("Error validating id token hint", validatedRequest); + Logger.LogWarning("Ignoring id_token_hint that is invalid or has no subject claim."); } + } + if (tokenValidationResult != null) + { validatedRequest.Client = tokenValidationResult.Client; - - // validate sub claim against currently logged on user + var subClaim = tokenValidationResult.Claims.FirstOrDefault(c => c.Type == JwtClaimTypes.Subject); if (subClaim != null && isAuthenticated) { @@ -141,12 +152,12 @@ public async Task ValidateAsync(NameValueCollection { return Invalid("Current user does not match identity token", validatedRequest); } - + validatedRequest.Subject = subject; validatedRequest.SessionId = await UserSession.GetSessionIdAsync(); validatedRequest.ClientIds = await UserSession.GetClientListAsync(); } - + var redirectUri = parameters.Get(OidcConstants.EndSessionRequest.PostLogoutRedirectUri); if (redirectUri.IsPresent()) { @@ -159,7 +170,7 @@ public async Task ValidateAsync(NameValueCollection Logger.LogWarning("Invalid PostLogoutRedirectUri: {postLogoutRedirectUri}", redirectUri); } } - + if (validatedRequest.PostLogOutUri != null) { var state = parameters.Get(OidcConstants.EndSessionRequest.State); @@ -171,7 +182,6 @@ public async Task ValidateAsync(NameValueCollection } else { - // no id_token to authenticate the client, but we do have a user and a user session validatedRequest.Subject = subject; validatedRequest.SessionId = await UserSession.GetSessionIdAsync(); validatedRequest.ClientIds = await UserSession.GetClientListAsync(); diff --git a/src/Open.IdentityServer/src/Validation/Default/TokenValidator.cs b/src/Open.IdentityServer/src/Validation/Default/TokenValidator.cs index 1db2c76fc..d403398cf 100644 --- a/src/Open.IdentityServer/src/Validation/Default/TokenValidator.cs +++ b/src/Open.IdentityServer/src/Validation/Default/TokenValidator.cs @@ -99,7 +99,12 @@ public async Task ValidateIdentityTokenAsync(string token _logger.LogDebug("Client found: {clientId} / {clientName}", client.ClientId, client.ClientName); var keys = await _keys.GetValidationKeysAsync(); - var result = await ValidateJwtAsync(token, keys, audience: clientId, validateLifetime: validateLifetime); + var result = await ValidateJwtAsync( + token, + keys, + validateLifetime: validateLifetime, + audience: clientId, + expectedJwtTypes: _options.IdentityTokenJwtTypes ?? Array.Empty()); result.Client = client; @@ -248,7 +253,12 @@ public async Task ValidateAccessTokenAsync(string token, return customResult; } - private async Task ValidateJwtAsync(string jwt, IEnumerable validationKeys, bool validateLifetime = true, string audience = null) + private async Task ValidateJwtAsync( + string jwt, + IEnumerable validationKeys, + bool validateLifetime = true, + string audience = null, + IEnumerable expectedJwtTypes = null) { var handler = new JwtSecurityTokenHandler(); handler.InboundClaimTypeMap.Clear(); @@ -274,8 +284,15 @@ private async Task ValidateJwtAsync(string jwt, IEnumerab var id = handler.ValidateToken(jwt, parameters, out var securityToken); var jwtSecurityToken = securityToken as JwtSecurityToken; + if (expectedJwtTypes != null) + { + if (!expectedJwtTypes.Contains(jwtSecurityToken.Header.Typ, StringComparer.Ordinal)) + { + return Invalid(OidcConstants.ProtectedResourceErrors.InvalidToken); + } + } // if no audience is specified, we make at least sure that it is an access token - if (audience.IsMissing()) + else if (audience.IsMissing()) { if (_options.AccessTokenJwtType.IsPresent()) { From 2cef7d3760acd83a5eef37bdd8c8b927db1b0775 Mon Sep 17 00:00:00 2001 From: Patrick Allwood Date: Wed, 18 Nov 2026 14:46:09 +0000 Subject: [PATCH 3/4] chore: fix headers --- .../Endpoints/EndSession/EndSessionTests.cs | 1 + .../Endpoints/Introspection/IntrospectionTests.cs | 1 + .../Endpoints/Introspection/Setup/Clients.cs | 1 + 3 files changed, 3 insertions(+) diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs index ac6e67674..48a2f3658 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/EndSession/EndSessionTests.cs @@ -1,4 +1,5 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Modified by Rock Solid Knowledge Ltd. Copyright in modifications 2026, Rock Solid Knowledge Ltd. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. using System; diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs index 7b8e01c9a..7ca254837 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/IntrospectionTests.cs @@ -1,4 +1,5 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Modified by Rock Solid Knowledge Ltd. Copyright in modifications 2026, Rock Solid Knowledge Ltd. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. #nullable enable diff --git a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs index 9f2787e13..9854b54b9 100644 --- a/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs +++ b/src/Open.IdentityServer/test/Open.IdentityServer.IntegrationTests/Endpoints/Introspection/Setup/Clients.cs @@ -1,4 +1,5 @@ // Copyright (c) Brock Allen & Dominick Baier. All rights reserved. +// Modified by Rock Solid Knowledge Ltd. Copyright in modifications 2026, Rock Solid Knowledge Ltd. // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. From 605a461807f072bfd0ecb9213dce1ffa7d995204 Mon Sep 17 00:00:00 2001 From: Patrick Allwood Date: Wed, 18 Nov 2026 17:07:16 +0000 Subject: [PATCH 4/4] Document additional option --- docs/reference/options.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/reference/options.rst b/docs/reference/options.rst index 9dfb8e2ec..0bd141bdb 100644 --- a/docs/reference/options.rst +++ b/docs/reference/options.rst @@ -12,6 +12,9 @@ IdentityServer Options * ``AccessTokenJwtType`` Specifies the value used for the JWT typ header for access tokens (defaults to ``at+jwt``). +* ``IdentityTokenJwtTypes`` + Specifies the allowable JWT typ values during identity token validation. (defaults to ``JWT``). + * ``EmitScopesAsSpaceDelimitedStringInJwt`` Specifies whether scopes in JWTs are emitted as array or string