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 5.0: Add system property to disable RFC 6761 localhost reso… · netty/netty@bc9a8f0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • resolver-dns
    • pom.xml
    • 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/pom.xml‎

Copy file name to clipboardExpand all lines: resolver-dns/pom.xml
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<artifactId>apacheds-protocol-dns</artifactId>
7474
<scope>test</scope>
7575
</dependency>
76+
<dependency>
77+
<groupId>org.jetbrains</groupId>
78+
<artifactId>annotations</artifactId>
79+
</dependency>
7680

7781
<!-- Automatic native-image reflection metadata generation for handlers dependencies -->
7882
<dependency>
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
+38-1Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@
5858
import io.netty.util.internal.EmptyArrays;
5959
import io.netty.util.internal.PlatformDependent;
6060
import io.netty.util.internal.StringUtil;
61+
import io.netty.util.internal.SystemPropertyUtil;
6162
import io.netty.util.internal.logging.InternalLogger;
6263
import io.netty.util.internal.logging.InternalLoggerFactory;
64+
import org.jetbrains.annotations.VisibleForTesting;
6365

6466
import java.net.IDN;
6567
import java.net.Inet4Address;
@@ -125,6 +127,26 @@ public boolean isSharable() {
125127
}
126128
};
127129

130+
/**
131+
* System property to control RFC 6761 localhost resolution.
132+
* <p>
133+
* When {@code true} (the default), {@code localhost} and {@code *.localhost}
134+
* are resolved to the loopback address without querying DNS servers. When
135+
* {@code false}, only the pre-#16749 rules apply (Windows {@code localhost}
136+
* and the Windows machine hostname). Set to {@code false} when
137+
* {@code *.localhost} hostnames must resolve via DNS to non-loopback
138+
* addresses.
139+
*
140+
* @see <a href="https://github.com/netty/netty/issues/16744">Issue 16744</a>
141+
* @see <a href="https://www.rfc-editor.org/rfc/rfc6761.html#section-6.3">RFC 6761</a>
142+
*/
143+
private static final String RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY =
144+
"io.netty.resolver.dns.resolveLocalhostWithoutDns";
145+
146+
/** The runtime value of {@link #RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY}. */
147+
@VisibleForTesting
148+
static boolean resolveLocalhostWithoutDns;
149+
128150
static final ResolvedAddressTypes DEFAULT_RESOLVE_ADDRESS_TYPES;
129151
static final String[] DEFAULT_SEARCH_DOMAINS;
130152
private static final UnixResolverOptions DEFAULT_OPTIONS;
@@ -170,6 +192,12 @@ public boolean isSharable() {
170192
}
171193
DEFAULT_OPTIONS = options;
172194
logger.debug("Default {}", DEFAULT_OPTIONS);
195+
196+
resolveLocalhostWithoutDns =
197+
SystemPropertyUtil.getBoolean(RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY, true);
198+
if (logger.isDebugEnabled()) {
199+
logger.debug("-D{}: {}", RESOLVE_LOCALHOST_WITHOUT_DNS_PROPERTY, resolveLocalhostWithoutDns);
200+
}
173201
}
174202

