Skip to content

Validate Dns hostname for embedded null characters#128982

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-dns-gethostentry-validation
Draft

Validate Dns hostname for embedded null characters#128982
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-dns-gethostentry-validation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 4, 2026

Dns.GetHostEntry and friends forwarded hostnames containing \0 to native resolvers (getaddrinfo/gethostbyname), which treat \0 as end-of-string. The name was silently truncated and resolution failed in confusing ways instead of being rejected up front.

Changes

  • Dns.ValidateHostName — added a Contains('\0') check that throws ArgumentException with parameter name hostName. This is the single funnel reached from both the sync (GetHostEntryOrAddressesCore) and async (GetHostEntryOrAddressesCoreAsync) string code paths, so one check covers GetHostEntry, GetHostAddresses, their Async/Begin variants, and the obsolete GetHostByName/Resolve overloads. IP-literal fast paths are unaffected (an IP literal cannot contain \0).
  • Strings.resx — added net_hostname_invalid_character ("The host name cannot contain a null character.").
  • GetHostEntryTest.cs — added DnsGetHostEntry_NullCharacterInName_ThrowsArgumentException theory exercising leading/trailing/embedded null patterns against the sync and async entry points.
// Before: silently truncated, then SocketException from the OS resolver
Dns.GetHostEntry("foo\0bar");

// After:
// System.ArgumentException: The host name cannot contain a null character. (Parameter 'hostName')

Copilot AI self-assigned this Jun 4, 2026
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI linked an issue Jun 4, 2026 that may be closed by this pull request
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 4, 2026 11:58
@MihaZupan MihaZupan assigned mrek-msft and unassigned MihaZupan Jun 4, 2026
Copilot AI changed the title [WIP] Fix Dns.GetHostEntry to validate for null characters Validate Dns hostname for embedded null characters Jun 4, 2026
Copilot AI requested a review from MihaZupan June 4, 2026 12:18
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dns.GetHostEntry do not validate for null characters

3 participants