-
Notifications
You must be signed in to change notification settings - Fork 245
Fix certificate reload logic to only trigger on certificate-specific errors #3653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
3
commits into
master
Choose a base branch
from
copilot/fix-certificate-reload-logic
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -888,13 +888,31 @@ public async Task RemoveAccountAsync( | |
|
|
||
| private bool IsInvalidClientCertificateOrSignedAssertionError(MsalServiceException exMsal) | ||
| { | ||
| return !_retryClientCertificate && | ||
| string.Equals(exMsal.ErrorCode, Constants.InvalidClient, StringComparison.OrdinalIgnoreCase) && | ||
| !exMsal.ResponseBody.Contains("AADSTS7000215" // No retry when wrong client secret. | ||
| if (_retryClientCertificate || | ||
| !string.Equals(exMsal.ErrorCode, Constants.InvalidClient, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Only retry for certificate-related or signed assertion-related errors. | ||
| // Check for specific error codes that indicate certificate/assertion issues. | ||
| string responseBody = exMsal.ResponseBody; | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| , StringComparison.OrdinalIgnoreCase | ||
| return responseBody.Contains(Constants.InvalidKeyError, StringComparison.OrdinalIgnoreCase) || | ||
jmprieur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| responseBody.Contains(Constants.SignedAssertionInvalidTimeRange, StringComparison.OrdinalIgnoreCase) || | ||
jmprieur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| responseBody.Contains(Constants.CertificateHasBeenRevoked, StringComparison.OrdinalIgnoreCase) || | ||
| responseBody.Contains(Constants.CertificateIsOutsideValidityWindow, StringComparison.OrdinalIgnoreCase) || | ||
| responseBody.Contains(Constants.CertificateNotWithinValidityPeriod, StringComparison.OrdinalIgnoreCase) || | ||
| responseBody.Contains(Constants.CertificateWasRevoked, StringComparison.OrdinalIgnoreCase); | ||
| #else | ||
| return responseBody.Contains(Constants.InvalidKeyError) || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems error prone, if we add a new error code we need to remember to put it in multiple places. Perhaps a dictionary should be created above and referred to? |
||
| responseBody.Contains(Constants.SignedAssertionInvalidTimeRange) || | ||
| responseBody.Contains(Constants.CertificateHasBeenRevoked) || | ||
| responseBody.Contains(Constants.CertificateIsOutsideValidityWindow) || | ||
| responseBody.Contains(Constants.CertificateNotWithinValidityPeriod) || | ||
| responseBody.Contains(Constants.CertificateWasRevoked); | ||
| #endif | ||
| ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
218 changes: 218 additions & 0 deletions
218
tests/Microsoft.Identity.Web.Test/CertificateReloadLogicTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Reflection; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Identity.Abstractions; | ||
| using Microsoft.Identity.Client; | ||
| using Microsoft.Identity.Web.Test.Common; | ||
| using Microsoft.Identity.Web.TestOnly; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Identity.Web.Test | ||
| { | ||
| /// <summary> | ||
| /// Tests for the certificate reload logic to ensure it only triggers on certificate-related errors. | ||
| /// This addresses the regression from PR #3430 where reload was triggered on all invalid_client errors. | ||
| /// </summary> | ||
| [Collection(nameof(TokenAcquirerFactorySingletonProtection))] | ||
| public class CertificateReloadLogicTests | ||
| { | ||
| private const string InvalidClientErrorCode = "invalid_client"; | ||
|
|
||
| [Theory] | ||
| [InlineData("AADSTS700027", true)] // InvalidKeyError | ||
| [InlineData("AADSTS700024", true)] // SignedAssertionInvalidTimeRange | ||
| [InlineData("AADSTS7000214", true)] // CertificateHasBeenRevoked | ||
| [InlineData("AADSTS1000502", true)] // CertificateIsOutsideValidityWindow | ||
| [InlineData("AADSTS7000274", true)] // CertificateNotWithinValidityPeriod | ||
| [InlineData("AADSTS7000277", true)] // CertificateWasRevoked | ||
| [InlineData("AADSTS7000215", false)] // Invalid client secret - should NOT trigger reload | ||
| [InlineData("AADSTS700016", false)] // Application not found - should NOT trigger reload | ||
| [InlineData("AADSTS7000222", false)] // Invalid client secret (expired) - should NOT trigger reload | ||
| [InlineData("AADSTS50011", false)] // Invalid reply address - should NOT trigger reload | ||
| [InlineData("AADSTS50012", false)] // Invalid client credentials - should NOT trigger reload | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_ReturnsTrueOnlyForCertificateErrors( | ||
| string errorCode, | ||
| bool shouldTriggerReload) | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
| var responseBody = $"{{\"error\":\"invalid_client\",\"error_description\":\"Error {errorCode}: Test error\"}}"; | ||
| var exception = CreateMsalServiceException(InvalidClientErrorCode, responseBody); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.Equal(shouldTriggerReload, result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_ReturnsFalseWhenErrorCodeIsNotInvalidClient() | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
| var responseBody = "{\"error\":\"unauthorized_client\",\"error_description\":\"Test error\"}"; | ||
| var exception = CreateMsalServiceException("unauthorized_client", responseBody); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Should not trigger reload for non-invalid_client errors"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_ReturnsFalseWhenRetryAlreadyInProgress() | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
|
|
||
| // Set _retryClientCertificate to true to simulate retry in progress | ||
| var retryField = typeof(TokenAcquisition).GetField("_retryClientCertificate", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
| retryField!.SetValue(tokenAcquisition, true); | ||
|
|
||
| var responseBody = $"{{\"error\":\"invalid_client\",\"error_description\":\"Error {Constants.InvalidKeyError}: Test error\"}}"; | ||
| var exception = CreateMsalServiceException(InvalidClientErrorCode, responseBody); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Should not trigger reload when retry is already in progress"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_ReturnsFalseForEmptyResponseBody() | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
| var exception = CreateMsalServiceException(InvalidClientErrorCode, string.Empty); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Should not trigger reload when response body is empty"); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("AADSTS700027")] // Case sensitive check - should still work | ||
| [InlineData("aadsts700027")] // Lowercase | ||
| [InlineData("AaDsTs700027")] // Mixed case | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_IsCaseInsensitive(string errorCodeCase) | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
| var responseBody = $"{{\"error\":\"invalid_client\",\"error_description\":\"Error {errorCodeCase}: Test error\"}}"; | ||
| var exception = CreateMsalServiceException(InvalidClientErrorCode, responseBody); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.True(result, $"Should trigger reload regardless of case: {errorCodeCase}"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsInvalidClientCertificateOrSignedAssertionError_WorksWithMultipleErrorCodesInResponse() | ||
| { | ||
| // Arrange | ||
| var tokenAcquisition = CreateTokenAcquisition(); | ||
| // Response might contain multiple error codes or descriptions | ||
| var responseBody = $"{{\"error\":\"invalid_client\",\"error_description\":\"Error {Constants.CertificateHasBeenRevoked}: Certificate has been revoked. Also note AADSTS7000215 in logs.\"}}"; | ||
| var exception = CreateMsalServiceException(InvalidClientErrorCode, responseBody); | ||
|
|
||
| // Act | ||
| bool result = InvokeIsInvalidClientCertificateOrSignedAssertionError(tokenAcquisition, exception); | ||
|
|
||
| // Assert | ||
| Assert.True(result, "Should trigger reload when certificate error code is present, even if other codes are also mentioned"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a TokenAcquisition instance for testing. | ||
| /// </summary> | ||
| private TokenAcquisition CreateTokenAcquisition() | ||
| { | ||
| TokenAcquirerFactoryTesting.ResetTokenAcquirerFactoryInTest(); | ||
| var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance(); | ||
| tokenAcquirerFactory.Services.Configure<MicrosoftIdentityApplicationOptions>(options => | ||
| { | ||
| options.Instance = "https://login.microsoftonline.com/"; | ||
| options.TenantId = "f645ad92-e38d-4d1a-b510-d1b09a74a8ca"; | ||
| options.ClientId = "idu773ld-e38d-jud3-45lk-d1b09a74a8ca"; | ||
| options.ClientCredentials = [new CredentialDescription() | ||
| { | ||
| SourceType = CredentialSource.ClientSecret, | ||
| ClientSecret = "someSecret" | ||
| }]; | ||
| }); | ||
|
|
||
| var serviceProvider = tokenAcquirerFactory.Build(); | ||
|
|
||
| // Get the TokenAcquisition instance from the service provider | ||
| var tokenAcquisition = serviceProvider.GetService(typeof(ITokenAcquisitionInternal)) as TokenAcquisition; | ||
|
|
||
| if (tokenAcquisition == null) | ||
| { | ||
| throw new InvalidOperationException("Failed to create TokenAcquisition instance for testing"); | ||
| } | ||
|
|
||
| return tokenAcquisition; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a MsalServiceException for testing. | ||
| /// </summary> | ||
| private MsalServiceException CreateMsalServiceException(string errorCode, string responseBody) | ||
| { | ||
| // Use the MsalServiceException constructor with errorCode and errorMessage | ||
| // The ResponseBody property is internal but can be accessed via reflection | ||
| var exception = new MsalServiceException(errorCode, $"Test exception: {errorCode}"); | ||
|
|
||
| // Set the ResponseBody property using reflection | ||
| var responseBodyField = typeof(MsalServiceException).GetProperty("ResponseBody"); | ||
| if (responseBodyField != null && responseBodyField.CanWrite) | ||
| { | ||
| responseBodyField.SetValue(exception, responseBody); | ||
| } | ||
| else | ||
| { | ||
| // Try the backing field instead | ||
| var backingField = typeof(MsalServiceException).GetField("<ResponseBody>k__BackingField", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
| if (backingField != null) | ||
| { | ||
| backingField.SetValue(exception, responseBody); | ||
| } | ||
| } | ||
|
|
||
| return exception; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Invokes the private IsInvalidClientCertificateOrSignedAssertionError method using reflection. | ||
| /// </summary> | ||
| private bool InvokeIsInvalidClientCertificateOrSignedAssertionError( | ||
| TokenAcquisition tokenAcquisition, | ||
| MsalServiceException exception) | ||
| { | ||
| var method = typeof(TokenAcquisition).GetMethod( | ||
| "IsInvalidClientCertificateOrSignedAssertionError", | ||
| BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| if (method == null) | ||
| { | ||
| throw new InvalidOperationException("Could not find IsInvalidClientCertificateOrSignedAssertionError method"); | ||
| } | ||
|
|
||
| var result = method.Invoke(tokenAcquisition, new object[] { exception }); | ||
| return (bool)result!; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that this is "Client assertion contains an invalid signature", not CertificateNotWithinValidityPeriod. This makes sense as right now this is the same as CertificateIsOutsideValidityWindow which is right above it. https://stackoverflow.com/questions/62716173/jwt-token-error-aadsts700027-client-assertion-contains-an-invalid-signature