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 {
522522 private nonisolated static func normalizeDomain(_ value: String) -> String {
523523 value
524524 .trimmingCharacters(in: .whitespacesAndNewlines)
525 .components(separatedBy: .controlCharacters).joined()
525526 .replacingOccurrences(of: "https://", with: "")
526527 .replacingOccurrences(of: "http://", with: "")
527528 .components(separatedBy: "/")
@@ -1332,6 +1333,7 @@ enum DomainDataPortabilityService {
13321333 private nonisolated static func normalizeDomain(_ value: String) -> String {
13331334 value
13341335 .trimmingCharacters(in: .whitespacesAndNewlines)
1336 .components(separatedBy: .controlCharacters).joined()
13351337 .replacingOccurrences(of: "https://", with: "")
13361338 .replacingOccurrences(of: "http://", with: "")
13371339 .components(separatedBy: "/")
DomainDig.xcodeproj/project.pbxproj +4
@@ -28,6 +28,7 @@
2828 E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; };
2929 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; };
3030 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000601 /* FeatureAccessTests.swift */; };
31 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000701 /* SMTPMessageTests.swift */; };
3132 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; };
3233 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; };
3334 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; };
@@ -103,6 +104,7 @@
103104 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; };
104105 A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; };
105106 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>"; };
106108 A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; };
107109 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; };
108110 A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; };
@@ -245,6 +247,7 @@
245247 A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */,
246248 A1B2C3D40000000000000201 /* InspectionInputTests.swift */,
247249 A1B2C3D40000000000000601 /* FeatureAccessTests.swift */,
250 A1B2C3D40000000000000701 /* SMTPMessageTests.swift */,
248251 A1B2C3D40000000000000301 /* AvailabilityTests.swift */,
249252 A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */,
250253 A1B2C3D40000000000000501 /* DomainNameTests.swift */,
@@ -510,6 +513,7 @@
510513 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */,
511514 A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */,
512515 A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */,
516 A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */,
513517 A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */,
514518 A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */,
515519 A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */,
DomainDig/IntegrationService.swift +32 −3
@@ -666,6 +666,27 @@ private enum IntegrationSecretStore {
666666 }
667667}
668668
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
669690private enum SMTPClient {
670691 static func send(
671692 event: MonitoringEvent,
@@ -717,10 +738,18 @@ private enum SMTPClient {
717738 .sorted { $0.key < $1.key }
718739 .map { "\($0.key): \($0.value)" }
719740 .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
720749 let body = [
721750 "From: DomainDig <\(configuration.senderAddress)>",
722 "To: \(configuration.recipientAddresses.joined(separator: ", "))",
723 "Subject: [DomainDig] \(event.severity.title) \(event.domain) \(event.type.title)",
751 "To: \(recipientList)",
752 "Subject: \(subject)",
724753 "Date: \(DateFormatter.rfc2822.string(from: Date()))",
725754 "",
726755 event.summary,
@@ -733,7 +762,7 @@ private enum SMTPClient {
733762 ]
734763 .joined(separator: "\r\n")
735764
736 try await channel.sendRaw(body + "\r\n.\r\n")
765 try await channel.sendRaw(SMTPMessage.dotStuffed(body) + "\r\n.\r\n")
737766 _ = try await channel.readResponse(expecting: [250])
738767 _ = try await channel.sendCommand("QUIT", expecting: [221])
739768 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 {
5959 .first { $0.name == "domain" }?
6060 .value?
6161 .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 }
6367
6468 switch url.host() {
6569 case "inspect": return .inspect(domain)