diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs index 60795b86ad..32ca7e462f 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs @@ -1170,8 +1170,8 @@ internal IEnumerable GetContentEncryptionKeys(JsonWebToken jwtToken var unwrappedKeys = new List(); // 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 @@ -1203,16 +1203,16 @@ internal IEnumerable 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))); } /// @@ -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) @@ -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); } @@ -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))); } @@ -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)); diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs index 0823fc1b95..a7a4726fc0 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JwtTokenUtilities.cs @@ -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) { @@ -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); @@ -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))));