-
Notifications
You must be signed in to change notification settings - Fork 456
Fix DateTime claim value type recognition for ISO8601 strings with and without trailing zeros #3373
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Security.Claims; | ||
| using System.Text; | ||
| using Microsoft.IdentityModel.Logging; | ||
| using Microsoft.IdentityModel.Protocols.OpenIdConnect; | ||
|
|
@@ -218,6 +219,21 @@ public void ResolveTokenSigningKey() | |
| Assert.Null(resolvedKey); | ||
| } | ||
|
|
||
| #region GetStringClaimValueType | ||
|
|
||
| [Fact] | ||
| public void GetStringClaimValueType_ReturnsDateTime_ForIso8601WithOrWithoutTrailingZeros() | ||
| { | ||
| var iso8601WithTrailingZeros = "2025-11-26T09:30:45.1234560Z"; | ||
| var iso8601WithoutTrailingZeros = "2025-11-26T09:30:45.123456Z"; | ||
| var resultWithTrailingZeros = JwtTokenUtilities.GetStringClaimValueType(iso8601WithTrailingZeros); | ||
| var resultWithoutTrailingZeros = JwtTokenUtilities.GetStringClaimValueType(iso8601WithoutTrailingZeros); | ||
| Assert.Equal(ClaimValueTypes.DateTime, resultWithTrailingZeros); | ||
| Assert.Equal(ClaimValueTypes.DateTime, resultWithoutTrailingZeros); | ||
| } | ||
|
Comment on lines
+224
to
+233
|
||
|
|
||
| #endregion | ||
|
|
||
| #region DecryptJwtToken Tests | ||
| [Fact] | ||
| public void DecryptJwtToken_WhenValidationParametersIsNull_ThrowsException() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| using System.Reflection; | ||
| using System.Security.Claims; | ||
| using System.Text; | ||
| using Microsoft.IdentityModel.JsonWebTokens; | ||
| using Microsoft.IdentityModel.TestUtils; | ||
| using Microsoft.IdentityModel.Tokens; | ||
| using Microsoft.IdentityModel.Tokens.Json; | ||
|
|
@@ -182,6 +183,23 @@ public void TestDateTimeClaim() | |
| Assert.True(string.Equals(dateTimeClaim.Value, dateTime.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture)), "dateTimeClaim.Value != dateTime.ToUniversalTime('o', CultureInfo.InvariantCulture).ToString()"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestReturnsDateTime_ForIso8601WithOrWithoutTrailingZeros() | ||
| { | ||
| var iso8601WithTrailingZeros = "2025-11-26T09:30:45.1234560Z"; | ||
| var iso8601WithoutTrailingZeros = "2025-11-26T09:30:45.123456Z"; | ||
| var jwtpayload = new JwtPayload | ||
| { | ||
| { "dateWithTrailingZeros", iso8601WithTrailingZeros }, | ||
| { "dateWithoutTrailingZeros", iso8601WithoutTrailingZeros } | ||
| }; | ||
|
|
||
| var claimWithTrailingZeros = jwtpayload.Claims.First(c => c.Type == "dateWithTrailingZeros"); | ||
| var claimWithoutTrailingZeros = jwtpayload.Claims.First(c => c.Type == "dateWithoutTrailingZeros"); | ||
| Assert.Equal(ClaimValueTypes.DateTime, claimWithTrailingZeros.ValueType); | ||
| Assert.Equal(ClaimValueTypes.DateTime, claimWithoutTrailingZeros.ValueType); | ||
| } | ||
|
Comment on lines
+186
to
+201
|
||
|
|
||
| [Fact] | ||
| public void TestClaimWithLargeExpValue() | ||
| { | ||
|
|
||
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.
The change to use
DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversalis broader than just handling trailing zeros. This will now classify ANY parseable DateTime string as a DateTime claim, including:This is a significant behavioral change beyond the trailing zeros issue described in the PR. Consider:
DateTimeStyles.RoundtripKind(used in JsonSerializerPrimitives.cs line 151) would be more appropriate to maintain consistency and only accept ISO8601 roundtrip formats