Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/Containerization/DNSConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
//===----------------------------------------------------------------------===//

import ContainerizationExtras

/// DNS configuration for a container. The values will be used to
/// construct /etc/resolv.conf for a given container.
public struct DNS: Sendable {
Expand Down Expand Up @@ -41,6 +43,26 @@ public struct DNS: Sendable {
self.searchDomains = searchDomains
self.options = options
}

/// Validates the DNS configuration.
///
/// Ensures that all nameserver entries are valid IPv4 or IPv6 addresses.
/// Arbitrary hostnames are not permitted as nameservers.
///
/// - Throws: ``ContainerizationError`` with code `.invalidArgument` if
/// any nameserver is not a valid IP address.
public func validate() throws {
for nameserver in nameservers {
let isValidIPv4 = (try? IPv4Address(nameserver)) != nil
let isValidIPv6 = (try? IPv6Address(nameserver)) != nil
if !isValidIPv4 && !isValidIPv6 {
throw ContainerizationError(
.invalidArgument,
message: "nameserver '\(nameserver)' is not a valid IPv4 or IPv6 address"
)
}
}
}
}

extension DNS {
Expand Down
1 change: 1 addition & 0 deletions Sources/Containerization/Vminitd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ extension Vminitd {

/// Configure DNS within the sandbox's environment.
public func configureDNS(config: DNS, location: String) async throws {
try config.validate()
_ = try await client.configureDns(
.with {
$0.location = location
Expand Down
30 changes: 30 additions & 0 deletions Tests/ContainerizationTests/DNSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,34 @@ struct DNSTests {
let expected = "nameserver 8.8.8.8\n"
#expect(dns.resolvConf == expected)
}

@Test func dnsValidateAcceptsValidIPv4Nameservers() throws {
let dns = DNS(nameservers: ["8.8.8.8", "1.1.1.1"])
#expect(throws: Never.self) { try dns.validate() }
}

@Test func dnsValidateAcceptsValidIPv6Nameservers() throws {
let dns = DNS(nameservers: ["2001:4860:4860::8888", "::1"])
#expect(throws: Never.self) { try dns.validate() }
}

@Test func dnsValidateAcceptsMixedIPv4AndIPv6Nameservers() throws {
let dns = DNS(nameservers: ["8.8.8.8", "2001:4860:4860::8844"])
#expect(throws: Never.self) { try dns.validate() }
}

@Test func dnsValidateAcceptsEmptyNameservers() throws {
let dns = DNS(nameservers: [])
#expect(throws: Never.self) { try dns.validate() }
}

@Test func dnsValidateRejectsHostname() {
let dns = DNS(nameservers: ["dns.example.com"])
#expect(throws: (any Error).self) { try dns.validate() }
}

@Test func dnsValidateRejectsInvalidAddress() {
let dns = DNS(nameservers: ["not-an-ip"])
#expect(throws: (any Error).self) { try dns.validate() }
}
}