Commit 68c810fe24

68c810fe249aa10e8f4ad50be4e81f6b66cdcbde

parent: e38ea79776

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 02:16 UTC

Stop force-unwrapping request URLs built from the inspected domain

A domain carrying a space trapped the app: URL(string: "https://exa
mple.com") is nil, and SSLCheckService, HTTPHeadersService and
RedirectChainService each built their request with !. Nothing validates
the input — trimmedDomain only trims the ends — so typing "example .com"
or space-separating bulk input (it splits on commas and newlines) was
enough to crash on Run.

DomainHostURL returns nil for a host that cannot form a URL and the
services report it as a section error. IPGeolocationService had the same
unwrap over an IP from DNS.

Layout: unified · split

DomainDig/DomainHostURL.swift added +30
@@ -0,0 +1,30 @@
1import Foundation
2
3/// Request URLs for an inspected host.
4///
5/// The inspected domain is free text, so it cannot be forced into a URL:
6/// `URL(string: "https://exa mple.com")` is nil, and a section service that
7/// built its request with `!` trapped on any host carrying a space, a tab or a
8/// control character — reachable by typing `example .com`, or by separating
9/// bulk input with spaces instead of commas or newlines.
10enum DomainHostURL {
11 static func https(_ host: String) -> URL? {
12 url(scheme: "https", host: host)
13 }
14
15 static func http(_ host: String) -> URL? {
16 url(scheme: "http", host: host)
17 }
18
19 /// `nil` when `host` cannot form a URL with a host component.
20 static func url(scheme: String, host: String) -> URL? {
21 let trimmedHost = host.trimmingCharacters(in: .whitespacesAndNewlines)
22 guard !trimmedHost.isEmpty,
23 trimmedHost.rangeOfCharacter(from: .whitespacesAndNewlines) == nil,
24 let url = URL(string: "\(scheme)://\(trimmedHost)"),
25 url.host?.isEmpty == false else {
26 return nil
27 }
28 return url
29 }
30}
DomainDig/HTTPHeadersService.swift +4 −1
@@ -36,7 +36,10 @@ struct HTTPHeadersResult {
3636
3737struct HTTPHeadersService {
3838 static func fetch(domain: String) async -> ServiceResult<HTTPHeadersResult> {
39 let url = URL(string: "https://\(domain)")!
39 guard let url = DomainHostURL.https(domain) else {
40 return .error("Not a valid domain name")
41 }
42
4043 var request = URLRequest(url: url, timeoutInterval: 10)
4144 request.httpMethod = "HEAD"
4245 let metricsDelegate = TaskMetricsDelegate()
DomainDig/IPGeolocationService.swift +4 −1
@@ -2,7 +2,10 @@ import Foundation
22
33struct IPGeolocationService {
44 static func lookup(ip: String) async -> ServiceResult<IPGeolocation> {
5 let url = URL(string: "https://ipapi.co/\(ip)/json/")!
5 guard let url = URL(string: "https://ipapi.co/\(ip)/json/") else {
6 return .error("Not a valid IP address")
7 }
8
69 let request = URLRequest(url: url, timeoutInterval: 10)
710
811 do {
DomainDig/RedirectChainService.swift +10 −2
@@ -2,12 +2,20 @@ import Foundation
22
33struct RedirectChainService {
44 static func trace(domain: String) async -> ServiceResult<[RedirectHop]> {
5 guard let httpsURL = DomainHostURL.https(domain) else {
6 return .error("Not a valid domain name")
7 }
8
59 do {
6 let hops = try await followChain(startingURL: URL(string: "https://\(domain)")!)
10 let hops = try await followChain(startingURL: httpsURL)
711 return hops.isEmpty ? .empty("No redirect data available") : .success(hops)
812 } catch {
13 guard let httpURL = DomainHostURL.http(domain) else {
14 return .error(error.localizedDescription)
15 }
16
917 do {
10 let hops = try await followChain(startingURL: URL(string: "http://\(domain)")!)
18 let hops = try await followChain(startingURL: httpURL)
1119 return hops.isEmpty ? .empty("No redirect data available") : .success(hops)
1220 } catch {
1321 return .error(error.localizedDescription)
DomainDig/SSLCheckService.swift +4 −1
@@ -4,6 +4,10 @@ import Security
44struct SSLCheckService {
55
66 static func check(domain: String) async -> ServiceResult<SSLCertificateInfo> {
7 guard let url = DomainHostURL.https(domain) else {
8 return .error("Not a valid domain name")
9 }
10
711 let delegate = SSLSessionDelegate()
812 let session = URLSession(
913 configuration: .ephemeral,
@@ -12,7 +16,6 @@ struct SSLCheckService {
1216 )
1317 defer { session.invalidateAndCancel() }
1418
15 let url = URL(string: "https://\(domain)")!
1619 let request = URLRequest(url: url, timeoutInterval: 10)
1720
1821 do {
DomainDigTests/InspectionInputTests.swift +43
@@ -1,6 +1,49 @@
11import XCTest
22@testable import DomainDig
33
4/// The inspected domain is free text, and the section services used to force it
5/// into a URL with `!`. These pin the two pure helpers that replaced that.
6final class DomainHostURLTests: XCTestCase {
7
8 func testBuildsHTTPSURLForAPlainHost() {
9 XCTAssertEqual(DomainHostURL.https("example.com")?.absoluteString, "https://example.com")
10 }
11
12 func testBuildsHTTPURLForAPlainHost() {
13 XCTAssertEqual(DomainHostURL.http("example.com")?.absoluteString, "http://example.com")
14 }
15
16 func testTrimsSurroundingWhitespace() {
17 XCTAssertEqual(DomainHostURL.https(" example.com\n")?.absoluteString, "https://example.com")
18 }
19
20 /// The crash this replaced: `URL(string: "https://exa mple.com")` is nil, so
21 /// the force-unwrap trapped. Reachable by typing `example .com`, or by
22 /// separating bulk input with spaces instead of commas or newlines.
23 func testRejectsAnInteriorSpace() {
24 XCTAssertNil(DomainHostURL.https("exa mple.com"))
25 }
26
27 func testRejectsAnInteriorTab() {
28 XCTAssertNil(DomainHostURL.https("exa\tmple.com"))
29 }
30
31 func testRejectsAControlCharacter() {
32 XCTAssertNil(DomainHostURL.https("exa\u{0}mple.com"))
33 }
34
35 func testRejectsInputWithNoHostComponent() {
36 XCTAssertNil(DomainHostURL.https(""))
37 XCTAssertNil(DomainHostURL.https(" "))
38 XCTAssertNil(DomainHostURL.https("/"))
39 XCTAssertNil(DomainHostURL.https(":443"))
40 }
41
42 func testEncodesAnInternationalizedHost() {
43 XCTAssertEqual(DomainHostURL.https("münchen.de")?.host, "xn--mnchen-3ya.de")
44 }
45}
46
447/// `rdap.org` answers 404 both for a domain its registry does not hold and for a
548/// TLD it knows no RDAP server for. Only the first means "available", so the
649/// availability check turns on which server answered.