From 07a0cb9215e06ea93c408ad98d873de48335099b Mon Sep 17 00:00:00 2001 From: "He-Pin(kerr)" Date: Fri, 27 Nov 2026 14:07:21 +0800 Subject: [PATCH] fix: normalize DNS search domains Motivation: System resolver configuration can use the root marker to indicate that no search suffix is configured, and some inputs prefix valid suffixes with a dot. Applying these strings verbatim can prevent the intended lookup. Repeated equivalent entries can repeat the same lookup. Modification: Normalize system and explicitly configured DNS search domains once in the resolver settings. Ignore the exact root marker, remove one leading dot from other entries and retain the first occurrence of each normalized string. Preserve empty suffixes, trailing dots and ndots. Document the behavior and add deterministic configuration tests. Result: An optional leading dot does not prevent a configured suffix from being used, and duplicate suffixes no longer expand the same candidate. Existing search order for distinct suffixes is unchanged. Tests: - JDK 17 focused ScalaTest compiler overlays (Scala 2.13.18 and 3.3.9): the three normalization assertions fail on the previous PR head and pass after the fix. Two root-marker cases and nine unchanged resolver configuration parser cases also pass on each axis. - Native scalafmt 3.11.5, scoped class-signature comparison and git diff --check passed. - Paradox 0.11.1 standalone parser accepted the changed DNS page. - Full sbt docs/paradox, validation and MiMa not run in the partial offline checkout. No DNS network tests were run. References: Refs netty/netty#18545; independently implemented in Pekko. --- .../apache/pekko/io/dns/DnsSettingsSpec.scala | 45 +++++++++++++++++++ .../org/apache/pekko/io/dns/DnsSettings.scala | 29 +++++++----- docs/src/main/paradox/io-dns.md | 7 +++ 3 files changed, 69 insertions(+), 12 deletions(-) diff --git a/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala b/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala index c658106a23f..199d67edf5b 100644 --- a/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala +++ b/actor-tests/src/test/scala/org/apache/pekko/io/dns/DnsSettingsSpec.scala @@ -19,6 +19,7 @@ import scala.concurrent.duration._ import org.apache.pekko import pekko.actor.ExtendedActorSystem +import pekko.io.dns.internal.ResolvConfParser import pekko.testkit.PekkoSpec import com.typesafe.config.ConfigFactory @@ -97,6 +98,50 @@ class DnsSettingsSpec extends PekkoSpec { dnsSettings.SearchDomains shouldEqual List("example.com", "example.net") } + "ignore a root search domain from the host configuration" in { + val hostConfig = ResolvConfParser.parseLines(Iterator("search example.com", "search .")) + val config = ConfigFactory.parseString("search-domains = \"default\"") + + DnsSettings.configuredSearchDomains(config, hostConfig.search) shouldEqual Nil + } + + "ignore a single configured root search domain without changing ndots" in { + val dnsSettings = new DnsSettings( + eas, + ConfigFactory.parseString(""" + nameservers = "127.0.0.1" + search-domains = "." + ndots = 5 + """).withFallback(defaultConfig)) + + dnsSettings.SearchDomains shouldEqual Nil + dnsSettings.NDots shouldEqual 5 + } + + "normalize search suffixes from the host configuration after parsing" in { + val hostConfig = ResolvConfParser.parseLines(Iterator("search .example.net example.org example.net")) + val config = ConfigFactory.parseString("search-domains = \"default\"") + + DnsSettings.configuredSearchDomains(config, hostConfig.search) shouldEqual List("example.net", "example.org") + } + + "normalize search suffixes in a single configured domain without removing a trailing dot" in { + val config = ConfigFactory.parseString("search-domains = \".example.net.\"") + + DnsSettings.configuredSearchDomains(config, Nil) shouldEqual List("example.net.") + } + + "normalize search suffixes without changing first-occurrence order" in { + val dnsSettings = new DnsSettings( + eas, + ConfigFactory.parseString(""" + nameservers = "127.0.0.1" + search-domains = [ ".", ".example.net", "", "example.org.", "example.net", "", "." ] + """).withFallback(defaultConfig)) + + dnsSettings.SearchDomains shouldEqual List("example.net", "", "example.org.") + } + "use host ndots if set to default" in { val dnsSettings = new DnsSettings( eas, diff --git a/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala b/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala index fde89e6c816..85a82169ed2 100644 --- a/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala +++ b/actor/src/main/scala/org/apache/pekko/io/dns/DnsSettings.scala @@ -125,18 +125,7 @@ private[dns] final class DnsSettings(system: ExtendedActorSystem, c: Config) { } else None } - val SearchDomains: List[String] = { - c.getValue("search-domains").valueType() match { - case ConfigValueType.STRING => - c.getString("search-domains") match { - case "default" => resolvConf.map(_.search).getOrElse(Nil) - case single => List(single) - } - case ConfigValueType.LIST => - c.getStringList("search-domains").asScala.toList - case _ => throw new IllegalArgumentException("Invalid type for search-domains. Must be a string or string list.") - } - } + val SearchDomains: List[String] = DnsSettings.configuredSearchDomains(c, resolvConf.map(_.search).getOrElse(Nil)) val NDots: Int = { c.getValue("ndots").valueType() match { @@ -169,6 +158,22 @@ private[dns] final class DnsSettings(system: ExtendedActorSystem, c: Config) { object DnsSettings { + private[dns] def configuredSearchDomains(config: Config, hostSearchDomains: => List[String]): List[String] = { + val configured = config.getValue("search-domains").valueType() match { + case ConfigValueType.STRING => + config.getString("search-domains") match { + case "default" => hostSearchDomains + case single => List(single) + } + case ConfigValueType.LIST => + config.getStringList("search-domains").asScala.toList + case _ => throw new IllegalArgumentException("Invalid type for search-domains. Must be a string or string list.") + } + // Some resolv.conf providers use the root to indicate that there is no search suffix. + // Keep the first occurrence and preserve explicit empty suffixes and trailing dots. + configured.iterator.filterNot(_ == ".").map(_.stripPrefix(".")).distinct.toList + } + private final val DnsFallbackPort = 53 private val inetSocketAddress = """(.*?)(?::(\d+))?""".r private val publicLookup = MethodHandles.publicLookup() diff --git a/docs/src/main/paradox/io-dns.md b/docs/src/main/paradox/io-dns.md index 11f39cd10b2..0bc6f2a2cdb 100644 --- a/docs/src/main/paradox/io-dns.md +++ b/docs/src/main/paradox/io-dns.md @@ -54,6 +54,13 @@ There are currently two implementations: `inet-address` is the default implementation as it pre-dates `async-dns`, `async-dns` may become the default in a future release. +For both `/etc/resolv.conf` and `pekko.io.dns.async-dns.search-domains`, `async-dns` ignores the exact +root marker `.`. If `.` is the only entry, the resolver does not append any search suffixes. A single +leading dot is removed from other search suffixes: `.example.org` is treated as `example.org`. +Identical entries after normalization are used once, in first-occurrence order. An explicitly +empty entry still adds no suffix at its position in that order. Trailing dots and `ndots` +are unchanged. + DNS lookups can be done via the `DNS` extension: Scala