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 @@ |
| 1 | import 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. |
| 10 | enum 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 { |
| 36 | 36 | |
| 37 | 37 | struct HTTPHeadersService { |
| 38 | 38 | 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 | |
| 40 | 43 | var request = URLRequest(url: url, timeoutInterval: 10) |
| 41 | 44 | request.httpMethod = "HEAD" |
| 42 | 45 | let metricsDelegate = TaskMetricsDelegate() |
DomainDig/IPGeolocationService.swift
+4 −1
| @@ -2,7 +2,10 @@ import Foundation |
| 2 | 2 | |
| 3 | 3 | struct IPGeolocationService { |
| 4 | 4 | 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 | |
| 6 | 9 | let request = URLRequest(url: url, timeoutInterval: 10) |
| 7 | 10 | |
| 8 | 11 | do { |
DomainDig/RedirectChainService.swift
+10 −2
| @@ -2,12 +2,20 @@ import Foundation |
| 2 | 2 | |
| 3 | 3 | struct RedirectChainService { |
| 4 | 4 | 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 | |
| 5 | 9 | do { |
| 6 | | let hops = try await followChain(startingURL: URL(string: "https://\(domain)")!) |
| 10 | let hops = try await followChain(startingURL: httpsURL) |
| 7 | 11 | return hops.isEmpty ? .empty("No redirect data available") : .success(hops) |
| 8 | 12 | } catch { |
| 13 | guard let httpURL = DomainHostURL.http(domain) else { |
| 14 | return .error(error.localizedDescription) |
| 15 | } |
| 16 | |
| 9 | 17 | do { |
| 10 | | let hops = try await followChain(startingURL: URL(string: "http://\(domain)")!) |
| 18 | let hops = try await followChain(startingURL: httpURL) |
| 11 | 19 | return hops.isEmpty ? .empty("No redirect data available") : .success(hops) |
| 12 | 20 | } catch { |
| 13 | 21 | return .error(error.localizedDescription) |
DomainDig/SSLCheckService.swift
+4 −1
| @@ -4,6 +4,10 @@ import Security |
| 4 | 4 | struct SSLCheckService { |
| 5 | 5 | |
| 6 | 6 | 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 | |
| 7 | 11 | let delegate = SSLSessionDelegate() |
| 8 | 12 | let session = URLSession( |
| 9 | 13 | configuration: .ephemeral, |
| @@ -12,7 +16,6 @@ struct SSLCheckService { |
| 12 | 16 | ) |
| 13 | 17 | defer { session.invalidateAndCancel() } |
| 14 | 18 | |
| 15 | | let url = URL(string: "https://\(domain)")! |
| 16 | 19 | let request = URLRequest(url: url, timeoutInterval: 10) |
| 17 | 20 | |
| 18 | 21 | do { |
DomainDigTests/InspectionInputTests.swift
+43
| @@ -1,6 +1,49 @@ |
| 1 | 1 | import XCTest |
| 2 | 2 | @testable import DomainDig |
| 3 | 3 | |
| 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. |
| 6 | final 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 | |
| 4 | 47 | /// `rdap.org` answers 404 both for a domain its registry does not hold and for a |
| 5 | 48 | /// TLD it knows no RDAP server for. Only the first means "available", so the |
| 6 | 49 | /// availability check turns on which server answered. |