Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
2c217a3
First integration of Resolver sources
rzikm Oct 4, 2024
704e33c
Allow mocking
rzikm Oct 4, 2024
aff6d17
Fix dispose
rzikm Oct 4, 2024
15db8a1
Fix tests
rzikm Oct 4, 2024
6bda235
Actually run ResolvConf tests
rzikm Oct 4, 2024
6fc61e0
Add retry functionality
rzikm Oct 8, 2024
2b6d91e
Add logging
rzikm Oct 10, 2024
2137954
Fix Microsoft.Extensions.ServiceDiscovery.Yarp.Tests
rzikm Oct 10, 2024
c204b01
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Nov 26, 2024
2fc7de4
Activity WIP
rzikm Nov 26, 2024
9b4de29
Fix reading CNAME with pointers in domain name segments
rzikm Nov 28, 2024
3ad5a75
Reenable telemetry
rzikm Nov 28, 2024
40f8f9b
Last changes to telemetry
rzikm Nov 28, 2024
e385605
Use strong RNG for Transaction ID generation
rzikm Jan 15, 2025
3cf0a9d
Check against too long domain name
rzikm Feb 3, 2025
6016e41
Disallow pointer to pointer.
rzikm Feb 3, 2025
f8c189d
Code review feedback (easy fixes)
rzikm Feb 3, 2025
d929889
More code review feedback
rzikm Feb 6, 2025
00e41d7
Fix increment
rzikm Feb 6, 2025
7267fb7
Detect CNAME loops
rzikm Feb 7, 2025
c1b76d3
Handle empty Tcp fallback responses and lack of TCP failover support …
rzikm Feb 7, 2025
86490cc
Dispose result.Response when overwritten by another result.
rzikm Feb 7, 2025
92b2905
Move DnsMessageHeader parsing to DnsPrimitives
rzikm Feb 11, 2025
fdf1904
Rework and add tests to retries and failover
rzikm Feb 12, 2025
61814e7
Test failover after exhausting attempts on one server
rzikm Feb 13, 2025
82a8108
Streamline options, remove unsupported options from ResolverOptions
rzikm Feb 13, 2025
fa627fd
Better handling of malformed responses
rzikm Feb 13, 2025
4ec79ca
Code review feedback
rzikm Mar 25, 2025
123375d
More feedback
rzikm Mar 27, 2025
6311616
Guarantee linear parsing of CNAME chains
rzikm Mar 27, 2025
c57a8c5
Fix decoding compressed CNAME in TCP fallback
rzikm Mar 27, 2025
83ca958
More code coverage
rzikm May 12, 2025
03e6a99
Correctly handle IDN names
rzikm May 15, 2025
46418fe
Minor fixes
rzikm May 22, 2025
4075e4f
Add Fuzzing tests for DNS resolver
rzikm May 23, 2025
56cd169
Improve fuzzing of EncodedDomainName
rzikm May 23, 2025
a19e8a2
Merge remote-tracking branch 'upstream/main' into managed-resolver
rzikm Jun 10, 2025
5d5edae
Remove commented out code
rzikm Jun 10, 2025
3c387e9
Fix build
rzikm Jun 10, 2025
c6e3d63
Fix Yarp service discovery tests
rzikm Jun 10, 2025
e66229a
Lazy TCP socket allocation in LoopbackTests
rzikm Jun 12, 2025
da018f7
LoopbackTestBaseLogging
rzikm Jun 12, 2025
2697412
fixup! LoopbackTestBaseLogging
rzikm Jun 12, 2025
214cc9b
Downgrade SharpFuzz
rzikm Jun 17, 2025
0d891cb
Fix buffer use after returning to pool
rzikm Jun 18, 2025
46cf262
Merge branch 'main' into managed-resolver
rzikm Jun 18, 2025
35a5809
Fix build
rzikm Jun 18, 2025
fcf18cf
Merge branch 'main' into managed-resolver
rzikm Jun 25, 2025
df785d1
Remove DnsClient package ref
rzikm Jun 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check against too long domain name
  • Loading branch information
rzikm committed Feb 3, 2025
commit 3cf0a9d28d9bd05ddd98fde71591bc7ca543827f
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Microsoft.Extensions.ServiceDiscovery.Dns.Resolver;

