Skip to content

Avoid adding interface addresses to localhost on Android - #128771

Open
simonrozsival wants to merge 5 commits into
mainfrom
dev/simonrozsival/android-localhost-loopback-augmentation
Open

simonrozsival wants to merge 5 commits into
mainfrom
dev/simonrozsival/android-localhost-loopback-augmentation

Conversation

@simonrozsival

@simonrozsival simonrozsival commented May 29, 2026 •

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: e4abb47e4ae220f7dd695dd79e45b2ca912799ec

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ Regression and compatibility cases pass on two Android emulator versions and macOS.
↑ Browser and WASI builds succeed.
↓ No physical Android device, Windows, or timing benchmark was run.
Diagnosis confidence 9.5/10 ↑ Reproduced interface addresses being added when the Android hostname is localhost.
↑ The same regression tests pass with the native guard.
Evidence fit 9/10 ↑ Reproduced the reported trailing-dot test failures and eliminated them on Android ARM64 NativeAOT.
↓ The original CI device configuration was not independently reproduced.

Summary

On Android, the machine hostname can be localhost or localhost.. 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.

localhost is 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 to localhost only when a trailing-dot localhost name or a *.localhost name 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

  • Skip .NET's interface-address augmentation for explicit localhost and localhost. lookups on Android, case-insensitively.
  • Preserve the empty-name local-machine intent through an additive native entry point, so Dns.GetHostEntry("") can still include local interface addresses when the machine hostname resolves directly.
  • Preserve WebProxy.BypassProxyOnLocal for local interface addresses. If an Android localhost. machine hostname cannot be resolved directly, fall back to localhost as the previous named lookup did.
  • Re-enable the existing Android localhost GetHostEntry test and add bare-name, trailing-dot, empty-name, and proxy regression coverage.

Compatibility: When Android's actual hostname is localhost or localhost., Dns.GetHostEntry(Dns.GetHostName()) can no longer include interface addresses added by .NET. Enumerate NetworkInterface addresses instead, or use Dns.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.
  • API 35 with machine hostname 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 Release and ./build.sh clr+libs -os wasi -c Release — passed. Browser and WASI CoreCLR helper regeneration produced no changes.
  • No timing benchmark was run.

Resolves #137979

Note

This pull request was authored with the assistance of GitHub Copilot.

@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.

@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@github-actions

This comment has been minimized.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 localhost results when Android’s gethostname() is "localhost".
  • Managed (all platforms): normalizes exact "localhost." → "localhost" and falls back from *.localhost to plain localhost when the OS resolver fails or returns no addresses.
  • Tests: updates functional tests to require loopback for *.localhost on 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>
@simonrozsival
simonrozsival force-pushed the dev/simonrozsival/android-localhost-loopback-augmentation branch from bba7bfb to 9997901 Compare May 29, 2026 16:30
@github-actions

Copy link
Copy Markdown
Contributor

System.Net Code Review — DNS / RFC 6761 localhost resolution on Android

Protocols: DNS resolution (RFC 6761 §6.3)
Platforms: Android (native fix), all platforms (managed normalization)

Summary

Severity Finding
💡 Trivial Non-Android Linux with hostname "localhost" would still augment — pre-existing, not a realistic config

Analysis

The two-layer fix is well-designed:

  1. Native (pal_networking.c) — ShouldAugmentWithInterfaceAddresses correctly prevents interface addresses from leaking into localhost resolution when Android's gethostname() returns "localhost". The #ifdef TARGET_ANDROID guard is appropriately scoped.

  2. Managed (Dns.cs) — NormalizeLocalhostName strips the trailing dot from "localhost." before calling the OS resolver. This is correct per DNS conventions (trailing dot = FQDN root notation) and fixes Android's getaddrinfo("localhost.") failure without regressing glibc/Windows which already handle both forms.

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 strcasecmp comparisons are safe (constant strings, no buffer concerns). NormalizeLocalhostName is pure/stateless — no concurrency issues.

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.

Generated by Code Review for issue #128771 · ● 4.4M · ◷

@simonrozsival simonrozsival changed the title Resolve the localhost name family to loopback on Android (RFC 6761) Resolve the localhost name family to loopback on Android May 29, 2026
@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 16, 2026 10:03
@simonrozsival
simonrozsival removed the request for review from Copilot July 16, 2026 10:03
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 16, 2026 10:09
@simonrozsival
simonrozsival removed the request for review from Copilot July 16, 2026 10:09

Copilot AI commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Merge conflicts resolved in commit aae18c65. The key resolutions:

  • Dns.cs: The main branch already incorporated equivalent functionality via ShouldFallbackToLocalhost (commit c41ce9b7e), which supersedes our NormalizeLocalhostName+IsLocalhostSubdomain approach. The managed code is now identical to main (no delta), and the two NormalizeLocalhostName call sites that were left behind are removed.
  • Test files: Resolved comment-only conflicts by taking main's wording; our functional changes (removing the !PlatformDetection.IsAndroid loopback exemptions) are preserved.
  • pal_networking.c: Auto-merged cleanly — our Android ShouldAugmentWithInterfaceAddresses fix is intact.

@simonrozsival
simonrozsival marked this pull request as ready for review July 16, 2026 19:54
Copilot AI review requested due to automatic review settings July 16, 2026 19:54
Copilot AI temporarily deployed to copilot-pat-pool July 16, 2026 19:54 Inactive
@azure-pipelines

Copy link
Copy Markdown
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.

@simonrozsival

Copy link
Copy Markdown
Member Author

/azp run runtime-android

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 2

@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

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
    }
  ]
}

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@wfurt

wfurt commented Nov 20, 2026

Copy link
Copy Markdown
Member

conceptually, localhost and localhost. are two different names. The first is not FQDN and could/should get also default domain appended (or list in search domains). I don't know if that matters in practice. Any thoughts on this @rzikm?

simonrozsival and others added 2 commits November 30, 2026 13:25
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Nov 30, 2026
@simonrozsival simonrozsival changed the title Resolve the localhost name family to loopback on Android Avoid adding interface addresses to localhost on Android Nov 30, 2026
@MichalStrehovsky
MichalStrehovsky removed their request for review December 1, 2026 03:17
@rzikm

rzikm commented Dec 1, 2026

Copy link
Copy Markdown
Member

conceptually, localhost and localhost. are two different names. The first is not FQDN and could/should get also default domain appended (or list in search domains). I don't know if that matters in practice. Any thoughts on this @rzikm?

It sounds like a technicality I would hesitate on relying in practice. I think we should handle both names the same

This branch was previously deployed

1 inactive (outdated) deployment
copilot-pat-pool — aae18c65 Deployed Jul 16, 2026 by Copilot via pat_pool #15330
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net linkable-framework Issues associated with delivering a linker friendly framework os-android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ci-scan] Test failure: Android localhost-dot DNS returns a non-loopback address

5 participants