GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 4.1: Add system property to disable RFC 6761 localhost reso… · netty/netty@0c57ae7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • resolver-dns/src
    • main/java/io/netty/resolver/dns
      • DnsNameResolver.java
    • test/java/io/netty/resolver/dns
      • DnsNameResolverTest.java
      • IsolatedDnsNameResolverTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import io.netty.util.internal.EmptyArrays;
6363
import io.netty.util.internal.PlatformDependent;
6464
import io.netty.util.internal.StringUtil;
65+
import io.netty.util.internal.SystemPropertyUtil;
6566
import io.netty.util.internal.logging.InternalLogger;
6667
import io.netty.util.internal.logging.InternalLoggerFactory;
6768

@@ -130,6 +131,26 @@ public boolean isSharable() {
130131
}
131132
};
132133

134+
/**
135+
* System property to control RFC 6761 localhost resolution.
136+
* <p>
137+
* When {@code true} (the default), {@code localhost} and {@code *.localhost}
138+
* are resolved to the loopback address without querying DNS servers. When
139+
* {@code false}, only the pre-#16749 rules apply (Windows {@code localhost}
140+
* and the Windows machine hostname). Set to {@code false} when
141+
* {@code *.localhost} hostnames must resolve via DNS to non-loopback
142+
* addresses.
143+
*
144+
* @see <a href="https://github.com/netty/netty/issues/16744">Issue 16744</a>
145+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3">RFC 6761</a>
146+
*/
147+
private static final String RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY =
148+
"io.netty.resolver.dns.resolveLocalhostWithoutDns";
149+
150+
/** The runtime value of {@link #RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY}. */
151+
// Visible for testing only
152+
static boolean resolveLocalhostWithoutDns;
153+
133154
static final ResolvedAddressTypes DEFAULT_RESOLVE_ADDRESS_TYPES;
134155
static final String[] DEFAULT_SEARCH_DOMAINS;
135156
private static final UnixResolverOptions DEFAULT_OPTIONS;
@@ -176,6 +197,12 @@ public boolean isSharable() {
176197
}
177198
DEFAULT_OPTIONS = options;
178199
logger.debug("Default {}", DEFAULT_OPTIONS);
200+
201+
resolveLocalhostWithoutDns =
202+
SystemPropertyUtil.getBoolean(RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY, true);
203+
if (logger.isDebugEnabled()) {
204+
logger.debug("-D{}: {}", RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY, resolveLocalhostWithoutDns);
205+
}
179206
}
180207

