Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
<data name="net_invalid_ip_addr" xml:space="preserve">
<value>IPv4 address 0.0.0.0 and IPv6 address ::0 are unspecified addresses that cannot be used as a target address.</value>
</data>
<data name="net_hostname_invalid_character" xml:space="preserve">
<value>The host name cannot contain a null character.</value>
</data>
<data name="SystemNetNameResolution_PlatformNotSupported" xml:space="preserve">
<value>System.Net.NameResolution is not supported on this platform.</value>
</data>
Expand Down
7 changes: 7 additions & 0 deletions src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,13 @@ private static void ValidateHostName(string hostName)
throw new ArgumentOutOfRangeException(nameof(hostName),
SR.Format(SR.net_toolong, nameof(hostName), MaxHostName.ToString(NumberFormatInfo.CurrentInfo)));
}

// The hostname is passed to native APIs that treat '\0' as the end of the string,
// so embedded null characters would silently truncate the name. Reject them up front.
if (hostName.Contains('\0'))
{
throw new ArgumentException(SR.net_hostname_invalid_character, nameof(hostName));
}
}

private static bool LogFailure(object hostNameOrAddress, in NameResolutionActivity activity, Exception exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ public async Task DnsGetHostEntry_BadName_ThrowsArgumentOutOfRangeException_Obso
await Assert.ThrowsAnyAsync<ArgumentOutOfRangeException>(() => Task.Factory.FromAsync(Dns.BeginGetHostEntry, Dns.EndGetHostEntry, hostNameOrAddress, null));
}

[Theory]
[InlineData("\0")]
[InlineData("\0host")]
[InlineData("host\0")]
[InlineData("ho\0st")]
[InlineData("host\0name")]
public async Task DnsGetHostEntry_NullCharacterInName_ThrowsArgumentException(string hostNameOrAddress)
{
Assert.Throws<ArgumentException>(() => Dns.GetHostEntry(hostNameOrAddress));
Assert.Throws<ArgumentException>(() => Dns.GetHostAddresses(hostNameOrAddress));
await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostEntryAsync(hostNameOrAddress));
await Assert.ThrowsAsync<ArgumentException>(() => Dns.GetHostAddressesAsync(hostNameOrAddress));
}

[Theory]
[InlineData(0)]
[InlineData(1)]
Expand Down
Loading