Commit 4b2e115e80
Verified · cmc
Layout: unified · split
DomainDataPortabilityService.swift +2
| @@ -522,6 +522,7 @@ enum DataValidationService { | ||
| 522 | 522 | private nonisolated static func normalizeDomain(_ value: String) -> String { |
| 523 | 523 | value |
| 524 | 524 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 525 | .components(separatedBy: .controlCharacters).joined() | |
| 525 | 526 | .replacingOccurrences(of: "https://", with: "") |
| 526 | 527 | .replacingOccurrences(of: "http://", with: "") |
| 527 | 528 | .components(separatedBy: "/") |
| @@ -1332,6 +1333,7 @@ enum DomainDataPortabilityService { | ||
| 1332 | 1333 | private nonisolated static func normalizeDomain(_ value: String) -> String { |
| 1333 | 1334 | value |
| 1334 | 1335 | .trimmingCharacters(in: .whitespacesAndNewlines) |
| 1336 | .components(separatedBy: .controlCharacters).joined() | |
| 1335 | 1337 | .replacingOccurrences(of: "https://", with: "") |
| 1336 | 1338 | .replacingOccurrences(of: "http://", with: "") |
| 1337 | 1339 | .components(separatedBy: "/") |
DomainDig.xcodeproj/project.pbxproj +4
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | E959C4D24DAAB3CB80D854B2 /* DiffServiceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A25DF2B8BB52589D49986B /* DiffServiceTests.swift */; }; |
| 29 | 29 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000201 /* InspectionInputTests.swift */; }; |
| 30 | 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 | 32 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000301 /* AvailabilityTests.swift */; }; |
| 32 | 33 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */; }; |
| 33 | 34 | A1B2C3D40000000000000502 /* DomainNameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1B2C3D40000000000000501 /* DomainNameTests.swift */; }; |
| @@ -103,6 +104,7 @@ | ||
| 103 | 104 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = HistoryEntryCodableTests.swift; sourceTree = "<group>"; }; |
| 104 | 105 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = InspectionInputTests.swift; sourceTree = "<group>"; }; |
| 105 | 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 | 108 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = AvailabilityTests.swift; sourceTree = "<group>"; }; |
| 107 | 109 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CloudSyncMergeTests.swift; sourceTree = "<group>"; }; |
| 108 | 110 | A1B2C3D40000000000000501 /* DomainNameTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = DomainNameTests.swift; sourceTree = "<group>"; }; |
| @@ -245,6 +247,7 @@ | ||
| 245 | 247 | A1B2C3D40000000000000101 /* HistoryEntryCodableTests.swift */, |
| 246 | 248 | A1B2C3D40000000000000201 /* InspectionInputTests.swift */, |
| 247 | 249 | A1B2C3D40000000000000601 /* FeatureAccessTests.swift */, |
| 250 | A1B2C3D40000000000000701 /* SMTPMessageTests.swift */, | |
| 248 | 251 | A1B2C3D40000000000000301 /* AvailabilityTests.swift */, |
| 249 | 252 | A1B2C3D40000000000000401 /* CloudSyncMergeTests.swift */, |
| 250 | 253 | A1B2C3D40000000000000501 /* DomainNameTests.swift */, |
| @@ -510,6 +513,7 @@ | ||
| 510 | 513 | 99024E91EED2275FD80E83A9 /* AppInfoTests.swift in Sources */, |
| 511 | 514 | A1B2C3D40000000000000202 /* InspectionInputTests.swift in Sources */, |
| 512 | 515 | A1B2C3D40000000000000602 /* FeatureAccessTests.swift in Sources */, |
| 516 | A1B2C3D40000000000000702 /* SMTPMessageTests.swift in Sources */, | |
| 513 | 517 | A1B2C3D40000000000000302 /* AvailabilityTests.swift in Sources */, |
| 514 | 518 | A1B2C3D40000000000000402 /* CloudSyncMergeTests.swift in Sources */, |
| 515 | 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. | |
| 671 | enum 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 | ||
| 669 | 690 | private enum SMTPClient { |
| 670 | 691 | static func send( |
| 671 | 692 | event: MonitoringEvent, |
| @@ -717,10 +738,18 @@ private enum SMTPClient { | ||
| 717 | 738 | .sorted { $0.key < $1.key } |
| 718 | 739 | .map { "\($0.key): \($0.value)" } |
| 719 | 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 | 749 | let body = [ |
| 721 | 750 | "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)", | |
| 724 | 753 | "Date: \(DateFormatter.rfc2822.string(from: Date()))", |
| 725 | 754 | "", |
| 726 | 755 | event.summary, |
| @@ -733,7 +762,7 @@ private enum SMTPClient { | ||
| 733 | 762 | ] |
| 734 | 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 | 766 | _ = try await channel.readResponse(expecting: [250]) |
| 738 | 767 | _ = try await channel.sendCommand("QUIT", expecting: [221]) |
| 739 | 768 | await channel.cancel() |
DomainDigTests/SMTPMessageTests.swift added +85
| @@ -0,0 +1,85 @@ | ||
| 1 | import 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. | |
| 10 | final 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. | |
| 56 | final 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 | 59 | .first { $0.name == "domain" }? |
| 60 | 60 | .value? |
| 61 | 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 | 68 | switch url.host() { |
| 65 | 69 | case "inspect": return .inspect(domain) |