Skip to content

Commit

Permalink
Using JwtConstants.MaxJwtSegmentCount to prevent possible performance…
Browse files Browse the repository at this point in the history
… issues
  • Loading branch information
mafurman committed Dec 9, 2019
1 parent ef0e716 commit 408194d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public JsonWebToken(string jwtEncodedString)
if (string.IsNullOrEmpty(jwtEncodedString))
throw new ArgumentNullException(nameof(jwtEncodedString));

// Number of segments is always one more than the number of '.'s.
var count = jwtEncodedString.Count(c => c == '.') + 1;
if (count == JwtConstants.JwsSegmentCount || count == JwtConstants.JweSegmentCount)
{
var tokenParts = jwtEncodedString.Split('.');
// Max number of segments is set to JwtConstants.MaxJwtSegmentCount + 1 so that we know if there were more than 5 segments present.
// In the case where JwtEncodedString has greater than 5 segments, the length of tokenParts will always be 6.
var tokenParts = jwtEncodedString.Split(new char[] { '.' }, JwtConstants.MaxJwtSegmentCount + 1);
if (tokenParts.Length == JwtConstants.JwsSegmentCount || tokenParts.Length == JwtConstants.JweSegmentCount)
Decode(tokenParts, jwtEncodedString);
}
else
throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14100, jwtEncodedString)));
}
Expand Down

0 comments on commit 408194d

Please sign in to comment.