| | | 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.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace DotnetFuzzing.Fuzzers; |
| | | 8 | | |
| | | 9 | | internal sealed class JsonDocumentFuzzer : IFuzzer |
| | | 10 | | { |
| | 1 | 11 | | public string[] TargetAssemblies { get; } = ["System.Text.Json"]; |
| | 0 | 12 | | public string[] TargetCoreLibPrefixes => []; |
| | 0 | 13 | | public string Dictionary => "json.dict"; |
| | | 14 | | |
| | | 15 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 4884 | 16 | | { |
| | 4884 | 17 | | if (bytes.IsEmpty) |
| | 0 | 18 | | { |
| | 0 | 19 | | return; |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | // The first byte is used to select various options. |
| | | 23 | | // The rest of the input is used as the UTF-8 JSON payload. |
| | 4884 | 24 | | byte optionsByte = bytes[0]; |
| | 4884 | 25 | | bytes = bytes.Slice(1); |
| | | 26 | | |
| | 4884 | 27 | | var options = new JsonDocumentOptions |
| | 4884 | 28 | | { |
| | 4884 | 29 | | AllowTrailingCommas = (optionsByte & 1) != 0, |
| | 4884 | 30 | | CommentHandling = (optionsByte & 2) != 0 ? JsonCommentHandling.Skip : JsonCommentHandling.Disallow, |
| | 4884 | 31 | | }; |
| | | 32 | | |
| | 4884 | 33 | | using var poisonAfter = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.After); |
| | | 34 | | |
| | | 35 | | try |
| | 4884 | 36 | | { |
| | 4884 | 37 | | JsonDocument.Parse(poisonAfter.Memory, options).Dispose(); |
| | 150 | 38 | | } |
| | 14202 | 39 | | catch (JsonException) { } |
| | 9768 | 40 | | } |
| | | 41 | | } |