Commit 4b2e115e80

4b2e115e801a92c68b88d1dd4dc86c22ff033a35

parent: 4fc2823e89

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 06:07 UTC

Refuse line breaks in the notification mail

The message is assembled by joining lines with CRLF and sending them raw, so
values that reach it decide the wire format, and the inspected domain reaches
the subject while the event details reach the body with arbitrary keys.

Two protections, both pure and internal so they can be tested where SMTPClient
cannot: validatedHeaderValue refuses a value carrying a line break, and send
throws before building the message if the sender, the recipient list or the
subject fails. dotStuffed implements RFC 5321 section 4.5.2 over the whole
body, which closes the worse of the two vectors — a line holding nothing but a
dot ends the DATA command, after which the remainder is read as SMTP commands.

The deep link refuses a value with a control character in it outright: it is
the one path into these values from outside the app, and a tainted domain
travels on into stored rows. Import strips instead, because refusing rows at
restore would cost a user their watchlist over a hand-edited backup.

Layout: unified · split

DomainDataPortabilityService.swift +2
@@ -522,6 +522,7 @@ enum DataValidationService {
522 private nonisolated static func normalizeDomain(_ value: String) -> String { 522 private nonisolated static func normalizeDomain(_ value: String) -> String {
523 value 523 value
524 .trimmingCharacters(in: .whitespacesAndNewlines) 524 .trimmingCharacters(in: .whitespacesAndNewlines)
525 .components(separatedBy: .controlCharacters).joined()
525 .replacingOccurrences(of: "https://", with: "") 526 .replacingOccurrences(of: "https://", with: "")
526 .replacingOccurrences(of: "http://", with: "") 527 .replacingOccurrences(of: "http://", with: "")
527 .components(separatedBy: "/") 528 .components(separatedBy: "/")
@@ -1332,6 +1333,7 @@ enum DomainDataPortabilityService {
1332 private nonisolated static func normalizeDomain(_ value: String) -> String { 1333 private nonisolated static func normalizeDomain(_ value: String) -> String {
1333 value 1334 value
1334 .trimmingCharacters(in: .whitespacesAndNewlines) 1335 .trimmingCharacters(in: .whitespacesAndNewlines)
1336 .components(separatedBy: .controlCharacters).joined()
1335 .replacingOccurrences(of: "https://", with: "") 1337 .replacingOccurrences(of: "https://", with: "")
1336 .replacingOccurrences(of: "http://", with: "") 1338 .replacingOccurrences(of: "http://", with: "")
1337 .components(separatedBy: "/") 1339 .components(separatedBy: "/")
DomainDig.xcodeproj/project.pbxproj +4
@@ -28,6 +28,7 @@
28 E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; }; 28 E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; };
29 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; }; 29 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; };
30 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000601 /* FeatureAccessTests.swift */; }; 30 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000601 /* FeatureAccessTests.swift */; };
31 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; };
31 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; 32 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; };
32 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; }; 33 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; };
33 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; }; 34 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; };
@@ -103,6 +104,7 @@
103 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; }; 104 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; };
104 A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; }; 105 A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; };
105 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = FeatureAccessTests.swift; sourceTree = "<group>"; }; 106 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = FeatureAccessTests.swift; sourceTree = "<group>"; };
107 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = SMTPMessageTests.swift; sourceTree = "<group>"; };
106 A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; 108 A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; };
107 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; }; 109 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; };
108 A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; }; 110 A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; };
@@ -245,6 +247,7 @@
245 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */, 247 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */,
246 A1B2C3D40000000000000201 /* InspectionInputTests.swift */, 248 A1B2C3D40000000000000201 /* InspectionInputTests.swift */,
247 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */, 249 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */,
250 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */,
248 A1B2C3D40000000000000301 /* AvailabilityTests.swift */, 251 A1B2C3D40000000000000301 /* AvailabilityTests.swift */,
249 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */, 252 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */,
250 A1B2C3D40000000000000501 /* DomainNameTests.swift */, 253 A1B2C3D40000000000000501 /* DomainNameTests.swift */,
@@ -510,6 +513,7 @@
510 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */, 513 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */,
511 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */, 514 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */,
512 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */, 515 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */,
516 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */,
513 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, 517 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */,
514 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */, 518 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */,
515 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */, 519 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */,
DomainDig/IntegrationService.swift +32 −3
@@ -666,6 +666,27 @@ private enum IntegrationSecretStore {
666 } 666 }
667} 667}
668 668
669/// Message construction rules the SMTP wire format imposes. Internal so the
670/// tests can reach them; `SMTPClient` itself is file-private.
671enum SMTPMessage {
672 /// `nil` when a value cannot appear in a header. A bare CR or LF ends the
673 /// header line, and whatever follows is read as further headers — so a
674 /// domain or address carrying one could add a Bcc the user never wrote.
675 static func validatedHeaderValue(_ value: String) -> String? {
676 value.rangeOfCharacter(from: .newlines) == nil ? value : nil
677 }
678
679 /// RFC 5321 section 4.5.2. A body line of "." alone ends the DATA command,
680 /// after which the rest of the message is read as SMTP commands, so every
681 /// line beginning with "." is sent doubled.
682 static func dotStuffed(_ body: String) -> String {
683 body
684 .components(separatedBy: "\r\n")
685 .map { $0.hasPrefix(".") ? "." + $0 : $0 }
686 .joined(separator: "\r\n")
687 }
688}
689
669private enum SMTPClient { 690private enum SMTPClient {
670 static func send( 691 static func send(
671 event: MonitoringEvent, 692 event: MonitoringEvent,
@@ -717,10 +738,18 @@ private enum SMTPClient {
717 .sorted { $0.key < $1.key } 738 .sorted { $0.key < $1.key }
718 .map { "\($0.key): \($0.value)" } 739 .map { "\($0.key): \($0.value)" }
719 .joined(separator: "\r\n") 740 .joined(separator: "\r\n")
741 let recipientList = configuration.recipientAddresses.joined(separator: ", ")
742 let subject = "[DomainDig] \(event.severity.title) \(event.domain) \(event.type.title)"
743 guard SMTPMessage.validatedHeaderValue(configuration.senderAddress) != nil,
744 SMTPMessage.validatedHeaderValue(recipientList) != nil,
745 SMTPMessage.validatedHeaderValue(subject) != nil else {
746 throw IntegrationError.smtp("The message could not be built: an address or the domain contains a line break.")
747 }
748
720 let body = [ 749 let body = [
721 "From: DomainDig <\(configuration.senderAddress)>", 750 "From: DomainDig <\(configuration.senderAddress)>",
722 "To: \(configuration.recipientAddresses.joined(separator: ", "))", 751 "To: \(recipientList)",
723 "Subject: [DomainDig] \(event.severity.title) \(event.domain) \(event.type.title)", 752 "Subject: \(subject)",
724 "Date: \(DateFormatter.rfc2822.string(from: Date()))", 753 "Date: \(DateFormatter.rfc2822.string(from: Date()))",
725 "", 754 "",
726 event.summary, 755 event.summary,
@@ -733,7 +762,7 @@ private enum SMTPClient {
733 ] 762 ]
734 .joined(separator: "\r\n") 763 .joined(separator: "\r\n")
735 764
736 try await channel.sendRaw(body + "\r\n.\r\n") 765 try await channel.sendRaw(SMTPMessage.dotStuffed(body) + "\r\n.\r\n")
737 _ = try await channel.readResponse(expecting: [250]) 766 _ = try await channel.readResponse(expecting: [250])
738 _ = try await channel.sendCommand("QUIT", expecting: [221]) 767 _ = try await channel.sendCommand("QUIT", expecting: [221])
739 await channel.cancel() 768 await channel.cancel()
DomainDigTests/SMTPMessageTests.swift added +85
@@ -0,0 +1,85 @@
1import XCTest
2@testable import DomainDig
3
4/// The notification mail is assembled by joining lines with CRLF and sending
5/// them raw, so values that reach it decide the wire format. Two things can go
6/// wrong: a bare line break ends a header and lets the rest be read as more
7/// headers, and a line of "." alone ends the DATA command and lets the rest be
8/// read as SMTP commands. Both values are reachable — the inspected domain
9/// arrives in the subject, and the event details carry arbitrary keys.
10final class SMTPMessageTests: XCTestCase {
11
12 // MARK: Header values
13
14 func testAPlainValueIsAllowed() {
15 XCTAssertEqual(SMTPMessage.validatedHeaderValue("example.com"), "example.com")
16 }
17
18 func testALineFeedIsRefused() {
19 XCTAssertNil(SMTPMessage.validatedHeaderValue("example.com\nBcc: attacker@example.net"))
20 }
21
22 func testACarriageReturnIsRefused() {
23 XCTAssertNil(SMTPMessage.validatedHeaderValue("example.com\r"))
24 }
25
26 func testACRLFPairIsRefused() {
27 XCTAssertNil(SMTPMessage.validatedHeaderValue("a\r\nb"))
28 }
29
30 // MARK: Dot stuffing
31
32 /// The case that ends the message early: a line holding nothing but a dot.
33 func testALoneDotIsDoubled() {
34 XCTAssertEqual(SMTPMessage.dotStuffed("before\r\n.\r\nafter"), "before\r\n..\r\nafter")
35 }
36
37 func testALineStartingWithADotIsDoubled() {
38 XCTAssertEqual(SMTPMessage.dotStuffed("a\r\n.MAIL FROM:<x>"), "a\r\n..MAIL FROM:<x>")
39 }
40
41 func testTheFirstLineIsStuffedToo() {
42 XCTAssertEqual(SMTPMessage.dotStuffed(".leading"), "..leading")
43 }
44
45 func testOrdinaryTextIsUnchanged() {
46 let body = "Subject: fine\r\n\r\nA domain changed. Nothing to stuff."
47 XCTAssertEqual(SMTPMessage.dotStuffed(body), body)
48 }
49
50 func testADotInsideALineIsNotTouched() {
51 XCTAssertEqual(SMTPMessage.dotStuffed("example.com changed"), "example.com changed")
52 }
53}
54
55/// The deep link is the one path into these values from outside the app.
56final class DeepLinkSanitizingTests: XCTestCase {
57
58 func testAPlainDomainIsAccepted() {
59 let url = URL(string: "domaindig://inspect?domain=example.com")!
60 XCTAssertEqual(DomainDigDeepLink.action(from: url), .inspect("example.com"))
61 }
62
63 func testAnEncodedLineBreakRefusesTheLink() {
64 let url = URL(string: "domaindig://inspect?domain=example.com%0ABcc:%20attacker@example.net")!
65 XCTAssertNil(DomainDigDeepLink.action(from: url))
66 }
67
68 /// A trailing break is trimmed before the check, which leaves a clean
69 /// value — so the link is honoured rather than refused. Only an interior
70 /// one survives trimming, and that is the case worth refusing.
71 func testATrailingCarriageReturnIsTrimmed() {
72 let url = URL(string: "domaindig://watch?domain=example.com%0D")!
73 XCTAssertEqual(DomainDigDeepLink.action(from: url), .watch("example.com"))
74 }
75
76 func testAnInteriorCarriageReturnRefusesTheLink() {
77 let url = URL(string: "domaindig://watch?domain=example%0D.com")!
78 XCTAssertNil(DomainDigDeepLink.action(from: url))
79 }
80
81 func testAnEncodedNullRefusesTheLink() {
82 let url = URL(string: "domaindig://domain?domain=example%00.com")!
83 XCTAssertNil(DomainDigDeepLink.action(from: url))
84 }
85}
Shared/DomainDigDeepLink.swift +5 −1
@@ -59,7 +59,11 @@ enum DomainDigDeepLink {
59 .first { $0.name == "domain" }? 59 .first { $0.name == "domain" }?
60 .value? 60 .value?
61 .trimmingCharacters(in: .whitespacesAndNewlines) ?? "" 61 .trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
62 guard !domain.isEmpty else { return nil } 62 // A control character inside the value is not a domain, and it travels
63 // on into stored rows and, from there, into mail headers. Refuse the
64 // whole link rather than carry it.
65 guard !domain.isEmpty,
66 domain.rangeOfCharacter(from: .controlCharacters) == nil else { return nil }
63 67
64 switch url.host() { 68 switch url.host() {
65 case "inspect": return .inspect(domain) 69 case "inspect": return .inspect(domain)