181208
/**
@@ -728,6 +755,10 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) {
728755
* According to RFC 6761 Section 6.3, localhost and subdomains of localhost should be resolved to the loopback
729756
* address by name resolution libraries without querying DNS servers. The hostname of the local machine can usually
730757
* be resolved from the hosts file, but on Windows, this is no longer possible.
758+
* <p>
759+
* RFC 6761 behavior for {@code localhost} and {@code *.localhost} can be
760+
* disabled via the {@code io.netty.resolver.dns.resolveLocalhostWithoutDns}
761+
* system property.
731762
*
732763
* @param hostname the hostname that's being looked up
733764
* @return true if the hostname should point to the loopback adress. False otherwise.
@@ -742,10 +773,15 @@ private static boolean isLocalHostAddress(String hostname) {
742773
return true;
743774
}
744775

776+
if (!resolveLocalhostWithoutDns) {
777+
return PlatformDependent.isWindows() && LOCALHOST.equalsIgnoreCase(hostname);
778+
}
779+
745780
if (hostname.endsWith(".")) {
746781
hostname = hostname.substring(0, hostname.length() - 1);
747782
}
748-
return hostname.equalsIgnoreCase(LOCALHOST) || hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST);
783+
return hostname.equalsIgnoreCase(LOCALHOST)
784+
|| hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST);
749785
}
750786

751787
private InetAddress getLocalHostAddress() {
Collapse file

‎resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java‎

Copy file name to clipboardExpand all lines: resolver-dns/src/test/java/io/netty/resolver/dns/DnsNameResolverTest.java
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public class DnsNameResolverTest {
261261
DOMAINS_PUNYCODE.put("müller.de", "xn--mller-kva.de");
262262
}
263263

264-
private static final Set<String> DOMAINS_ALL;
264+
static final Set<String> DOMAINS_ALL;
265265

266266
static {
267267
Set<String> all = new HashSet<String>(DOMAINS.size() + DOMAINS_PUNYCODE.size());
@@ -393,7 +393,13 @@ private static DnsNameResolverBuilder newResolver(DnsNameResolverChannelStrategy
393393
private static DnsNameResolverBuilder newResolver(DnsNameResolverChannelStrategy strategy, boolean decodeToUnicode,
394394
DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
395395
TestDnsServer dnsServer) {
396-
DnsNameResolverBuilder builder = new DnsNameResolverBuilder(group.next())
396+
return newResolver(strategy, decodeToUnicode, dnsServerAddressStreamProvider, dnsServer, group.next());
397+
}
398+
399+
static DnsNameResolverBuilder newResolver(DnsNameResolverChannelStrategy strategy, boolean decodeToUnicode,
400+
DnsServerAddressStreamProvider dnsServerAddressStreamProvider,
401+
TestDnsServer dnsServer, EventLoop eventLoop) {
402+
DnsNameResolverBuilder builder = new DnsNameResolverBuilder(eventLoop)
397403
.dnsQueryLifecycleObserverFactory(new TestRecursiveCacheDnsQueryLifecycleObserverFactory())
398404
.datagramChannelType(NioDatagramChannel.class)
399405
.maxQueriesPerResolve(1)
@@ -2178,7 +2184,7 @@ private static void assertQueryObserver(DnsNameResolver resolver, DnsRecordType
21782184
}
21792185
}
21802186

2181-
private static final class TestRecursiveCacheDnsQueryLifecycleObserverFactory
2187+
static final class TestRecursiveCacheDnsQueryLifecycleObserverFactory
21822188
implements DnsQueryLifecycleObserverFactory {
21832189
final Queue<TestDnsQueryLifecycleObserver> observers =
21842190
new ConcurrentLinkedQueue<TestDnsQueryLifecycleObserver>();
Collapse file

‎resolver-dns/src/test/java/io/netty/resolver/dns/IsolatedDnsNameResolverTest.java‎

Copy file name to clipboard
+158Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright 2026 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.resolver.dns;
17+
18+
import io.netty.channel.EventLoopGroup;
19+
import io.netty.channel.nio.NioEventLoopGroup;
20+
import io.netty.resolver.HostsFileEntriesResolver;
21+
import io.netty.resolver.ResolvedAddressTypes;
22+
import io.netty.resolver.dns.DnsNameResolverTest.TestRecursiveCacheDnsQueryLifecycleObserverFactory;
23+
import io.netty.util.internal.PlatformDependent;
24+
import org.apache.directory.server.dns.messages.QuestionRecord;
25+
import org.apache.directory.server.dns.messages.RecordType;
26+
import org.apache.directory.server.dns.messages.ResourceRecord;
27+
import org.apache.directory.server.dns.store.RecordStore;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.parallel.Isolated;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.EnumSource;
33+
34+
import java.net.InetAddress;
35+
import java.util.Collections;
36+
import java.util.Set;
37+
38+
import static io.netty.resolver.dns.DnsNameResolverTest.DOMAINS_ALL;
39+
import static io.netty.resolver.dns.DnsNameResolverTest.newResolver;
40+
import static io.netty.resolver.dns.TestDnsServer.newARecord;
41+
import static org.assertj.core.api.Assumptions.assumeThat;
42+
import static org.junit.jupiter.api.Assertions.assertEquals;
43+
import static org.junit.jupiter.api.Assertions.assertFalse;
44+
45+
@Isolated
46+
public class IsolatedDnsNameResolverTest {
47+
private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL);
48+
private static final EventLoopGroup group = new NioEventLoopGroup(1);
49+
private static boolean existingLocalhostSetting;
50+
51+
@BeforeAll
52+
public static void captureSetting() throws Exception {
53+
existingLocalhostSetting = DnsNameResolver.resolveLocalhostWithoutDns;
54+
dnsServer.start();
55+
}
56+
57+
@AfterAll
58+
public static void restoreSetting() {
59+
try {
60+
dnsServer.stop();
61+
group.shutdownGracefully();
62+
} finally {
63+
DnsNameResolver.resolveLocalhostWithoutDns = existingLocalhostSetting;
64+
}
65+
}
66+
67+
@ParameterizedTest
68+
@EnumSource(DnsNameResolverChannelStrategy.class)
69+
public void testResolveDotLocalhostViaDnsWhenDisabled(DnsNameResolverChannelStrategy strategy) throws Exception {
70+
final String hostname = "myservice.localhost";
71+
final String expectedIp = "10.0.0.1";
72+
final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() {
73+
@Override
74+
public Set<ResourceRecord> getRecords(QuestionRecord question) {
75+
if (question.getRecordType() == RecordType.A) {
76+
String domainName = question.getDomainName();
77+
if (domainName.equalsIgnoreCase(hostname) || domainName.equalsIgnoreCase(hostname + '.')) {
78+
return Collections.singleton(newARecord(hostname, expectedIp));
79+
}
80+
}
81+
return Collections.emptySet();
82+
}
83+
});
84+
customDnsServer.start();
85+
DnsNameResolver.resolveLocalhostWithoutDns = false;
86+
try {
87+
DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next())
88+
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
89+
.hostsFileEntriesResolver(new HostsFileEntriesResolver() {
90+
@Override
91+
public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
92+
return null;
93+
}
94+
})
95+
.build();
96+
try {
97+
InetAddress address = resolver.resolve(hostname).syncUninterruptibly().getNow();
98+
assertEquals(expectedIp, address.getHostAddress());
99+
100+
TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
101+
(TestRecursiveCacheDnsQueryLifecycleObserverFactory)
102+
resolver.dnsQueryLifecycleObserverFactory();
103+
assertFalse(lifecycleObserverFactory.observers.isEmpty());
104+
} finally {
105+
resolver.close();
106+
}
107+
} finally {
108+
customDnsServer.stop();
109+
}
110+
}
111+
112+
@ParameterizedTest
113+
@EnumSource(DnsNameResolverChannelStrategy.class)
114+
public void testResolveLocalhostViaDnsWhenDisabledOnNonWindows(DnsNameResolverChannelStrategy strategy)
115+
throws Exception {
116+
assumeThat(PlatformDependent.isWindows()).isFalse();
117+
118+
final String expectedIp = "10.0.0.2";
119+
final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() {
120+
@Override
121+
public Set<ResourceRecord> getRecords(QuestionRecord question) {
122+
if (question.getRecordType() == RecordType.A) {
123+
String domainName = question.getDomainName();
124+
if (domainName.equalsIgnoreCase("localhost") || domainName.equalsIgnoreCase("localhost.")) {
125+
return Collections.singleton(newARecord("localhost", expectedIp));
126+
}
127+
}
128+
return Collections.emptySet();
129+
}
130+
});
131+
customDnsServer.start();
132+
DnsNameResolver.resolveLocalhostWithoutDns = false;
133+
try {
134+
DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next())
135+
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
136+
.hostsFileEntriesResolver(new HostsFileEntriesResolver() {
137+
@Override
138+
public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
139+
return null;
140+
}
141+
})
142+
.build();
143+
try {
144+
InetAddress address = resolver.resolve("localhost").syncUninterruptibly().getNow();
145+
assertEquals(expectedIp, address.getHostAddress());
146+
147+
TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
148+
(TestRecursiveCacheDnsQueryLifecycleObserverFactory)
149+
resolver.dnsQueryLifecycleObserverFactory();
150+
assertFalse(lifecycleObserverFactory.observers.isEmpty());
151+
} finally {
152+
resolver.close();
153+
}
154+
} finally {
155+
customDnsServer.stop();
156+
}
157+
}
158+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.