| | | 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.Net; |
| | | 6 | | using System.Net.Sockets; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Text.Unicode; |
| | | 10 | | |
| | | 11 | | namespace DotnetFuzzing.Fuzzers |
| | | 12 | | { |
| | | 13 | | internal sealed class IPAddressFuzzer : IFuzzer |
| | | 14 | | { |
| | 0 | 15 | | public string[] TargetAssemblies => ["System.Net.Primitives", "System.Private.Uri"]; |
| | 0 | 16 | | public string[] TargetCoreLibPrefixes => []; |
| | | 17 | | |
| | | 18 | | public void FuzzTarget(ReadOnlySpan<byte> bytes) |
| | 0 | 19 | | { |
| | 0 | 20 | | using var poisonedBytes = PooledBoundedMemory<byte>.Rent(bytes, PoisonPagePlacement.After); |
| | 0 | 21 | | using var poisonedChars = PooledBoundedMemory<char>.Rent(MemoryMarshal.Cast<byte, char>(bytes), PoisonPagePl |
| | | 22 | | |
| | 0 | 23 | | if (IPAddress.IsValidUtf8(poisonedBytes.Span)) |
| | 0 | 24 | | { |
| | 0 | 25 | | TestValidInput(bytes: poisonedBytes.Span); |
| | 0 | 26 | | } |
| | | 27 | | else |
| | 0 | 28 | | { |
| | 0 | 29 | | Assert.False(IPAddress.TryParse(poisonedBytes.Span, out _)); |
| | 0 | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | if (IPAddress.IsValid(poisonedChars.Span)) |
| | 0 | 33 | | { |
| | 0 | 34 | | TestValidInput(chars: poisonedChars.Span.ToString()); |
| | 0 | 35 | | } |
| | | 36 | | else |
| | 0 | 37 | | { |
| | 0 | 38 | | Assert.False(IPAddress.TryParse(poisonedChars.Span, out _)); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | static void TestValidInput(ReadOnlySpan<byte> bytes = default, string? chars = null) |
| | 0 | 42 | | { |
| | 0 | 43 | | if (chars is null) |
| | 0 | 44 | | { |
| | | 45 | | // bytes past the '%' may not be valid UTF-8: https://github.com/dotnet/runtime/issues/111288 |
| | 0 | 46 | | int percentIndex = bytes.IndexOf((byte)'%'); |
| | 0 | 47 | | Assert.True(Utf8.IsValid(bytes.Slice(0, percentIndex < 0 ? bytes.Length : percentIndex))); |
| | | 48 | | |
| | 0 | 49 | | chars = Encoding.UTF8.GetString(bytes); |
| | 0 | 50 | | } |
| | | 51 | | else |
| | 0 | 52 | | { |
| | 0 | 53 | | bytes = Encoding.UTF8.GetBytes(chars); |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | Assert.True(IPAddress.IsValid(chars)); |
| | 0 | 57 | | Assert.True(IPAddress.TryParse(chars, out IPAddress? ipFromChars)); |
| | 0 | 58 | | Assert.True(IPAddress.TryParse(bytes, out IPAddress? ipFromBytes)); |
| | | 59 | | |
| | 0 | 60 | | Assert.True(ipFromChars.Equals(ipFromBytes)); |
| | 0 | 61 | | Assert.True(ipFromBytes.Equals(ipFromChars)); |
| | | 62 | | |
| | 0 | 63 | | Assert.True(IPAddress.IsValid(ipFromChars.ToString())); |
| | | 64 | | |
| | 0 | 65 | | TestUri(chars); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | static void TestUri(string chars) |
| | 0 | 69 | | { |
| | 0 | 70 | | bool isIpv6 = chars.Contains(':'); |
| | 0 | 71 | | UriHostNameType hostNameType = isIpv6 ? UriHostNameType.IPv6 : UriHostNameType.IPv4; |
| | | 72 | | |
| | 0 | 73 | | if (isIpv6) |
| | 0 | 74 | | { |
| | | 75 | | // Remove the ScopeId |
| | 0 | 76 | | int percentIndex = chars.IndexOf('%'); |
| | 0 | 77 | | if (percentIndex >= 0) |
| | 0 | 78 | | { |
| | 0 | 79 | | chars = chars.Substring(0, percentIndex); |
| | 0 | 80 | | if (chars.StartsWith('[')) |
| | 0 | 81 | | { |
| | 0 | 82 | | chars = $"{chars}]"; |
| | 0 | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | if (!chars.StartsWith('[')) |
| | 0 | 87 | | { |
| | 0 | 88 | | chars = $"[{chars}]"; |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | // Remove the port |
| | 0 | 92 | | int bracketIndex = chars.IndexOf(']'); |
| | 0 | 93 | | if (bracketIndex >= 0 && |
| | 0 | 94 | | bracketIndex + 1 < chars.Length && |
| | 0 | 95 | | chars[bracketIndex + 1] == ':') |
| | 0 | 96 | | { |
| | 0 | 97 | | chars = chars.Substring(0, bracketIndex + 1); |
| | 0 | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | Assert.True(Uri.TryCreate($"http://{chars}/", UriKind.Absolute, out Uri? uri)); |
| | 0 | 102 | | Assert.Equal(hostNameType, uri.HostNameType); |
| | 0 | 103 | | Assert.Equal(hostNameType, Uri.CheckHostName(chars)); |
| | 0 | 104 | | } |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |