-
Notifications
You must be signed in to change notification settings - Fork 415
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
Regression tests: Algorithm #2934
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d424242
Added AlgorithmValidationError, refactored error creation in Validate…
iNinja 2bf18ef
Added regression/comparison tests for algorithm validation scenarios.
iNinja 1a63e55
Merge branch 'dev' into iinglese/regression-tests-algorithm
iNinja 3349f12
Merge branch 'dev' into iinglese/regression-tests-algorithm
iNinja 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 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 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
42 changes: 42 additions & 0 deletions
42
src/Microsoft.IdentityModel.Tokens/Validation/Results/Details/AlgorithmValidationError.cs
This file contains 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,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
|
||
#nullable enable | ||
namespace Microsoft.IdentityModel.Tokens | ||
{ | ||
internal class AlgorithmValidationError : ValidationError | ||
{ | ||
protected string? _invalidAlgorithm; | ||
|
||
public AlgorithmValidationError( | ||
MessageDetail messageDetail, | ||
Type exceptionType, | ||
StackFrame stackFrame, | ||
string? invalidAlgorithm) : | ||
base(messageDetail, ValidationFailureType.AlgorithmValidationFailed, exceptionType, stackFrame) | ||
{ | ||
_invalidAlgorithm = invalidAlgorithm; | ||
} | ||
|
||
internal override Exception GetException() | ||
{ | ||
if (ExceptionType == typeof(SecurityTokenInvalidAlgorithmException)) | ||
{ | ||
SecurityTokenInvalidAlgorithmException exception = new(MessageDetail.Message, InnerException) | ||
{ | ||
InvalidAlgorithm = _invalidAlgorithm | ||
}; | ||
|
||
return exception; | ||
} | ||
|
||
return base.GetException(); | ||
} | ||
|
||
internal string? InvalidAlgorithm => _invalidAlgorithm; | ||
} | ||
} | ||
#nullable restore |
This file contains 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
134 changes: 134 additions & 0 deletions
134
...dentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.ValidateTokenAsyncTests.Algorithm.cs
This file contains 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,134 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#nullable enable | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.TestUtils; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Xunit; | ||
|
||
namespace Microsoft.IdentityModel.JsonWebTokens.Tests | ||
{ | ||
public partial class JsonWebTokenHandlerValidateTokenAsyncTests | ||
{ | ||
[Theory, MemberData(nameof(ValidateTokenAsync_AlgorithmTestCases), DisableDiscoveryEnumeration = true)] | ||
public async Task ValidateTokenAsync_Algorithm(ValidateTokenAsyncAlgorithmTheoryData theoryData) | ||
{ | ||
var context = TestUtilities.WriteHeader($"{this}.ValidateTokenAsync_Algorithm", theoryData); | ||
|
||
string jwtString = CreateTokenWithSigningCredentials(theoryData.SigningCredentials); | ||
|
||
await ValidateAndCompareResults(jwtString, theoryData, context); | ||
|
||
TestUtilities.AssertFailIfErrors(context); | ||
} | ||
|
||
public static TheoryData<ValidateTokenAsyncAlgorithmTheoryData> ValidateTokenAsync_AlgorithmTestCases | ||
{ | ||
get | ||
{ | ||
var theoryData = new TheoryData<ValidateTokenAsyncAlgorithmTheoryData>(); | ||
|
||
theoryData.Add(new ValidateTokenAsyncAlgorithmTheoryData("Valid_AlgorithmIsValid") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2, | ||
TokenValidationParameters = CreateTokenValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, | ||
validAlgorithms: [SecurityAlgorithms.RsaSha256Signature]), | ||
ValidationParameters = CreateValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, | ||
validAlgorithms: [SecurityAlgorithms.RsaSha256Signature]), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncAlgorithmTheoryData("Valid_ValidAlgorithmsIsNull") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2, | ||
TokenValidationParameters = CreateTokenValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, | ||
validAlgorithms: null), | ||
ValidationParameters = CreateValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, | ||
validAlgorithms: null), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncAlgorithmTheoryData("Valid_ValidAlgorithmsIsEmptyList") | ||
{ | ||
SigningCredentials = KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2, | ||
TokenValidationParameters = CreateTokenValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, validAlgorithms: []), | ||
ValidationParameters = CreateValidationParameters( | ||
KeyingMaterial.DefaultX509SigningCreds_2048_RsaSha2_Sha2.Key, validAlgorithms: []), | ||
}); | ||
|
||
theoryData.Add(new ValidateTokenAsyncAlgorithmTheoryData("Invalid_TokenIsSignedWithAnInvalidAlgorithm") | ||
iNinja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// Token is signed with HmacSha256 but only sha256 is considered valid for this test's purposes | ||
SigningCredentials = KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2, | ||
TokenValidationParameters = CreateTokenValidationParameters( | ||
KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2.Key, | ||
validAlgorithms: [SecurityAlgorithms.Sha256]), | ||
ValidationParameters = CreateValidationParameters( | ||
KeyingMaterial.DefaultSymmetricSigningCreds_256_Sha2.Key, | ||
validAlgorithms: [SecurityAlgorithms.Sha256]), | ||
ExpectedIsValid = false, | ||
ExpectedException = ExpectedException.SecurityTokenInvalidSignatureException("IDX10511:"), | ||
ExpectedExceptionValidationParameters = ExpectedException.SecurityTokenInvalidAlgorithmException( | ||
"IDX10518:", | ||
propertiesExpected: new() { { "InvalidAlgorithm", SecurityAlgorithms.HmacSha256Signature } }), | ||
}); | ||
|
||
return theoryData; | ||
|
||
static TokenValidationParameters CreateTokenValidationParameters( | ||
SecurityKey? signingKey = null, List<string>? validAlgorithms = null) | ||
{ | ||
// only validate the signature and algorithm | ||
var tokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateAudience = false, | ||
ValidateIssuer = false, | ||
ValidateLifetime = false, | ||
ValidateTokenReplay = false, | ||
ValidateIssuerSigningKey = false, | ||
RequireSignedTokens = true, | ||
IssuerSigningKey = signingKey, | ||
}; | ||
|
||
tokenValidationParameters.ValidAlgorithms = validAlgorithms; | ||
|
||
return tokenValidationParameters; | ||
} | ||
|
||
static ValidationParameters CreateValidationParameters( | ||
SecurityKey? signingKey = null, List<string>? validAlgorithms = null) | ||
{ | ||
ValidationParameters validationParameters = new ValidationParameters(); | ||
|
||
if (signingKey is not null) | ||
validationParameters.IssuerSigningKeys.Add(signingKey); | ||
|
||
validationParameters.ValidAlgorithms = validAlgorithms; | ||
|
||
// Skip all validations except signature and algorithm | ||
validationParameters.AudienceValidator = SkipValidationDelegates.SkipAudienceValidation; | ||
validationParameters.IssuerSigningKeyValidator = SkipValidationDelegates.SkipIssuerSigningKeyValidation; | ||
validationParameters.IssuerValidatorAsync = SkipValidationDelegates.SkipIssuerValidation; | ||
validationParameters.LifetimeValidator = SkipValidationDelegates.SkipLifetimeValidation; | ||
validationParameters.TokenReplayValidator = SkipValidationDelegates.SkipTokenReplayValidation; | ||
validationParameters.TypeValidator = SkipValidationDelegates.SkipTokenTypeValidation; | ||
|
||
return validationParameters; | ||
} | ||
} | ||
} | ||
|
||
public class ValidateTokenAsyncAlgorithmTheoryData : ValidateTokenAsyncBaseTheoryData | ||
{ | ||
public ValidateTokenAsyncAlgorithmTheoryData(string testId) : base(testId) { } | ||
|
||
public SigningCredentials? SigningCredentials { get; set; } | ||
} | ||
} | ||
} | ||
#nullable restore |
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.
Curious as to why we need to recreate the AlgorithmValidationError?