Repository navigation
Avoid adding interface addresses to localhost on Android - #128771
simonrozsival wants to merge 5 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 <223556219+Copilot@users.noreply.github.com>
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 <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
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). |
|
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, |
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
It sounds like a technicality I would hesitate on relying in practice. I think we should handle both names the same |
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
e4abb47e4ae220f7dd695dd79e45b2ca912799ec↑ Browser and WASI builds succeed.
↓ No physical Android device, Windows, or timing benchmark was run.
localhost.↑ The same regression tests pass with the native guard.
↓ The original CI device configuration was not independently reproduced.
Summary
On Android, the machine hostname can be
localhostorlocalhost.. When a named lookup matched that hostname, the native resolver appended local interface addresses to the OS result, causing localhost lookups to return non-loopback addresses. This change prevents that augmentation for explicit localhost lookups while preserving local-machine lookups and proxy bypass behavior.localhostis a relative name and can be subject to resolver search domains;localhost.is absolute. The existing managed resolver asks the OS for the original name first and falls back tolocalhostonly when a trailing-dot localhost name or a*.localhostname fails or yields no addresses. This PR does not normalize successful OS answers or guarantee that custom OS results are loopback. See RFC 6761 §6.3.Changes
localhostandlocalhost.lookups on Android, case-insensitively.Dns.GetHostEntry("")can still include local interface addresses when the machine hostname resolves directly.WebProxy.BypassProxyOnLocalfor local interface addresses. If an Androidlocalhost.machine hostname cannot be resolved directly, fall back tolocalhostas the previous named lookup did.GetHostEntrytest and add bare-name, trailing-dot, empty-name, and proxy regression coverage.Compatibility: When Android's actual hostname is
localhostorlocalhost.,Dns.GetHostEntry(Dns.GetHostName())can no longer include interface addresses added by .NET. EnumerateNetworkInterfaceaddresses instead, or useDns.GetHostEntry("")when the hostname resolves directly. A dotted machine hostname that the OS cannot resolve directly can still make the empty-name lookup fail. This behavior change will need breaking-change documentation after merge.Validation
./build.sh libs -os android -arch arm64 -rc Release -lc Release— passed. Android ARM64 NativeAOT on API 29 and API 35: NameResolution functional tests each passed 128 with 47 skipped; WebProxy tests each passed 51 with 1 skipped. API 35 NameResolution PAL and unit suites passed 34 and 171 tests, respectively.localhost.: the directly resolvable case passed 126 NameResolution functional tests with 49 skipped and all 52 WebProxy tests; the non-resolvable fallback passed 51 WebProxy tests with 1 skipped. The relevant proxy regression cases ran in both configurations../build.sh libs -os osx -arch arm64 -rc Release -lc Release— passed. macOS ARM64 NameResolution functional/PAL/unit suites passed 186/34/171 tests, with 13 functional skips; WebProxy passed 50 with 2 skips../build.sh clr+libs -os browser -c Releaseand./build.sh clr+libs -os wasi -c Release— passed. Browser and WASI CoreCLR helper regeneration produced no changes.Resolves #137979
Note
This pull request was authored with the assistance of GitHub Copilot.