175203
/**
@@ -715,6 +743,10 @@ private List<InetAddress> resolveHostsFileEntries(String hostname) {
715743
* According to RFC 6761 Section 6.3, localhost and subdomains of localhost should be resolved to the loopback
716744
* address by name resolution libraries without querying DNS servers. The hostname of the local machine can usually
717745
* be resolved from the hosts file, but on Windows, this is no longer possible.
746+
* <p>
747+
* RFC 6761 behavior for {@code localhost} and {@code *.localhost} can be
748+
* disabled via the {@code io.netty.resolver.dns.resolveLocalhostWithoutDns}
749+
* system property.
718750
*
719751
* @param hostname the hostname that's being looked up
720752
* @return true if the hostname should point to the loopback adress. False otherwise.
@@ -729,10 +761,15 @@ private static boolean isLocalHostAddress(String hostname) {
729761
return true;
730762
}
731763

764+
if (!resolveLocalhostWithoutDns) {
765+
return PlatformDependent.isWindows() && LOCALHOST.equalsIgnoreCase(hostname);
766+
}
767+
732768
if (hostname.endsWith(".")) {
733769
hostname = hostname.substring(0, hostname.length() - 1);
734770
}
735-
return hostname.equalsIgnoreCase(LOCALHOST) || hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST);
771+
return hostname.equalsIgnoreCase(LOCALHOST)
772+
|| hostname.toLowerCase(Locale.US).endsWith(DOT_LOCALHOST);
736773
}
737774

738775
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)
@@ -2173,7 +2179,7 @@ private static void assertQueryObserver(DnsNameResolver resolver, DnsRecordType
21732179
}
21742180
}
21752181

2176-
private static final class TestRecursiveCacheDnsQueryLifecycleObserverFactory
2182+
static final class TestRecursiveCacheDnsQueryLifecycleObserverFactory
21772183
implements DnsQueryLifecycleObserverFactory {
21782184
final Queue<TestDnsQueryLifecycleObserver> observers =
21792185
new ConcurrentLinkedQueue<TestDnsQueryLifecycleObserver>();
Collapse file

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

Copy file name to clipboard
+159Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.MultiThreadIoEventLoopGroup;
20+
import io.netty.channel.nio.NioIoHandler;
21+
import io.netty.resolver.HostsFileEntriesResolver;
22+
import io.netty.resolver.ResolvedAddressTypes;
23+
import io.netty.resolver.dns.DnsNameResolverTest.TestRecursiveCacheDnsQueryLifecycleObserverFactory;
24+
import io.netty.util.internal.PlatformDependent;
25+
import org.apache.directory.server.dns.messages.QuestionRecord;
26+
import org.apache.directory.server.dns.messages.RecordType;
27+
import org.apache.directory.server.dns.messages.ResourceRecord;
28+
import org.apache.directory.server.dns.store.RecordStore;
29+
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.parallel.Isolated;
32+
import org.junit.jupiter.params.ParameterizedTest;
33+
import org.junit.jupiter.params.provider.EnumSource;
34+
35+
import java.net.InetAddress;
36+
import java.util.Collections;
37+
import java.util.Set;
38+
39+
import static io.netty.resolver.dns.DnsNameResolverTest.DOMAINS_ALL;
40+
import static io.netty.resolver.dns.DnsNameResolverTest.newResolver;
41+
import static io.netty.resolver.dns.TestDnsServer.newARecord;
42+
import static org.assertj.core.api.Assumptions.assumeThat;
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertFalse;
45+
46+
@Isolated
47+
public class IsolatedDnsNameResolverTest {
48+
private static final TestDnsServer dnsServer = new TestDnsServer(DOMAINS_ALL);
49+
private static final EventLoopGroup group = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
50+
private static boolean existingLocalhostSetting;
51+
52+
@BeforeAll
53+
public static void captureSetting() throws Exception {
54+
existingLocalhostSetting = DnsNameResolver.resolveLocalhostWithoutDns;
55+
dnsServer.start();
56+
}
57+
58+
@AfterAll
59+
public static void restoreSetting() {
60+
try {
61+
dnsServer.stop();
62+
group.shutdownGracefully();
63+
} finally {
64+
DnsNameResolver.resolveLocalhostWithoutDns = existingLocalhostSetting;
65+
}
66+
}
67+
68+
@ParameterizedTest
69+
@EnumSource(DnsNameResolverChannelStrategy.class)
70+
public void testResolveDotLocalhostViaDnsWhenDisabled(DnsNameResolverChannelStrategy strategy) throws Exception {
71+
final String hostname = "myservice.localhost";
72+
final String expectedIp = "10.0.0.1";
73+
final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() {
74+
@Override
75+
public Set<ResourceRecord> getRecords(QuestionRecord question) {
76+
if (question.getRecordType() == RecordType.A) {
77+
String domainName = question.getDomainName();
78+
if (domainName.equalsIgnoreCase(hostname) || domainName.equalsIgnoreCase(hostname + '.')) {
79+
return Collections.singleton(newARecord(hostname, expectedIp));
80+
}
81+
}
82+
return Collections.emptySet();
83+
}
84+
});
85+
customDnsServer.start();
86+
DnsNameResolver.resolveLocalhostWithoutDns = false;
87+
try {
88+
DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next())
89+
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
90+
.hostsFileEntriesResolver(new HostsFileEntriesResolver() {
91+
@Override
92+
public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
93+
return null;
94+
}
95+
})
96+
.build();
97+
try {
98+
InetAddress address = resolver.resolve(hostname).syncUninterruptibly().getNow();
99+
assertEquals(expectedIp, address.getHostAddress());
100+
101+
TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
102+
(TestRecursiveCacheDnsQueryLifecycleObserverFactory)
103+
resolver.dnsQueryLifecycleObserverFactory();
104+
assertFalse(lifecycleObserverFactory.observers.isEmpty());
105+
} finally {
106+
resolver.close();
107+
}
108+
} finally {
109+
customDnsServer.stop();
110+
}
111+
}
112+
113+
@ParameterizedTest
114+
@EnumSource(DnsNameResolverChannelStrategy.class)
115+
public void testResolveLocalhostViaDnsWhenDisabledOnNonWindows(DnsNameResolverChannelStrategy strategy)
116+
throws Exception {
117+
assumeThat(PlatformDependent.isWindows()).isFalse();
118+
119+
final String expectedIp = "10.0.0.2";
120+
final TestDnsServer customDnsServer = new TestDnsServer(new RecordStore() {
121+
@Override
122+
public Set<ResourceRecord> getRecords(QuestionRecord question) {
123+
if (question.getRecordType() == RecordType.A) {
124+
String domainName = question.getDomainName();
125+
if (domainName.equalsIgnoreCase("localhost") || domainName.equalsIgnoreCase("localhost.")) {
126+
return Collections.singleton(newARecord("localhost", expectedIp));
127+
}
128+
}
129+
return Collections.emptySet();
130+
}
131+
});
132+
customDnsServer.start();
133+
DnsNameResolver.resolveLocalhostWithoutDns = false;
134+
try {
135+
DnsNameResolver resolver = newResolver(strategy, false, null, customDnsServer, group.next())
136+
.resolvedAddressTypes(ResolvedAddressTypes.IPV4_ONLY)
137+
.hostsFileEntriesResolver(new HostsFileEntriesResolver() {
138+
@Override
139+
public InetAddress address(String inetHost, ResolvedAddressTypes resolvedAddressTypes) {
140+
return null;
141+
}
142+
})
143+
.build();
144+
try {
145+
InetAddress address = resolver.resolve("localhost").syncUninterruptibly().getNow();
146+
assertEquals(expectedIp, address.getHostAddress());
147+
148+
TestRecursiveCacheDnsQueryLifecycleObserverFactory lifecycleObserverFactory =
149+
(TestRecursiveCacheDnsQueryLifecycleObserverFactory)
150+
resolver.dnsQueryLifecycleObserverFactory();
151+
assertFalse(lifecycleObserverFactory.observers.isEmpty());
152+
} finally {
153+
resolver.close();
154+
}
155+
} finally {
156+
customDnsServer.stop();
157+
}
158+
}
159+
}

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.