diff --git a/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs b/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs index 347e80b9f1..9e542fda07 100644 --- a/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs +++ b/src/Microsoft.IdentityModel.Tokens/Base64UrlEncoder.cs @@ -246,8 +246,10 @@ private static unsafe byte[] UnsafeNetCoreDecode(ReadOnlySpan strSpan, boo int length = Encoding.UTF8.GetBytes(source, bytesSpan); Span utf8Span = bytesSpan.Slice(0, length); + OperationStatus status = System.Buffers.Text.Base64.DecodeFromUtf8InPlace(utf8Span, out int bytesWritten); - Debug.Assert(status == OperationStatus.Done, "Expected DecodeFromUtf8 to be successful"); + if (status != OperationStatus.Done) + throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString()))); byte[] result = bytesSpan.Slice(0, bytesWritten).ToArray(); @@ -296,8 +298,10 @@ private static unsafe void UnsafeNetCoreDecode(ReadOnlySpan strSpan, Span< int length = Encoding.UTF8.GetBytes(source, bytesSpan); Span utf8Span = bytesSpan.Slice(0, length); + OperationStatus status = System.Buffers.Text.Base64.DecodeFromUtf8InPlace(utf8Span, out int bytesWritten); - Debug.Assert(status == OperationStatus.Done, "Expected DecodeFromUtf8 to be successful"); + if (status != OperationStatus.Done) + throw LogHelper.LogExceptionMessage(new FormatException(LogHelper.FormatInvariant(LogMessages.IDX10400, strSpan.ToString()))); utf8Span.Slice(0, bytesWritten).CopyTo(output);