Repository navigation
Resolve the localhost name family to loopback on Android - #128771
simonrozsival wants to merge 3 commits into
Conversation
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
|
/azp run runtime-android |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR adjusts name resolution behavior so the localhost name family behaves consistently with RFC 6761 expectations on Android, and adds managed normalization/fallback logic to make localhost. and *.localhost variants resolve more reliably across platforms.
Changes:
- Native (Android-only): prevents interface-address augmentation from polluting
localhostresults when Android’sgethostname()is"localhost". - Managed (all platforms): normalizes exact
"localhost."→"localhost"and falls back from*.localhostto plainlocalhostwhen the OS resolver fails or returns no addresses. - Tests: updates functional tests to require loopback for
*.localhoston Android (while still allowing Apple mobile exceptions) and adds/clarifies trailing-dot coverage.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/native/libs/System.Native/pal_networking.c | Adds ShouldAugmentWithInterfaceAddresses to skip Android augmentation for localhost, preventing non-loopback leakage. |
| src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs | Normalizes localhost. and implements RFC 6761-style fallback for *.localhost when OS resolution fails/returns empty. |
| src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs | Updates localhost-subdomain expectations (Android now requires loopback) and documents/validates localhost. behavior. |
| src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostAddressesTest.cs | Same as above for GetHostAddresses, including trailing-dot and subdomain coverage. |
On Android the "localhost" name family did not resolve to loopback as
required by RFC 6761 Section 6.3:
- "localhost" could return non-loopback addresses because interface
addresses leaked into the result.
- "localhost." (fully-qualified form) and "*.localhost" subdomains failed
with SocketException, since Android's getaddrinfo does not strip the
trailing dot and has no /etc/hosts entry for subdomains.
Native (pal_networking.c, Android-only):
SystemNative_GetHostEntryForName augments getaddrinfo results with local
interface addresses (getifaddrs) when the queried name equals
gethostname(). On Android gethostname() can return "localhost", polluting
its resolution with non-loopback addresses. New helper
ShouldAugmentWithInterfaceAddresses skips augmentation for "localhost" on
Android only.
Managed (Dns.cs, all platforms):
- Normalize an exact "localhost." to "localhost" at both the sync and
async resolution cores so the fully-qualified form resolves identically
to "localhost". This is a no-op on glibc/Windows, which already strip
the trailing dot.
- For "*.localhost" subdomains, try the OS resolver first and fall back to
resolving plain "localhost" if it fails or returns no addresses. The
fallback target is not a subdomain, so it cannot recurse.
Tests:
Require loopback for "*.localhost" on Android, and add coverage for
"localhost." and subdomain-with-trailing-dot forms.
Co-authored-by: Copilot <[email protected]>
bba7bfb to
9997901
Compare
System.Net Code Review — DNS / RFC 6761 localhost resolution on AndroidProtocols: DNS resolution (RFC 6761 §6.3) Summary
AnalysisThe two-layer fix is well-designed:
RFC 6761 compliance: ✅ Section 6.3 states localhost names SHOULD resolve to loopback. Both the FQDN normalization and the subdomain fallback correctly implement this. Security: ✅ The change tightens behavior — prevents non-loopback addresses from appearing in localhost resolution. Edge cases: The native Tests: ✅ Android exclusions properly removed; loopback is now asserted on Android. Verdict✅ LGTM — Clean, well-documented fix with correct RFC references and appropriate platform scoping.
|
|
/azp run runtime-android |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: simonrozsival <[email protected]>
Co-authored-by: simonrozsival <[email protected]>
Merge conflicts resolved in commit
|
|
Azure Pipelines: Successfully started running 2 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
/azp run runtime-android |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
| // The subdomain goes to OS resolver first. If it fails (likely on most systems), | ||
| // it falls back to resolving plain "localhost", which should return loopback addresses. | ||
| // On Android/Apple mobile platforms the OS resolver may return non-loopback addresses | ||
| // On Apple mobile platforms the OS resolver may return non-loopback addresses | ||
| // for *.localhost. | ||
| bool requireLoopback = !PlatformDetection.IsAppleMobile && !PlatformDetection.IsAndroid; | ||
| bool requireLoopback = !PlatformDetection.IsAppleMobile; |
| // Augment the result with the local interface addresses when resolving the machine's own | ||
| // host name (see ShouldAugmentWithInterfaceAddresses for platform-specific exceptions). | ||
| if (result == 0 && ShouldAugmentWithInterfaceAddresses((const char*)address, name)) |
|
Workflow state for the Holistic Review Orchestrator. {
"version": 5,
"last_dispatched_commit": "aae18c65d4f5bd90cb5fc0c58cde45ba44264427",
"last_dispatched_base_ref": "main",
"last_dispatched_base_sha": "6e0eb8307f56dc6fea81aa4cce2cf40c1013fa4d",
"last_reviewed_commit": "aae18c65d4f5bd90cb5fc0c58cde45ba44264427",
"last_reviewed_base_ref": "main",
"last_reviewed_base_sha": "6e0eb8307f56dc6fea81aa4cce2cf40c1013fa4d",
"last_recorded_worker_run_id": "29685640739",
"review_attempt_commit": "",
"review_attempt_base_ref": "",
"review_attempt_count": 0,
"max_review_attempts": 5,
"review_history_format": "holistic-review-disclosure-v1",
"review_history": [
{
"commit": "aae18c65d4f5bd90cb5fc0c58cde45ba44264427",
"review_id": 4730700694
}
]
} |
There was a problem hiding this comment.
Holistic Review
Motivation: On Android the localhost name family did not follow RFC 6761 §6.3. gethostname() can return "localhost", so the existing augmentation logic in SystemNative_GetHostEntryForName (which appends getifaddrs() interface addresses when the queried name matches the host name) leaked non-loopback interface addresses into localhost resolution. This PR's in-scope changes address the native augmentation and tighten the corresponding tests.
Approach: A new ShouldAugmentWithInterfaceAddresses helper replaces the inline strcasecmp(address, name) == 0 check. It preserves the prior behavior on all platforms and additionally, on Android only (TARGET_ANDROID), suppresses interface-address augmentation when the queried name is "localhost". The helper and its call site are correctly kept under the existing #if HAVE_GETIFADDRS guard. The test files relax the requireLoopback predicate from !IsAppleMobile && !IsAndroid to !IsAppleMobile, so Android now asserts the stronger, RFC-compliant loopback expectation for *.localhost, localhost., and subdomain-with-trailing-dot cases.
Summary: The in-scope changes are small, correct, and well-scoped. The Android-only carve-out is appropriately gated by TARGET_ANDROID and cannot affect glibc/Windows/Apple behavior. Because the managed layer normalizes localhost. to localhost and falls back *.localhost → localhost, the native helper only ever sees the bare localhost string on the affected path, so the strcasecmp(address, "localhost") guard covers the whole family. The test changes correctly turn Android into a positive (loopback-required) assertion rather than a permissive any-address check, improving coverage. No correctness, security, or performance concerns in the reviewed range.
Note: the PR description references managed Dns.cs changes (normalization and subdomain fallback), but the reviewed base-to-head range (6e0eb83..aae18c6) contains only the native pal_networking.c change and the two test files; the managed changes are not part of this range and were not reviewed here.
Verdict: LGTM.
Note
This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.
Generated by Holistic Review · 58 AIC · ⌖ 10.4 AIC · ⊞ 10K
|
conceptually, |
Note
This pull request was authored with the assistance of GitHub Copilot.
Summary
On Android, the
localhostname family did not resolve to loopback as required by RFC 6761 §6.3:localhostcould return non-loopback addresses (interface addresses leaked into the result).localhost.(fully-qualified form with a trailing dot) and*.localhostsubdomains failed withSocketExceptionbecause Android'sgetaddrinfodoes not strip the trailing dot and has no/etc/hostsentry for subdomains.This PR makes the whole
localhostfamily (localhost,localhost.,*.localhost,*.localhost.) resolve to the loopback address, consistent with desktop glibc/Windows behavior.Changes
Native (
src/native/libs/System.Native/pal_networking.c) — Android-onlySystemNative_GetHostEntryForNameaugmentsgetaddrinforesults with local interface addresses (fromgetifaddrs) when the queried name equalsgethostname(). On Android,gethostname()can itself return"localhost", which caused non-loopback interface addresses to pollutelocalhostresolution. A new helperShouldAugmentWithInterfaceAddressesskips augmentation forlocalhoston Android only.Managed (
src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs) — all platformslocalhost.→localhost(NormalizeLocalhostName) at both the sync and async resolution cores, so the fully-qualified form resolves identically tolocalhost. This is platform-agnostic per the RFC; it only changes behavior on resolvers that don't strip the trailing dot themselves (Android), and is a no-op on glibc/Windows which already resolve both.*.localhostsubdomains: try the OS resolver first; if it fails or returns no addresses, fall back to resolving plainlocalhost(IsLocalhostSubdomain). The fallback targetlocalhostis not a subdomain, so the fallback cannot recurse.Tests (
tests/FunctionalTests/GetHostAddressesTest.cs,GetHostEntryTest.cs)*.localhostsubdomains now require loopback on Android (requireLoopback = !PlatformDetection.IsAppleMobile), and added/clarifiedlocalhost.-with-trailing-dot and subdomain-with-trailing-dot coverage.Validation
*_LocalhostWithTrailingDot_ReturnsLoopback(previously failing).