Commit 6ab3cb748c

6ab3cb748ce8f03678c20e98404c5e9131707f3d

parent: 7e33c16600

Unsigned

cmc <hello@cleberg.net> · 2026-10-24 03:57 UTC

Validate the domain at the two input boundaries

`run()` reports the input error instead of starting a 14-section lookup
that can only return section errors, and bulk input drops tokens that are
not domains, now splitting on whitespace as well as commas and newlines.

`normalizedDomain` is left alone: it is the key that matches stored rows
in `trackDomain`, `openInspection` and `workflowsContaining`.

Layout: unified · split

DomainDig/DomainViewModel.swift +13 −5
@@ -175,6 +175,7 @@ private struct PortfolioDashboardStamp: Equatable {
175final class DomainViewModel { 175final class DomainViewModel {
176 var domain: String = "" 176 var domain: String = ""
177 var bulkInput: String = "" 177 var bulkInput: String = ""
178 var domainInputError: String?
178 179
179 var dnsSections: [DNSSection] = [] 180 var dnsSections: [DNSSection] = []
180 var dnsLoading = false 181 var dnsLoading = false
@@ -1138,8 +1139,16 @@ final class DomainViewModel {
1138 } 1139 }
1139 1140
1140 func run() { 1141 func run() {
1141 let target = trimmedDomain 1142 let target: String
1142 guard !target.isEmpty else { return } 1143 switch DomainName.validate(trimmedDomain) {
1144 case .success(let host):
1145 target = host
1146 domainInputError = nil
1147 case .failure(let error):
1148 domainInputError = error.localizedDescription
1149 return
1150 }
1151
1143 clearBatchState() 1152 clearBatchState()
1144 let lookupID = beginLookup(for: target) 1153 let lookupID = beginLookup(for: target)
1145 1154
@@ -2166,13 +2175,12 @@ final class DomainViewModel {
2166 } 2175 }
2167 2176
2168 private func parsedDomains(from input: String) -> [String] { 2177 private func parsedDomains(from input: String) -> [String] {
2169 let separators = CharacterSet(charactersIn: ",\n") 2178 let separators = CharacterSet(charactersIn: ",").union(.whitespacesAndNewlines)
2170 var seen = Set<String>() 2179 var seen = Set<String>()
2171 2180
2172 return input 2181 return input
2173 .components(separatedBy: separators) 2182 .components(separatedBy: separators)
2174 .map(normalizedDomain) 2183 .compactMap { try? DomainName.validate($0).get() }
2175 .filter { !$0.isEmpty }
2176 .filter { seen.insert($0).inserted } 2184 .filter { seen.insert($0).inserted }
2177 } 2185 }
2178 2186
DomainDigTests/DomainNameTests.swift +32
@@ -96,3 +96,35 @@ final class DomainNameTests: XCTestCase {
96 } 96 }
97 } 97 }
98} 98}
99
100/// The validator sits at the input boundary only. `normalizedDomain` is the key
101/// that matches already-stored rows, so it stays lenient: routing it through the
102/// validator would turn tracking and workflow lookups into no-ops for any stored
103/// domain the rules reject.
104@MainActor
105final class DomainViewModelInputTests: XCTestCase {
106 /// Shared and never released: deallocating a `DomainViewModel` runs its
107 /// implicit main-actor-isolated deinit through the iOS 17 back-deployment
108 /// shim, which aborts in the simulator. The app holds one for its whole
109 /// lifetime, so nothing releases one there either.
110 private static let viewModel = DomainViewModel()
111
112 func testAnInvalidDomainDoesNotStartALookup() {
113 let viewModel = Self.viewModel
114 viewModel.domain = "localhost"
115
116 viewModel.run()
117
118 XCTAssertFalse(viewModel.hasRun)
119 XCTAssertNotNil(viewModel.domainInputError)
120 }
121
122 func testAStoredSingleLabelDomainStillTracksAndMatchesWorkflows() {
123 let viewModel = Self.viewModel
124 viewModel.trackedDomains = []
125 viewModel.workflows = [DomainWorkflow(name: "Internal", domains: ["localhost"])]
126
127 XCTAssertTrue(viewModel.trackDomain(domain: "localhost", availabilityStatus: nil))
128 XCTAssertEqual(viewModel.workflowsContaining(domain: "localhost").count, 1)
129 }
130}