Skip to content

Commit

Permalink
Avoid allocating exceptional StringBuilder until there's a need (#2171)
Browse files Browse the repository at this point in the history
* Avoid allocating exceptional StringBuilder until there's a need

* Update src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs
  • Loading branch information
stephentoub authored Jul 27, 2023
1 parent d228169 commit f1f7f12
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
28 changes: 14 additions & 14 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JsonWebToken jwtToken

var unwrappedKeys = new List<SecurityKey>();
// keep track of exceptions thrown, keys that were tried
var exceptionStrings = new StringBuilder();
var keysAttempted = new StringBuilder();
StringBuilder exceptionStrings = null;
StringBuilder keysAttempted = null;
foreach (var key in keys)
{
try
Expand Down Expand Up @@ -1203,16 +1203,16 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JsonWebToken jwtToken
}
catch (Exception ex)
{
exceptionStrings.AppendLine(ex.ToString());
(exceptionStrings ??= new StringBuilder()).AppendLine(ex.ToString());
}

keysAttempted.AppendLine(key.ToString());
(keysAttempted ??= new StringBuilder()).AppendLine(key.ToString());
}

if (unwrappedKeys.Count > 0 && exceptionStrings.Length == 0)
if (unwrappedKeys.Count > 0 && exceptionStrings is null)
return unwrappedKeys;
else
throw LogHelper.LogExceptionMessage(new SecurityTokenKeyWrapException(LogHelper.FormatInvariant(TokenLogMessages.IDX10618, keysAttempted, exceptionStrings, jwtToken)));
throw LogHelper.LogExceptionMessage(new SecurityTokenKeyWrapException(LogHelper.FormatInvariant(TokenLogMessages.IDX10618, (object)keysAttempted ?? "", (object)exceptionStrings ?? "", jwtToken)));
}

/// <summary>
Expand Down Expand Up @@ -1728,8 +1728,8 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
}

// keep track of exceptions thrown, keys that were tried
var exceptionStrings = new StringBuilder();
var keysAttempted = new StringBuilder();
StringBuilder exceptionStrings = null;
StringBuilder keysAttempted = null;
var kidExists = !string.IsNullOrEmpty(jwtToken.Kid);

if (keys != null)
Expand All @@ -1747,12 +1747,12 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
}
catch (Exception ex)
{
exceptionStrings.AppendLine(ex.ToString());
(exceptionStrings ??= new StringBuilder()).AppendLine(ex.ToString());
}

if (key != null)
{
keysAttempted.Append(key.ToString()).Append(" , KeyId: ").AppendLine(key.KeyId);
(keysAttempted ??= new StringBuilder()).Append(key.ToString()).Append(" , KeyId: ").AppendLine(key.KeyId);
if (kidExists && !kidMatched && key.KeyId != null)
kidMatched = jwtToken.Kid.Equals(key.KeyId, key is X509SecurityKey ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
}
Expand All @@ -1773,12 +1773,12 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
var isKidInTVP = keysInTokenValidationParameters.Any(x => x.KeyId.Equals(localJwtToken.Kid));
var keyLocation = isKidInTVP ? "TokenValidationParameters" : "Configuration";
throw LogHelper.LogExceptionMessage(new SecurityTokenInvalidSignatureException(LogHelper.FormatInvariant(TokenLogMessages.IDX10511,
keysAttempted,
(object)keysAttempted ?? "",
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
LogHelper.MarkAsNonPII(keyLocation),
LogHelper.MarkAsNonPII(jwtToken.Kid),
exceptionStrings,
(object)exceptionStrings ?? "",
jwtToken)));
}

Expand All @@ -1797,12 +1797,12 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
}
}

if (keysAttempted.Length > 0)
if (keysAttempted is not null)
throw LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(LogHelper.FormatInvariant(TokenLogMessages.IDX10503,
keysAttempted,
LogHelper.MarkAsNonPII(numKeysInTokenValidationParameters),
LogHelper.MarkAsNonPII(numKeysInConfiguration),
exceptionStrings,
(object)exceptionStrings ?? "",
jwtToken)));

throw LogHelper.LogExceptionMessage(new SecurityTokenSignatureKeyNotFoundException(TokenLogMessages.IDX10500));
Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ internal static string DecryptJwtToken(
byte[] decryptedTokenBytes = null;

// keep track of exceptions thrown, keys that were tried
var exceptionStrings = new StringBuilder();
var keysAttempted = new StringBuilder();
StringBuilder exceptionStrings = null;
StringBuilder keysAttempted = null;
string zipAlgorithm = null;
foreach (SecurityKey key in decryptionParameters.Keys)
{
Expand Down Expand Up @@ -225,11 +225,11 @@ internal static string DecryptJwtToken(
}
catch (Exception ex)
{
exceptionStrings.AppendLine(ex.ToString());
(exceptionStrings ??= new StringBuilder()).AppendLine(ex.ToString());
}

if (key != null)
keysAttempted.AppendLine(key.ToString());
(keysAttempted ??= new StringBuilder()).AppendLine(key.ToString());
}

ValidateDecryption(decryptionParameters, decryptionSucceeded, algorithmNotSupportedByCryptoProvider, exceptionStrings, keysAttempted);
Expand All @@ -248,8 +248,8 @@ internal static string DecryptJwtToken(

private static void ValidateDecryption(JwtTokenDecryptionParameters decryptionParameters, bool decryptionSucceeded, bool algorithmNotSupportedByCryptoProvider, StringBuilder exceptionStrings, StringBuilder keysAttempted)
{
if (!decryptionSucceeded && keysAttempted.Length > 0)
throw LogHelper.LogExceptionMessage(new SecurityTokenDecryptionFailedException(LogHelper.FormatInvariant(TokenLogMessages.IDX10603, keysAttempted, exceptionStrings, LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken))));
if (!decryptionSucceeded && keysAttempted is not null)
throw LogHelper.LogExceptionMessage(new SecurityTokenDecryptionFailedException(LogHelper.FormatInvariant(TokenLogMessages.IDX10603, keysAttempted, (object)exceptionStrings ?? "", LogHelper.MarkAsSecurityArtifact(decryptionParameters.EncodedToken, SafeLogJwtToken))));

if (!decryptionSucceeded && algorithmNotSupportedByCryptoProvider)
throw LogHelper.LogExceptionMessage(new SecurityTokenDecryptionFailedException(LogHelper.FormatInvariant(TokenLogMessages.IDX10619, LogHelper.MarkAsNonPII(decryptionParameters.Alg), LogHelper.MarkAsNonPII(decryptionParameters.Enc))));
Expand Down

0 comments on commit f1f7f12

Please sign in to comment.