Skip to content

Commit

Permalink
Add test case. Fix Dot3 in read JWT. (#2501)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaytak authored Feb 28, 2024
1 parent 28965b7 commit aa7d838
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,10 @@ internal void ReadToken(ReadOnlyMemory<char> encodedTokenMemory)
// empty payload for JWE's {encrypted tokens}.
Payload = new JsonClaimSet();

if (Dot3 == encodedTokenSpan.Length) // TODO: Should this be encodedJsonSpan.Length - 1?
throw LogHelper.LogExceptionMessage(new ArgumentException(LogMessages.IDX14121));

Dot3 = Dot2 + Dot3 + 1;

if (Dot3 == encodedTokenSpan.Length - 1)
throw LogHelper.LogExceptionMessage(new SecurityTokenMalformedException(LogMessages.IDX14121));

Dot4 = encodedTokenSpan.Slice(Dot3 + 1).IndexOf('.');
if (Dot4 == -1)
throw LogHelper.LogExceptionMessage(new SecurityTokenMalformedException(LogMessages.IDX14121));
Expand Down
7 changes: 7 additions & 0 deletions test/System.IdentityModel.Tokens.Jwt.Tests/JwtTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public static TheoryData<JwtTheoryData> InvalidNumberOfSegmentsData(IList<string
Token = "a"
});
theoryData.Add(new JwtTheoryData
{
CanRead = false,
ExpectedException = ExpectedException.SecurityTokenMalformedTokenException(errorStrings[0]),
TestId = "a.",
Token = "a."
});
theoryData.Add(new JwtTheoryData
{
CanRead = false,
ExpectedException = ExpectedException.SecurityTokenMalformedTokenException(errorStrings[1]),
Expand Down

0 comments on commit aa7d838

Please sign in to comment.