internal static class DnsPrimitives
{
internal const int MaxDomainNameLength = 253;

internal static bool TryWriteQName(Span<byte> destination, string name, out int written)
{
//
Expand Down Expand Up @@ -103,7 +105,15 @@ private static bool TryReadQNameCore(StringBuilder sb, ReadOnlySpan<byte> messag
{
sb.Append('.');
}

sb.Append(Encoding.ASCII.GetString(messageBuffer.Slice(currentOffset + 1, length)));

if (sb.Length > MaxDomainNameLength)
{
// domain name is too long
return false;
}

currentOffset += 1 + length;
bytesRead += 1 + length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
Expand All @@ -14,7 +15,6 @@ namespace Microsoft.Extensions.ServiceDiscovery.Dns.Resolver;

internal partial class DnsResolver : IDnsResolver, IDisposable
{
private const int MaximumNameLength = 253;
private const int IPv4Length = 4;
private const int IPv6Length = 16;

Expand Down Expand Up @@ -66,6 +66,8 @@ public async ValueTask<ServiceResult[]> ResolveServiceAsync(string name, Cancell
ObjectDisposedException.ThrowIf(_disposed, this);
cancellationToken.ThrowIfCancellationRequested();

name = GetNormalizedHostName(name);

NameResolutionActivity activity = Telemetry.StartNameResolution(name, QueryType.SRV, _timeProvider.GetTimestamp());
SendQueryResult result = await SendQueryWithRetriesAsync(name, QueryType.SRV, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -118,7 +120,7 @@ public async ValueTask<ServiceResult[]> ResolveServiceAsync(string name, Cancell

public async ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, CancellationToken cancellationToken = default)
{
if (name == "localhost")
if (string.Equals(name, "localhost", StringComparison.OrdinalIgnoreCase))
{
// name localhost exists outside of DNS and can't be resolved by a DNS server
int len = (Socket.OSSupportsIPv4 ? 1 : 0) + (Socket.OSSupportsIPv6 ? 1 : 0);
Expand Down Expand Up @@ -157,7 +159,7 @@ public async ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, Add
throw new ArgumentOutOfRangeException(nameof(addressFamily), addressFamily, "Invalid address family");
}

if (name == "localhost")
if (string.Equals(name, "localhost", StringComparison.OrdinalIgnoreCase))
{
// name localhost exists outside of DNS and can't be resolved by a DNS server
if (addressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4)
Expand All @@ -172,10 +174,7 @@ public async ValueTask<AddressResult[]> ResolveIPAddressesAsync(string name, Add
return Array.Empty<AddressResult>();
}

if (name.Length > MaximumNameLength)
{
throw new ArgumentException("Name is too long", nameof(name));
}
name = GetNormalizedHostName(name);

var queryType = addressFamily == AddressFamily.InterNetwork ? QueryType.A : QueryType.AAAA;
NameResolutionActivity activity = Telemetry.StartNameResolution(name, queryType, _timeProvider.GetTimestamp());
Expand Down Expand Up @@ -623,4 +622,12 @@ public void Dispose()

return (pendingRequestsCts, DisposeTokenSource: false, pendingRequestsCts);
}

private static readonly IdnMapping s_idnMapping = new IdnMapping();

private static string GetNormalizedHostName(string name)
{
// TODO: better exception message
return s_idnMapping.GetAscii(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,32 @@ public void TryReadQName_ReservedBits()

Assert.False(DnsPrimitives.TryReadQName(data, 0, out _, out _));
}

[Theory]
[InlineData(253)]
[InlineData(254)]
[InlineData(255)]
public void TryReadQName_NameTooLong(int length)
{
// longest possible label is 63 bytes + 1 byte for length
byte[] labelData = new byte[64];
Array.Fill(labelData, (byte)'a');
labelData[0] = 63;

int remainder = length - 3 * 64;

byte[] lastLabelData = new byte[remainder + 1];
Array.Fill(lastLabelData, (byte)'a');
lastLabelData[0] = (byte)remainder;

byte[] data = Enumerable.Repeat(labelData, 3).SelectMany(x => x).Concat(lastLabelData).Concat(new byte[1]).ToArray();
if (length > 253)
{
Assert.False(DnsPrimitives.TryReadQName(data, 0, out _, out _));
}
else
{
Assert.True(DnsPrimitives.TryReadQName(data, 0, out _, out _));
}
}
}