| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Text; |
| | | 6 | | |
| | | 7 | | namespace DotnetFuzzing.Fuzzers |
| | | 8 | | { |
| | | 9 | | internal sealed class Base64Fuzzer : IFuzzer |
| | | 10 | | { |
| | | 11 | | private const int Base64LineBreakPosition = 76; // Needs to be in sync with Convert.Base64LineBreakPosition |
| | | 12 | | |
| | 0 | 13 | | public string[] TargetAssemblies => []; |
| | | 14 | | |
| | 0 | 15 | | public string[] TargetCoreLibPrefixes => ["System.Buffers.Text.Base64", "System.Convert"]; |
| | | 16 | | |
| | | 17 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 1787 | 18 | | { |
| | 1787 | 19 | | using PooledBoundedMemory<byte> inputPoisonBefore = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacemen |
| | 1787 | 20 | | using PooledBoundedMemory<byte> inputPoisonAfter = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement |
| | | 21 | | |
| | 1787 | 22 | | TestCases(inputPoisonBefore.Span, PoisonPagePlacement.Before); |
| | 1787 | 23 | | TestCases(inputPoisonAfter.Span, PoisonPagePlacement.After); |
| | 3574 | 24 | | } |
| | | 25 | | |
| | | 26 | | private void TestCases(Span<byte> input, PoisonPagePlacement poison) |
| | 3574 | 27 | | { |
| | 3574 | 28 | | TestBase64(input, poison); |
| | 3574 | 29 | | TestBase64Chars(input, poison); |
| | 3574 | 30 | | TestToStringToCharArray(input, Base64FormattingOptions.None); |
| | 3574 | 31 | | TestToStringToCharArray(input, Base64FormattingOptions.InsertLineBreaks); |
| | 3574 | 32 | | } |
| | | 33 | | |
| | | 34 | | private void TestBase64(Span<byte> input, PoisonPagePlacement poison) |
| | 3574 | 35 | | { |
| | 3574 | 36 | | int maxEncodedLength = Base64.GetMaxEncodedToUtf8Length(input.Length); |
| | 3574 | 37 | | using PooledBoundedMemory<byte> destPoisoned = PooledBoundedMemory<byte>.Rent(maxEncodedLength, poison); |
| | 3574 | 38 | | Span<byte> encoderDest = destPoisoned.Span; |
| | 3574 | 39 | | using PooledBoundedMemory<byte> decoderDestPoisoned = PooledBoundedMemory<byte>.Rent(Base64.GetMaxDecodedFro |
| | 3574 | 40 | | Span<byte> decoderDest = decoderDestPoisoned.Span; |
| | 3574 | 41 | | { // IsFinalBlock = true |
| | 3574 | 42 | | OperationStatus status = Base64.EncodeToUtf8(input, encoderDest, out int bytesConsumed, out int bytesEnc |
| | 3574 | 43 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 44 | | Assert.Equal(input.Length, bytesConsumed); |
| | 3574 | 45 | | Assert.Equal(true, maxEncodedLength >= bytesEncoded && maxEncodedLength - 2 <= bytesEncoded); |
| | | 46 | | |
| | 3574 | 47 | | status = Base64.DecodeFromUtf8(encoderDest.Slice(0, bytesEncoded), decoderDest, out int bytesRead, out i |
| | | 48 | | |
| | 3574 | 49 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 50 | | Assert.Equal(input.Length, bytesDecoded); |
| | 3574 | 51 | | Assert.Equal(bytesEncoded, bytesRead); |
| | 3574 | 52 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesDecoded)); |
| | 3574 | 53 | | } |
| | | 54 | | |
| | 3574 | 55 | | { // IsFinalBlock = false |
| | 3574 | 56 | | encoderDest.Clear(); |
| | 3574 | 57 | | decoderDest.Clear(); |
| | 3574 | 58 | | OperationStatus status = Base64.EncodeToUtf8(input, encoderDest, out int bytesConsumed, out int bytesEnc |
| | 3574 | 59 | | Span<byte> decodeInput = encoderDest.Slice(0, bytesEncoded); |
| | | 60 | | |
| | 3574 | 61 | | if (input.Length % 3 == 0) |
| | 996 | 62 | | { |
| | 996 | 63 | | Assert.Equal(OperationStatus.Done, status); |
| | 996 | 64 | | Assert.Equal(input.Length, bytesConsumed); |
| | 996 | 65 | | Assert.Equal(true, maxEncodedLength == bytesEncoded); |
| | | 66 | | |
| | 996 | 67 | | status = Base64.DecodeFromUtf8(decodeInput, decoderDest, out int bytesRead, out int bytesDecoded, is |
| | | 68 | | |
| | 996 | 69 | | Assert.Equal(OperationStatus.Done, status); |
| | 996 | 70 | | Assert.Equal(input.Length, bytesDecoded); |
| | 996 | 71 | | Assert.Equal(bytesEncoded, bytesRead); |
| | 996 | 72 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesDecoded)); |
| | 996 | 73 | | } |
| | | 74 | | else |
| | 2578 | 75 | | { |
| | 2578 | 76 | | Assert.Equal(OperationStatus.NeedMoreData, status); |
| | 2578 | 77 | | Assert.Equal(true, input.Length / 3 * 4 == bytesEncoded); |
| | | 78 | | |
| | 2578 | 79 | | status = Base64.DecodeFromUtf8(decodeInput, decoderDest, out int bytesRead, out int bytesDecoded, is |
| | | 80 | | |
| | 2578 | 81 | | if (decodeInput.Length % 4 == 0) |
| | 2578 | 82 | | { |
| | 2578 | 83 | | Assert.Equal(OperationStatus.Done, status); |
| | 2578 | 84 | | Assert.Equal(bytesConsumed, bytesDecoded); |
| | 2578 | 85 | | Assert.Equal(bytesEncoded, bytesRead); |
| | 2578 | 86 | | } |
| | | 87 | | else |
| | 0 | 88 | | { |
| | 0 | 89 | | Assert.Equal(OperationStatus.NeedMoreData, status); |
| | 0 | 90 | | } |
| | | 91 | | |
| | 2578 | 92 | | Assert.SequenceEqual(input.Slice(0, bytesDecoded), decoderDest.Slice(0, bytesDecoded)); |
| | 2578 | 93 | | } |
| | 3574 | 94 | | } |
| | | 95 | | |
| | 3574 | 96 | | { // Encode / decode in place |
| | 3574 | 97 | | encoderDest.Clear(); |
| | 3574 | 98 | | input.CopyTo(encoderDest); |
| | 3574 | 99 | | OperationStatus status = Base64.EncodeToUtf8InPlace(encoderDest, input.Length, out int bytesEncoded); |
| | | 100 | | |
| | 3574 | 101 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 102 | | Assert.Equal(true, maxEncodedLength >= bytesEncoded && maxEncodedLength - 2 <= bytesEncoded); |
| | | 103 | | |
| | 3574 | 104 | | status = Base64.DecodeFromUtf8InPlace(encoderDest.Slice(0, bytesEncoded), out int bytesDecoded); |
| | | 105 | | |
| | 3574 | 106 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 107 | | Assert.Equal(input.Length, bytesDecoded); |
| | 3574 | 108 | | Assert.SequenceEqual(input, encoderDest.Slice(0, bytesDecoded)); |
| | 3574 | 109 | | } |
| | | 110 | | |
| | 3574 | 111 | | { // Decode the random input directly, Assert IsValid result matches with decoded result |
| | 3574 | 112 | | decoderDest.Clear(); |
| | 3574 | 113 | | if (Base64.IsValid(input, out int decodedLength)) |
| | 254 | 114 | | { |
| | 254 | 115 | | int maxDecodedLength = Base64.GetMaxDecodedFromUtf8Length(input.Length); |
| | 254 | 116 | | OperationStatus status = Base64.DecodeFromUtf8(input, decoderDest, out int bytesRead, out int bytesD |
| | 254 | 117 | | Assert.Equal(OperationStatus.Done, status); |
| | 254 | 118 | | Assert.Equal(input.Length, bytesRead); |
| | 254 | 119 | | Assert.Equal(decodedLength, bytesDecoded); |
| | 254 | 120 | | Assert.Equal(true, maxDecodedLength >= bytesDecoded); |
| | | 121 | | |
| | 254 | 122 | | status = Base64.DecodeFromUtf8InPlace(input, out int inPlaceDecoded); |
| | 254 | 123 | | Assert.Equal(OperationStatus.Done, status); |
| | 254 | 124 | | Assert.Equal(bytesDecoded, inPlaceDecoded); |
| | 254 | 125 | | Assert.SequenceEqual<byte>(input.Slice(0, inPlaceDecoded), decoderDest.Slice(0, bytesDecoded)); |
| | 254 | 126 | | } |
| | | 127 | | else |
| | 3320 | 128 | | { |
| | 3320 | 129 | | Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8(input, decoderDest, out int _, out i |
| | 3320 | 130 | | Assert.Equal(OperationStatus.InvalidData, Base64.DecodeFromUtf8InPlace(input, out int inPlaceDecoded |
| | 3320 | 131 | | } |
| | 3574 | 132 | | } |
| | | 133 | | |
| | 3574 | 134 | | { // Test new simplified UTF-8 APIs |
| | | 135 | | // Test EncodeToUtf8 returning byte[] |
| | 3574 | 136 | | byte[] encodedArray = Base64.EncodeToUtf8(input); |
| | 3574 | 137 | | Assert.Equal(true, maxEncodedLength >= encodedArray.Length && maxEncodedLength - 2 <= encodedArray.Lengt |
| | | 138 | | |
| | | 139 | | // Test EncodeToUtf8 returning int |
| | 3574 | 140 | | encoderDest.Clear(); |
| | 3574 | 141 | | int charsWritten = Base64.EncodeToUtf8(input, encoderDest); |
| | 3574 | 142 | | Assert.SequenceEqual(encodedArray.AsSpan(), encoderDest.Slice(0, charsWritten)); |
| | | 143 | | |
| | | 144 | | // Test TryEncodeToUtf8 |
| | 3574 | 145 | | encoderDest.Clear(); |
| | 3574 | 146 | | Assert.Equal(true, Base64.TryEncodeToUtf8(input, encoderDest, out int tryCharsWritten)); |
| | 3574 | 147 | | Assert.Equal(charsWritten, tryCharsWritten); |
| | 3574 | 148 | | Assert.SequenceEqual(encodedArray.AsSpan(), encoderDest.Slice(0, tryCharsWritten)); |
| | | 149 | | |
| | | 150 | | // Test DecodeFromUtf8 returning byte[] |
| | 3574 | 151 | | byte[] decodedArray = Base64.DecodeFromUtf8(encodedArray); |
| | 3574 | 152 | | Assert.SequenceEqual(input, decodedArray.AsSpan()); |
| | | 153 | | |
| | | 154 | | // Test DecodeFromUtf8 returning int |
| | 3574 | 155 | | decoderDest.Clear(); |
| | 3574 | 156 | | int bytesWritten = Base64.DecodeFromUtf8(encodedArray, decoderDest); |
| | 3574 | 157 | | Assert.Equal(input.Length, bytesWritten); |
| | 3574 | 158 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesWritten)); |
| | | 159 | | |
| | | 160 | | // Test TryDecodeFromUtf8 |
| | 3574 | 161 | | decoderDest.Clear(); |
| | 3574 | 162 | | Assert.Equal(true, Base64.TryDecodeFromUtf8(encodedArray, decoderDest, out int tryBytesWritten)); |
| | 3574 | 163 | | Assert.Equal(input.Length, tryBytesWritten); |
| | 3574 | 164 | | Assert.SequenceEqual(input, decoderDest.Slice(0, tryBytesWritten)); |
| | | 165 | | |
| | | 166 | | // Test TryEncodeToUtf8InPlace |
| | 3574 | 167 | | using PooledBoundedMemory<byte> inPlaceBuffer = PooledBoundedMemory<byte>.Rent(maxEncodedLength, poison) |
| | 3574 | 168 | | Span<byte> inPlaceDest = inPlaceBuffer.Span; |
| | 3574 | 169 | | input.CopyTo(inPlaceDest); |
| | 3574 | 170 | | Assert.Equal(true, Base64.TryEncodeToUtf8InPlace(inPlaceDest, input.Length, out int inPlaceWritten)); |
| | 3574 | 171 | | Assert.SequenceEqual(encodedArray.AsSpan(), inPlaceDest.Slice(0, inPlaceWritten)); |
| | | 172 | | |
| | | 173 | | // Test GetEncodedLength matches GetMaxEncodedToUtf8Length |
| | 3574 | 174 | | Assert.Equal(Base64.GetMaxEncodedToUtf8Length(input.Length), Base64.GetEncodedLength(input.Length)); |
| | | 175 | | |
| | | 176 | | // Test GetMaxDecodedLength matches GetMaxDecodedFromUtf8Length |
| | 3574 | 177 | | Assert.Equal(Base64.GetMaxDecodedFromUtf8Length(maxEncodedLength), Base64.GetMaxDecodedLength(maxEncoded |
| | 3574 | 178 | | } |
| | 7148 | 179 | | } |
| | | 180 | | |
| | | 181 | | private static void TestBase64Chars(Span<byte> input, PoisonPagePlacement poison) |
| | 3574 | 182 | | { |
| | 3574 | 183 | | int encodedLength = Base64.GetEncodedLength(input.Length); |
| | 3574 | 184 | | int maxDecodedLength = Base64.GetMaxDecodedLength(encodedLength); |
| | | 185 | | |
| | 3574 | 186 | | using PooledBoundedMemory<char> destPoisoned = PooledBoundedMemory<char>.Rent(encodedLength, poison); |
| | 3574 | 187 | | using PooledBoundedMemory<byte> decoderDestPoisoned = PooledBoundedMemory<byte>.Rent(maxDecodedLength, poiso |
| | | 188 | | |
| | 3574 | 189 | | Span<char> encoderDest = destPoisoned.Span; |
| | 3574 | 190 | | Span<byte> decoderDest = decoderDestPoisoned.Span; |
| | | 191 | | |
| | 3574 | 192 | | { // IsFinalBlock = true |
| | 3574 | 193 | | OperationStatus status = Base64.EncodeToChars(input, encoderDest, out int bytesConsumed, out int charsEn |
| | | 194 | | |
| | 3574 | 195 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 196 | | Assert.Equal(input.Length, bytesConsumed); |
| | 3574 | 197 | | Assert.Equal(encodedLength, charsEncoded); |
| | | 198 | | |
| | 3574 | 199 | | string encodedString = Base64.EncodeToString(input); |
| | 3574 | 200 | | Assert.Equal(encodedString, new string(encoderDest.Slice(0, charsEncoded))); |
| | | 201 | | |
| | 3574 | 202 | | status = Base64.DecodeFromChars(encoderDest.Slice(0, charsEncoded), decoderDest, out int charsRead, out |
| | | 203 | | |
| | 3574 | 204 | | Assert.Equal(OperationStatus.Done, status); |
| | 3574 | 205 | | Assert.Equal(input.Length, bytesDecoded); |
| | 3574 | 206 | | Assert.Equal(charsEncoded, charsRead); |
| | 3574 | 207 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesDecoded)); |
| | 3574 | 208 | | } |
| | | 209 | | |
| | 3574 | 210 | | { // IsFinalBlock = false |
| | 3574 | 211 | | encoderDest.Clear(); |
| | 3574 | 212 | | decoderDest.Clear(); |
| | 3574 | 213 | | OperationStatus status = Base64.EncodeToChars(input, encoderDest, out int bytesConsumed, out int charsEn |
| | 3574 | 214 | | Span<char> decodeInput = encoderDest.Slice(0, charsEncoded); |
| | | 215 | | |
| | 3574 | 216 | | if (input.Length % 3 == 0) |
| | 996 | 217 | | { |
| | 996 | 218 | | Assert.Equal(OperationStatus.Done, status); |
| | 996 | 219 | | Assert.Equal(input.Length, bytesConsumed); |
| | 996 | 220 | | Assert.Equal(encodedLength, charsEncoded); |
| | | 221 | | |
| | 996 | 222 | | status = Base64.DecodeFromChars(decodeInput, decoderDest, out int charsRead, out int bytesDecoded, i |
| | | 223 | | |
| | 996 | 224 | | Assert.Equal(OperationStatus.Done, status); |
| | 996 | 225 | | Assert.Equal(input.Length, bytesDecoded); |
| | 996 | 226 | | Assert.Equal(charsEncoded, charsRead); |
| | 996 | 227 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesDecoded)); |
| | 996 | 228 | | } |
| | | 229 | | else |
| | 2578 | 230 | | { |
| | 2578 | 231 | | Assert.Equal(OperationStatus.NeedMoreData, status); |
| | 2578 | 232 | | Assert.Equal(true, input.Length / 3 * 4 == charsEncoded); |
| | | 233 | | |
| | 2578 | 234 | | status = Base64.DecodeFromChars(decodeInput, decoderDest, out int charsRead, out int bytesDecoded, i |
| | | 235 | | |
| | 2578 | 236 | | if (decodeInput.Length % 4 == 0) |
| | 2578 | 237 | | { |
| | 2578 | 238 | | Assert.Equal(OperationStatus.Done, status); |
| | 2578 | 239 | | Assert.Equal(bytesConsumed, bytesDecoded); |
| | 2578 | 240 | | Assert.Equal(charsEncoded, charsRead); |
| | 2578 | 241 | | } |
| | | 242 | | else |
| | 0 | 243 | | { |
| | 0 | 244 | | Assert.Equal(OperationStatus.NeedMoreData, status); |
| | 0 | 245 | | } |
| | | 246 | | |
| | 2578 | 247 | | Assert.SequenceEqual(input.Slice(0, bytesDecoded), decoderDest.Slice(0, bytesDecoded)); |
| | 2578 | 248 | | } |
| | 3574 | 249 | | } |
| | | 250 | | |
| | 3574 | 251 | | { // Test array-returning and int-returning overloads |
| | 3574 | 252 | | char[] encodedChars = Base64.EncodeToChars(input); |
| | 3574 | 253 | | Assert.Equal(encodedLength, encodedChars.Length); |
| | | 254 | | |
| | 3574 | 255 | | encoderDest.Clear(); |
| | 3574 | 256 | | int charsWritten = Base64.EncodeToChars(input, encoderDest); |
| | 3574 | 257 | | Assert.Equal(encodedLength, charsWritten); |
| | 3574 | 258 | | Assert.SequenceEqual(encodedChars.AsSpan(), encoderDest.Slice(0, charsWritten)); |
| | | 259 | | |
| | 3574 | 260 | | byte[] decodedBytes = Base64.DecodeFromChars(encodedChars); |
| | 3574 | 261 | | Assert.SequenceEqual(input, decodedBytes.AsSpan()); |
| | | 262 | | |
| | 3574 | 263 | | decoderDest.Clear(); |
| | 3574 | 264 | | int bytesWritten = Base64.DecodeFromChars(encodedChars, decoderDest); |
| | 3574 | 265 | | Assert.Equal(input.Length, bytesWritten); |
| | 3574 | 266 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesWritten)); |
| | 3574 | 267 | | } |
| | | 268 | | |
| | 3574 | 269 | | { // Test Try* variants |
| | 3574 | 270 | | encoderDest.Clear(); |
| | 3574 | 271 | | Assert.Equal(true, Base64.TryEncodeToChars(input, encoderDest, out int charsWritten)); |
| | 3574 | 272 | | Assert.Equal(encodedLength, charsWritten); |
| | | 273 | | |
| | 3574 | 274 | | decoderDest.Clear(); |
| | 3574 | 275 | | Assert.Equal(true, Base64.TryDecodeFromChars(encoderDest.Slice(0, charsWritten), decoderDest, out int by |
| | 3574 | 276 | | Assert.Equal(input.Length, bytesWritten); |
| | 3574 | 277 | | Assert.SequenceEqual(input, decoderDest.Slice(0, bytesWritten)); |
| | 3574 | 278 | | } |
| | | 279 | | |
| | 3574 | 280 | | { // Decode the random chars directly (as chars, from the input bytes interpreted as UTF-16) |
| | | 281 | | // Create a char span from the input bytes for testing decode with random data |
| | 3574 | 282 | | if (input.Length >= 2) |
| | 3556 | 283 | | { |
| | 3556 | 284 | | ReadOnlySpan<char> inputChars = System.Runtime.InteropServices.MemoryMarshal.Cast<byte, char>(input) |
| | 3556 | 285 | | decoderDest.Clear(); |
| | | 286 | | |
| | | 287 | | // Try decoding - may succeed or fail depending on if input is valid base64 |
| | 3556 | 288 | | OperationStatus status = Base64.DecodeFromChars(inputChars, decoderDest, out int charsConsumed, out |
| | | 289 | | // Just verify we don't crash - the result depends on input validity |
| | 3556 | 290 | | Assert.Equal(true, status == OperationStatus.Done || status == OperationStatus.InvalidData || |
| | 3556 | 291 | | status == OperationStatus.NeedMoreData || status == OperationStatus.DestinationTo |
| | 3556 | 292 | | } |
| | 3574 | 293 | | } |
| | 7148 | 294 | | } |
| | | 295 | | |
| | | 296 | | private static void TestToStringToCharArray(Span<byte> input, Base64FormattingOptions options) |
| | 7148 | 297 | | { |
| | 7148 | 298 | | int encodedLength = ToBase64_CalculateOutputLength(input.Length, options == Base64FormattingOptions.InsertLi |
| | 7148 | 299 | | char[] dest = new char[encodedLength]; |
| | | 300 | | |
| | 7148 | 301 | | string toStringResult = Convert.ToBase64String(input, options); |
| | 7148 | 302 | | byte[] decoded = Convert.FromBase64String(toStringResult); |
| | | 303 | | |
| | 7148 | 304 | | Assert.SequenceEqual(input, decoded); |
| | | 305 | | |
| | 7148 | 306 | | int written = Convert.ToBase64CharArray(input.ToArray(), 0, input.Length, dest, 0, options); |
| | 7148 | 307 | | decoded = Convert.FromBase64CharArray(dest, 0, written); |
| | | 308 | | |
| | 7148 | 309 | | Assert.SequenceEqual(input, decoded); |
| | 7148 | 310 | | Assert.SequenceEqual(toStringResult.AsSpan(), dest.AsSpan(0, written)); |
| | 7148 | 311 | | } |
| | | 312 | | |
| | | 313 | | private static int ToBase64_CalculateOutputLength(int inputLength, bool insertLineBreaks) |
| | 7148 | 314 | | { |
| | 7148 | 315 | | uint outlen = ((uint)inputLength + 2) / 3 * 4; |
| | | 316 | | |
| | 7148 | 317 | | if (outlen == 0) |
| | 0 | 318 | | return 0; |
| | | 319 | | |
| | 7148 | 320 | | if (insertLineBreaks) |
| | 3574 | 321 | | { |
| | 3574 | 322 | | (uint newLines, uint remainder) = Math.DivRem(outlen, Base64LineBreakPosition); |
| | 3574 | 323 | | if (remainder == 0) |
| | 74 | 324 | | { |
| | 74 | 325 | | --newLines; |
| | 74 | 326 | | } |
| | 3574 | 327 | | outlen += newLines * 2; // 2 line break chars added: "\r\n" |
| | 3574 | 328 | | } |
| | | 329 | | |
| | 7148 | 330 | | return (int)outlen; |
| | 7148 | 331 | | } |
| | | 332 | | } |
| | | 333 | | } |