SPB Git

Repository Public

UnitTests.swift 30.9 KB 411 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/SPMailCore/Tests/SPMailCoreTests/UnitTests.swift3// Description: Unit tests of SPMailCore that need no server: JSON, SSE, backoff, cipher, store, address parsing, reply builders, sanitiser, link safety, Authentication-Results.4import CryptoKit5import Foundation6import Testing7@testable import SPMailCore89func tempStore() throws -> (MailStore, InMemorySecretStore, URL) {10    let dir = FileManager.default.temporaryDirectory.appendingPathComponent("spmail-test-\(UUID().uuidString)")11    let secrets = InMemorySecretStore()12    // decrypted temporary files go to a folder of this test: a wipe must never touch the real apps' `spmail-files`13    return (try MailStore(directory: dir, secrets: secrets, temporaryFilesDirectory: dir.appendingPathComponent("tmp-files")), secrets, dir)14}1516func fileContains(_ dir: URL, _ needle: String) -> Bool {17    for f in (try? FileManager.default.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil)) ?? [] {18        if let d = try? Data(contentsOf: f), d.range(of: Data(needle.utf8)) != nil { return true }19    }20    return false21}2223func sample(_ id: String, thread: String? = nil, box: String = "in", date: TimeInterval, seen: Bool = false, flagged: Bool = false, subject: String = "Sujet", preview: String = "aperçu") -> EmailSummary {24    EmailSummary(id: id, blobId: "b\(id)", threadId: thread ?? "t\(id)", mailboxIds: [box], keywords: (seen ? ["$seen"] : []) + (flagged ? ["$flagged"] : []), size: 100,25                 receivedAt: Date(timeIntervalSince1970: date), messageId: ["<\(id)@x>"], from: [EmailAddress(name: "Alice Tremblay", email: "alice@example.org")],26                 to: [EmailAddress(email: "moi@stg.spboucher.ai")], subject: subject, preview: preview)27}2829@Suite struct JSONTests {30    @Test func roundTripAndIntegers() throws {31        let v: JSONValue = ["a": 1, "b": [true, nil, "x"], "c": 1.5, "d": ["k": "v"]]32        let s = String(decoding: try v.data(), as: UTF8.self)33        #expect(s.contains("\"a\":1") && !s.contains("1.0"))34        #expect(try JSONValue.parse(Data(s.utf8)) == v)35        #expect(v["b"][2].stringValue == "x" && v["d"]["k"].stringValue == "v" && v["zz"]["y"].isNull)36    }37    @Test func base64URL() {38        let d = Data([0xfb, 0xff, 0xfe, 0x01])39        #expect(d.base64URL == "-__-AQ" && Data(base64URL: "-__-AQ") == d)40    }41    @Test func emailDecoding() throws {42        let j = try JSONValue.parse(Data(#"{"id":"e1","threadId":"t","mailboxIds":{"a":true},"keywords":{"$seen":true,"$flagged":true},"size":12,"receivedAt":"2026-11-19T02:44:22Z","messageId":["m@x"],"inReplyTo":null,"from":[{"name":"A","email":"a@b.c"}],"to":null,"subject":"été 日本語","hasAttachment":true,"preview":"p"}"#.utf8))43        let e = try #require(EmailSummary(json: j))44        #expect(e.isSeen && e.isFlagged && !e.isDraft && e.hasAttachment && e.subject == "été 日本語" && e.from.first?.email == "a@b.c" && e.to.isEmpty)45        #expect(e.receivedAt.timeIntervalSince1970 == 1_795_056_262)46    }47    @Test func bodyDecoding() throws {48        let j = try JSONValue.parse(Data(#"{"textBody":[{"partId":"1","type":"text/plain"}],"htmlBody":[{"partId":"2","type":"text/html"}],"attachments":[{"blobId":"B","name":"pièce.pdf","type":"application/pdf","size":3,"disposition":"attachment"}],"bodyValues":{"1":{"value":"Bonjour"},"2":{"value":"<p>Bonjour</p>","isTruncated":true}},"header:Authentication-Results:asText:all":[" mx; spf=pass "]}"#.utf8))49        let b = EmailBody(json: j)50        #expect(b.text == "Bonjour" && b.html == "<p>Bonjour</p>" && b.attachments.first?.name == "pièce.pdf" && b.isTruncated && b.authenticationResults == ["mx; spf=pass"])51    }52    @Test func outgoingEmailJSON() {53        let m = OutgoingMessage(from: EmailAddress(name: "A", email: "a@stg.spboucher.ai"), to: [EmailAddress(email: "b@stg.spboucher.ai")], subject: "S", text: "T", html: "<b>T</b>",54                                attachments: [OutgoingAttachment(name: "f.txt", type: "text/plain", size: 1, blobId: "B"), OutgoingAttachment(name: "pending.txt", type: "text/plain", size: 1)],55                                inReplyTo: ["<p@x>"], references: ["<r@x>", "<p@x>"])56        let e = m.jmapEmail(mailboxId: "d", draft: true)57        #expect(e["mailboxIds"]["d"].boolValue == true && e["keywords"]["$draft"].boolValue == true && e["bodyValues"]["h"]["value"].stringValue == "<b>T</b>")58        #expect(e["attachments"].arrayValue?.count == 1 && e["inReplyTo"].stringArray == ["<p@x>"] && e["references"].stringArray.count == 2 && e["cc"].isNull)59    }60}6162@Suite struct EventSourceTests {63    @Test func parsesChunkedStreamWithPingsAndCRLF() {64        var p = SSEParser()65        var events: [SSEEvent] = []66        for chunk in [": ping\n\nevent: sta", "te\ndata: {\"@type\":\"StateChange\",", "\"changed\":{\"i\":{\"Email\":\"s2\"}}}\n", "\n", "data: a\r\ndata: b\r\n\r\n", "retry: 5000\nid: 7\ndata: z\n\n"] {67            events += p.feed(chunk)68        }69        #expect(events.count == 3)70        #expect(events[0].event == "state" && StateChange(data: events[0].data)?.changed == ["i": ["Email": "s2"]])71        #expect(events[1] == SSEEvent(event: "message", data: "a\nb", id: nil))72        #expect(events[2].id == "7" && p.retryMilliseconds == 5000)73    }74    @Test func stateChangeTypes() {75        let s = StateChange(data: #"{"@type":"StateChange","changed":{"i":{"Mailbox":"sai","Email":"sam"}}}"#)76        #expect(s?.types(for: "i") == ["Mailbox", "Email"] && s?.types(for: "other").isEmpty == true && StateChange(data: "nope") == nil)77    }78    @Test func backoffGrowsAndCaps() {79        var b = Backoff(base: 1, factor: 2, maximum: 30, jitter: 0)80        #expect((0..<7).map { _ in b.next() } == [1, 2, 4, 8, 16, 30, 30])81        b.reset()82        #expect(b.next() == 1)83        var j = Backoff(base: 10, factor: 2, maximum: 300, jitter: 0.2)84        let v = j.next()85        #expect(v >= 8 && v <= 12)86    }87}8889@Suite struct CipherTests {90    @Test func sealOpenAndTamper() throws {91        let secrets = InMemorySecretStore()92        let c = try CacheCipher.load(from: secrets)93        let sealed = try c.seal("Bonjour — été", context: "a/1")94        #expect(try c.openString(sealed, context: "a/1") == "Bonjour — été")95        #expect(sealed.range(of: Data("Bonjour".utf8)) == nil)96        #expect(throws: (any Error).self) { try c.openString(sealed, context: "a/2") } // bound to its row97        var bad = sealed; bad[bad.count - 1] ^= 198        #expect(throws: (any Error).self) { try c.open(bad, context: "a/1") }99        #expect(try c.seal("x") != c.seal("x")) // fresh nonce100        // same key is reloaded, another install cannot read101        #expect(try CacheCipher.load(from: secrets).openString(sealed, context: "a/1") == "Bonjour — été")102        #expect(throws: (any Error).self) { try CacheCipher.load(from: InMemorySecretStore()).open(sealed, context: "a/1") }103        #expect(throws: (any Error).self) { try CacheCipher.load(from: InMemorySecretStore(), create: false) }104    }105}106107@Suite struct StoreTests {108    @Test func threadsScopesAndCounts() async throws {109        let (store, _, _) = try tempStore()110        try await store.upsertMailboxes("A", [Mailbox(id: "in", name: "Inbox", role: "inbox"), Mailbox(id: "tr", name: "Trash", role: "trash"), Mailbox(id: "x", name: "Projets")])111        try await store.upsertEmails("A", [sample("1", thread: "T", date: 100, seen: true), sample("2", thread: "T", date: 200), sample("3", date: 150, flagged: true),112                                           sample("4", box: "tr", date: 300), sample("5", thread: "T", box: "x", date: 250, seen: true)])113        let inbox = try await store.threads("A", scope: .mailbox("in"))114        #expect(inbox.map(\.email.id) == ["2", "3"])115        #expect(inbox[0].count == 2 && inbox[0].unreadCount == 1 && inbox[1].anyFlagged)116        #expect(try await store.threads("A", scope: .mailbox("in"), threaded: false).map(\.email.id) == ["2", "3", "1"])117        #expect(try await store.threads("A", scope: .unread).map(\.email.id) == ["2", "3"]) // the unread message in the Trash is excluded118        #expect(try await store.threads("A", scope: .flagged).map(\.email.id) == ["3"])119        #expect(try await store.count("A", scope: .mailbox("in")) == 3)120        #expect(try await store.count("A", scope: .mailbox("in"), unreadOnly: true) == 2)121        #expect(try await store.threadEmails("A", threadId: "T").map(\.id) == ["1", "2", "5"])122        #expect(try await store.threads("B", scope: .mailbox("in")).isEmpty) // accounts are separate123        #expect(try await store.mailboxes("A").map(\.id) == ["in", "tr", "x"])124        #expect(try await store.oldestReceived("A", mailboxId: "in") == Date(timeIntervalSince1970: 100))125        // a move replaces the membership126        var moved = sample("3", box: "x", date: 150, flagged: true)127        moved.preview = "déplacé"128        try await store.upsertEmails("A", [moved])129        #expect(try await store.threads("A", scope: .mailbox("in")).map(\.email.id) == ["2"])130        #expect(try await store.email("A", id: "3")?.preview == "déplacé")131        try await store.deleteEmails("A", ids: ["2", "1"])132        #expect(try await store.threads("A", scope: .mailbox("in")).isEmpty)133        #expect(try await store.knownIds("A", among: ["1", "3", "5"]) == ["3", "5"])134    }135136    @Test func previewsBodiesAndOutboxAreSealedAtRest() async throws {137        let (store, secrets, dir) = try tempStore()138        try await store.upsertEmails("A", [sample("1", date: 1, subject: "Sujet visible", preview: "APERCU-SECRET-42 du message")])139        try await store.saveBody("A", id: "1", EmailBody(text: "CORPS-SECRET-42 confidentiel", html: "<p>HTML-SECRET-42</p>"))140        var item = OutboxItem(account: "A", message: OutgoingMessage(from: EmailAddress(email: "a@stg.spboucher.ai"), to: [EmailAddress(email: "b@stg.spboucher.ai")], subject: "BROUILLON-SECRET-42", text: "texte"))141        item.message.attachments = [OutgoingAttachment(id: "att1", name: "f.bin", type: "application/octet-stream", size: 20)]142        try await store.saveOutbox(item)143        try await store.saveAttachmentData("att1", outboxId: item.id, Data("PIECE-SECRETE-42".utf8))144        try await store.setValue("identities/A", [Identity(id: "i", name: "N", email: "a@stg.spboucher.ai", textSignature: "SIGNATURE-SECRETE-42")])145        try await store.checkpoint()146        for secret in ["APERCU-SECRET-42", "CORPS-SECRET-42", "HTML-SECRET-42", "BROUILLON-SECRET-42", "PIECE-SECRETE-42", "SIGNATURE-SECRETE-42"] { #expect(!fileContains(dir, secret), "\(secret) is in clear on disk") }147        #expect(fileContains(dir, "Sujet visible")) // header fields are indexed in clear, by design148        #expect(try await store.body("A", id: "1")?.text == "CORPS-SECRET-42 confidentiel")149        #expect(try await store.email("A", id: "1")?.preview == "APERCU-SECRET-42 du message")150        #expect(try await store.attachmentData("att1") == Data("PIECE-SECRETE-42".utf8))151        #expect(try await store.outboxItem(item.id)?.message.subject == "BROUILLON-SECRET-42")152        #expect(try await store.value("identities/A", as: [Identity].self)?.first?.textSignature == "SIGNATURE-SECRETE-42")153        // 401 path: mail goes, the unsent draft stays and is still readable with the key154        try await store.wipeKeepingOutbox()155        #expect(try await store.email("A", id: "1") == nil)156        #expect(try await store.body("A", id: "1") == nil)157        #expect(try await store.outbox().count == 1)158        // another install key cannot read the file159        let other = try MailStore(directory: dir, secrets: InMemorySecretStore(), temporaryFilesDirectory: dir.appendingPathComponent("tmp-files"))160        #expect(try await other.outbox().isEmpty)161        // sign-out: files and key are gone162        try await store.destroy()163        #expect(!FileManager.default.fileExists(atPath: dir.appendingPathComponent("mail.sqlite").path))164        #expect(secrets.read(SecretKeys.cacheKey) == nil)165    }166167    @Test func localSearchReadsHeadersPreviewsAndBodies() async throws {168        let (store, _, _) = try tempStore()169        try await store.upsertEmails("A", [sample("1", date: 3, subject: "Facture de novembre"), sample("2", date: 2, preview: "le rendez-vous de jeudi"), sample("3", date: 1), sample("4", date: 0, subject: "100%_fini")])170        try await store.saveBody("A", id: "3", EmailBody(html: "<p>Le <b>contrat</b> sign&eacute;</p>"))171        #expect(try await store.search("A", text: "FACTURE").map(\.id) == ["1"])172        #expect(try await store.search("A", text: "alice").count == 4)173        #expect(try await store.search("A", text: "rendez-vous").map(\.id) == ["2"])174        #expect(try await store.search("A", text: "contrat signé").map(\.id) == ["3"])175        #expect(try await store.search("A", text: "%_").map(\.id) == ["4"])176        #expect(try await store.search("A", text: "absent").isEmpty)177    }178179    @Test func pendingOperationsStatesAndCorrespondents() async throws {180        let (store, _, _) = try tempStore()181        try await store.addPending("A", emailId: "1", patch: ["keywords/$seen": true])182        try await store.addPending("A", emailId: "2", patch: ["@destroy": true])183        let ops = try await store.pending("A")184        #expect(ops.map(\.emailId) == ["1", "2"] && ops[0].patch["keywords/$seen"].boolValue == true)185        try await store.removePending(ids: [ops[0].id])186        #expect(try await store.pending("A").count == 1)187        try await store.setState("A", "Email", "s1")188        try await store.setState("A", "Email", "s2")189        #expect(try await store.state("A", "Email") == "s2")190        try await store.setState("A", "Email", nil)191        #expect(try await store.state("A", "Email") == nil)192        try await store.noteCorrespondents([EmailAddress(name: "Bob Roy", email: "Bob@Example.org"), EmailAddress(email: "carole@example.org"), EmailAddress(email: "bad")])193        try await store.noteCorrespondents([EmailAddress(email: "carole@example.org")], weight: 5)194        #expect(try await store.suggestCorrespondents("roy").map(\.email) == ["bob@example.org"])195        #expect(try await store.suggestCorrespondents("c").first?.email == "carole@example.org")196        #expect(try await store.suggestCorrespondents("bad").isEmpty)197    }198199    @Test func listStaysFastWithTwelveThousandMessages() async throws {200        let (store, _, _) = try tempStore()201        var batch: [EmailSummary] = []202        for n in 0..<12_000 { batch.append(sample("m\(n)", thread: "t\(n / 3)", date: Double(n), seen: n % 4 != 0, subject: "Message \(n)", preview: "aperçu \(n)")) }203        try await store.upsertEmails("A", batch)204        let clock = ContinuousClock()205        var page: [ThreadSummary] = []206        let first = try await clock.measure { page = try await store.threads("A", scope: .mailbox("in"), limit: 100) }207        #expect(page.count == 100 && page[0].email.id == "m11999" && page[0].count == 3)208        let deep = try await clock.measure { page = try await store.threads("A", scope: .mailbox("in"), limit: 100, offset: 3900) }209        #expect(page.count == 100 && page.last?.email.threadId == "t0")210        let unread = try await clock.measure { _ = try await store.threads("A", scope: .unread, limit: 100) }211        #expect(first < .seconds(1) && deep < .seconds(1) && unread < .seconds(1), "page queries took \(first) / \(deep) / \(unread)")212    }213}214215@Suite struct ComposeTests {216    @Test func addressLists() {217        let l = AddressParser.parseList(#"a@b.co, "Doe, Jane" <jane@d.example>; Bob Roy <bob@c.example> , <solo@x.org>, nope, x@y, "Q \"uote\"" <q@z.org>"#)218        #expect(l.map(\.email) == ["a@b.co", "jane@d.example", "bob@c.example", "solo@x.org", "q@z.org"])219        #expect(l[1].name == "Doe, Jane" && l[2].name == "Bob Roy" && l[3].name == nil && l[4].name == "Q \"uote\"")220        #expect(EmailAddress(name: "Doe, Jane", email: "j@d.e").formatted == "\"Doe, Jane\" <j@d.e>")221        #expect(EmailAddress(name: "Bob Roy", email: "b@c.d").formatted == "Bob Roy <b@c.d>")222        for bad in ["", "a@", "@b.c", "a b@c.d", "a@b", "a@-b.c", ".a@b.c", "a@b..c", "a@@b.c"] { #expect(!AddressParser.isValid(bad), "\(bad)") }223        #expect(AddressParser.isValid("prénom.nom+tag@sous.domaine.example"))224    }225226    @Test func subjectsAndThreadingHeaders() {227        #expect(ReplyBuilder.subject("Bonjour", kind: .reply) == "Re: Bonjour")228        #expect(ReplyBuilder.subject("RE: Bonjour", kind: .replyAll) == "RE: Bonjour")229        #expect(ReplyBuilder.subject("Bonjour", kind: .forward) == "Fwd: Bonjour")230        #expect(ReplyBuilder.subject("TR: Bonjour", kind: .forward) == "TR: Bonjour")231        var e = sample("1", date: 0)232        e.messageId = ["<c@x>"]; e.references = ["<a@x>", "<b@x>"]; e.inReplyTo = ["<b@x>"]233        #expect(ReplyBuilder.threading(for: e).inReplyTo == ["<c@x>"])234        #expect(ReplyBuilder.threading(for: e).references == ["<a@x>", "<b@x>", "<c@x>"])235        e.references = []236        #expect(ReplyBuilder.threading(for: e).references == ["<b@x>", "<c@x>"]) // falls back on the parent's In-Reply-To237    }238239    @Test func recipientsAndQuoting() {240        var e = sample("1", date: 1_700_000_000, subject: "Projet")241        e.from = [EmailAddress(name: "Alice", email: "alice@example.org")]242        e.to = [EmailAddress(email: "moi@stg.spboucher.ai"), EmailAddress(email: "bob@example.org")]243        e.cc = [EmailAddress(email: "carole@example.org"), EmailAddress(email: "ALICE@example.org")]244        let me: Set<String> = ["moi@stg.spboucher.ai"]245        #expect(ReplyBuilder.recipients(for: e, kind: .reply, me: me).to.map(\.email) == ["alice@example.org"])246        let all = ReplyBuilder.recipients(for: e, kind: .replyAll, me: me)247        #expect(all.to.map(\.email) == ["alice@example.org"] && all.cc.map(\.email) == ["bob@example.org", "carole@example.org"])248        e.replyTo = [EmailAddress(email: "liste@example.org")]249        #expect(ReplyBuilder.recipients(for: e, kind: .reply, me: me).to.map(\.email) == ["liste@example.org"])250        // answering my own sent message goes to its recipients, not to me251        var mine = e; mine.from = [EmailAddress(email: "moi@stg.spboucher.ai")]; mine.replyTo = []; mine.to = [EmailAddress(email: "bob@example.org")]252        #expect(ReplyBuilder.recipients(for: mine, kind: .reply, me: me).to.map(\.email) == ["bob@example.org"])253254        let identity = Identity(id: "i", name: "Moi", email: "moi@stg.spboucher.ai", textSignature: "Moi\nSP")255        let body = EmailBody(text: "Ligne 1\n> déjà cité", html: "<p>Ligne 1</p><script>x()</script>", attachments: [BodyPart(blobId: "B", size: 3, name: "a.pdf", type: "application/pdf")])256        let reply = ReplyBuilder.build(kind: .reply, original: e, body: body, identity: identity, me: me)257        #expect(reply.subject == "Re: Projet" && reply.inReplyTo == ["<1@x>"] && reply.relation == .reply && reply.relatedEmailId == "1" && reply.attachments.isEmpty)258        #expect(reply.text == "\n\n-- \nMoi\nSP" && reply.fullText.contains("> Ligne 1") && reply.fullText.contains(">> déjà cité") && reply.fullText.contains("a écrit :"))259        #expect(reply.fullHTML?.contains("<blockquote") == true && reply.fullHTML?.contains("script") == false && reply.fullHTML?.contains("Moi<br>SP") == true)260        #expect(reply.jmapEmail(mailboxId: "d", draft: true)["bodyValues"]["t"]["value"].stringValue == reply.fullText)261        let fwd = ReplyBuilder.build(kind: .forward, original: e, body: body, identity: identity, me: me, labels: .english)262        #expect(fwd.subject == "Fwd: Projet" && fwd.to.isEmpty && fwd.inReplyTo.isEmpty && fwd.attachments.first?.blobId == "B" && fwd.relation == .forward)263        #expect(fwd.fullText.contains("Forwarded message") && fwd.fullText.contains("Subject: Projet") && fwd.fullText.contains("Ligne 1"))264        #expect(ReplyBuilder.blank(identity: identity).isEmpty && !reply.isEmpty)265    }266267    @Test func dangerousAttachmentTypes() {268        for n in ["facture.pdf.exe", "macro.docm", "run.command", "page.html", "x.JS", "installer.pkg"] { #expect(OutgoingAttachment.isDangerous(name: n, type: "application/octet-stream"), "\(n)") }269        #expect(OutgoingAttachment.isDangerous(name: "image", type: "image/svg+xml"))270        for n in ["photo.jpg", "rapport.pdf", "notes.txt", "tableau.xlsx"] { #expect(!OutgoingAttachment.isDangerous(name: n, type: "application/pdf"), "\(n)") }271    }272273    @Test func outboxDeliveryRules() {274        var item = OutboxItem(account: "A", message: OutgoingMessage(from: EmailAddress(email: "a@x.y")))275        #expect(!item.isFullyDelivered)276        item.state = .accepted277        #expect(!item.isFullyDelivered && !item.deliveryIsFinal) // accepted is not delivered278        item.delivery = ["b@x.y": DeliveryStatus(delivered: .queued, smtpReply: "250 Queued")]279        #expect(!item.isFullyDelivered && !item.deliveryIsFinal)280        item.delivery = ["b@x.y": DeliveryStatus(delivered: .yes, smtpReply: "250 OK"), "c@x.y": DeliveryStatus(delivered: .no, smtpReply: "550")]281        #expect(!item.isFullyDelivered && item.hasDeliveryFailure && item.deliveryIsFinal)282        item.delivery["c@x.y"] = DeliveryStatus(delivered: .yes, smtpReply: "250 OK")283        #expect(item.isFullyDelivered)284    }285286    @Test func emlFileNames() {287        let d = Date(timeIntervalSince1970: 1_795_056_262)288        #expect(EMLExport.fileName(subject: "Re: a/b\\c:d*?\"<>|", date: d) == "2026-11-19 Re- a-b-c-d------.eml")289        #expect(EMLExport.fileName(subject: "  ", date: d) == "2026-11-19 message.eml")290        #expect(EMLExport.fileName(subject: "..caché", date: d) == "2026-11-19 caché.eml")291    }292}293294@Suite struct SanitizerTests {295    func clean(_ s: String, remote: Bool = false) -> String { HTMLSanitizer.sanitize(s, allowRemoteImages: remote).html }296297    @Test func removesActiveContent() {298        let dirty = """299        <html><head><title>T</title><meta http-equiv="refresh" content="0;url=http://evil.example"><base href="http://evil.example/"><link rel="stylesheet" href="http://evil.example/x.css">300        <script>alert(1)</script></head><body onload="x()"><p onclick="steal()" style="color:red">Bonjour <b>vous</b></p>301        <iframe src="http://evil.example"></iframe><object data="x.swf"><param name="a"><embed src="y"></object><embed src="z">302        <form action="http://evil.example"><input name="pw"><button>OK</button><textarea>zzz</textarea></form>303        <svg onload="x()"><script>alert(2)</script><circle/></svg><math><mi>x</mi></math><p>Fin</p>304        <SCRIPT SRC=http://evil.example/x.js></SCRIPT><script>unterminated305        """306        let out = clean(dirty)307        for bad in ["script", "alert", "onload", "onclick", "iframe", "object", "embed", "<form", "<input", "button", "textarea", "zzz", "meta", "base", "<link", "evil.example", "svg", "math", "<title", "unterminated"] {308            #expect(!out.lowercased().contains(bad), "\(bad) survived: \(out)")309        }310        #expect(out.contains("<p style=\"color:red\">Bonjour <b>vous</b></p>") && out.contains("<p>Fin</p>"))311    }312313    @Test func neutralisesDangerousLinks() {314        #expect(!clean(#"<a href="javascript:alert(1)">x</a>"#).contains("href"))315        #expect(!clean(#"<a href="  JaVa&#x09;Script:alert(1)">x</a>"#).contains("href"))316        #expect(!clean(#"<a href="jav&#x61;script&colon;alert(1)">x</a>"#).contains("href"))317        #expect(!clean("<a href=\"java\nscript:alert(1)\">x</a>").contains("href"))318        #expect(!clean(#"<a href="data:text/html;base64,PHNjcmlwdD4=">x</a>"#).contains("href"))319        #expect(!clean(#"<a href="vbscript:x">x</a><a href="file:///etc/passwd">y</a><a href="/relative">z</a>"#).contains("href"))320        #expect(clean(#"<a href="https://example.org/a?b=1&amp;c=2" target="_blank" onclick="x">ok</a>"#) == #"<a href="https://example.org/a?b=1&amp;c=2" rel="noopener noreferrer">ok</a>"#)321        #expect(clean(#"<a href=mailto:a@b.c>m</a>"#).contains(#"href="mailto:a@b.c""#))322        #expect(clean(#"<a href='https://x.org/"><img src=x onerror=alert(1)>'>t</a>"#).contains("<img") == false)323    }324325    @Test func blocksRemoteLoadsUntilOptIn() {326        let html = #"<img src="https://tracker.example/p.gif?id=1" width="1" height="1"><img src="//cdn.example/a.png" srcset="https://cdn.example/b.png 2x"><img src="cid:logo@x"><img src="data:image/png;base64,iVBORw0KGgo="><img src="data:image/svg+xml;base64,AAAA"><img src="file:///x"><td background="http://bg.example/x.png" style="background:url(http://css.example/y.png)">c</td>"#327        let blocked = HTMLSanitizer.sanitize(html)328        #expect(blocked.remoteImageCount == 2 && blocked.cids == ["logo@x"])329        for bad in ["tracker.example", "cdn.example", "srcset", "bg.example", "css.example", "svg", "file:"] { #expect(!blocked.html.contains(bad), "\(bad) survived") }330        #expect(blocked.html.contains("data-spm-remote") && blocked.html.contains("spmail-cid://part/logo%40x") && blocked.html.contains("data:image/png;base64,iVBORw0KGgo="))331        let shown = HTMLSanitizer.sanitize(html, allowRemoteImages: true)332        #expect(shown.html.contains("spmail-img://proxy/?u=https%3A%2F%2Ftracker.example%2Fp.gif%3Fid%3D1") && shown.html.contains("spmail-img://proxy/?u=https%3A%2F%2Fcdn.example%2Fa.png"))333        #expect(!shown.html.contains("src=\"http") && !shown.html.contains("srcset")) // even opted-in, nothing loads straight from the sender334    }335336    @Test func sanitisesStyleSheets() {337        let out = clean(#"<style>@import url("http://evil.example/x.css"); p{background:URL( 'http://evil.example/t.gif' );color:blue} a{b\65havior:url(x.htc);position:fixed} @font-face{font-family:x;src:url(http://evil.example/f.woff)}</style><p style="width:expression(alert(1));background-image:u\72l(http://evil.example/z)">x</p></style>"#)338        for bad in ["evil.example", "@import", "expression(", "behavior:", "position:fixed", "font-face", "url("] { #expect(!out.lowercased().contains(bad), "CSS \(bad) survived: \(out)") }339        #expect(out.contains("color:blue"))340        #expect(HTMLSanitizer.sanitizeCSS("a{}</style><script>") .contains("<") == false)341    }342343    @Test func survivesBrokenMarkup() {344        #expect(clean("a < b et c > d") == "a &lt; b et c &gt; d")345        #expect(clean("<p>non fermé <b>gras") == "<p>non fermé <b>gras</b></p>")346        #expect(clean("</div></p>texte<!-- <script>x</script> -->fin<!-- sans fin") == "textefin")347        #expect(clean("<div><p>a</div>b") == "<div><p>a</p></div>b")348        #expect(clean("<img src=\"x\" alt=\"a>b\"><br/>") == "<img alt=\"a&gt;b\"><br>")349        #expect(clean("<") == "&lt;" && clean("<p") == "<p></p>" && clean("<!DOCTYPE html><?xml ?>x") == "x")350    }351352    @Test func recordsLinksForTheDestinationPrompt() {353        let r = HTMLSanitizer.sanitize(#"<a href="https://evil.example/login">www.banque.example</a> <a href="https://ok.example"><img src="cid:a" alt="Logo"> Voir</a><a>sans lien</a>"#)354        #expect(r.links == [.init(href: "https://evil.example/login", text: "www.banque.example"), .init(href: "https://ok.example", text: "Logo Voir")])355        #expect(LinkSafety.check(text: r.links[0].text, href: r.links[0].href) == .mismatch(shown: "banque.example", actual: "evil.example"))356    }357358    @Test func linkSafetyVerdicts() {359        #expect(LinkSafety.check(text: "Cliquez ici", href: "https://example.org/x") == .ok)360        #expect(LinkSafety.check(text: "https://www.example.org/compte", href: "https://login.example.org/a") == .ok)361        #expect(LinkSafety.check(text: "example.org", href: "https://example.org.evil.test/") == .mismatch(shown: "example.org", actual: "example.org.evil.test"))362        #expect(LinkSafety.check(text: "ici", href: "https://example.org@evil.test/") == .suspicious(reason: "credentials"))363        #expect(LinkSafety.check(text: "ici", href: "http://192.168.1.10/a") == .suspicious(reason: "ip"))364        #expect(LinkSafety.check(text: "ici", href: "https://xn--pple-43d.com/") == .suspicious(reason: "punycode"))365        #expect(LinkSafety.check(text: "écrire", href: "mailto:a@b.c") == .ok)366    }367368    @Test func wrapperDocumentIsLockedDown() {369        let doc = HTMLSanitizer.document(body: "<p>x</p>", dark: true)370        #expect(doc.contains("default-src 'none'") && doc.contains("img-src data: spmail-cid: spmail-img:;") && doc.contains("form-action 'none'") && doc.contains("base-uri 'none'"))371        #expect(!doc.contains("script-src") && !doc.contains("http:") && !doc.contains("https:"))372    }373374    @Test func textConversions() {375        #expect(HTMLText.plainText(fromHTML: "<style>p{}</style><p>Bonjour&nbsp;<b>vous</b> &amp; moi</p><ul><li>un</li><li>deux</li></ul><script>x</script>ligne<br>suite &#233;t&#xE9;") == "Bonjour\u{00A0}vous & moi\n• un\n• deux\nligne\nsuite été")376        #expect(HTMLText.html(fromPlain: "a <b> & c\n> cité 1\n> cité 2\nfin") == "a &lt;b&gt; &amp; c<br><blockquote type=\"cite\">cité 1<br>cité 2</blockquote>fin")377        var a = AttributedString("gras")378        a.inlinePresentationIntent = .stronglyEmphasized379        var l = AttributedString("lien")380        l.link = URL(string: "https://example.org/?a=1&b=2")381        var j = AttributedString("js")382        j.link = URL(string: "javascript:alert(1)")383        #expect(HTMLText.html(fromAttributed: AttributedString("un <tag>\n") + a + AttributedString(" ") + l + j) == "<div>un &lt;tag&gt;<br><b>gras</b> <a href=\"https://example.org/?a=1&amp;b=2\">lien</a>js</div>")384        #expect(HTMLText.decodeEntities("&lt;&amp;&#x41;&#66;&bogus;&") == "<&AB&bogus;&")385    }386}387388@Suite struct AuthenticationTests {389    @Test func parsesTypicalHeaders() {390        let s = AuthenticationSummary.parse(["mx1.spboucher.ai; dkim=pass (2048-bit key) header.d=example.org header.s=sel header.b=abc; spf=pass (sender ok) smtp.mailfrom=bounce@mail.example.org; dmarc=pass (p=reject) header.from=example.org"])391        #expect(s.spf == .pass && s.dkim == .pass && s.dmarc == .pass && s.dkimDomain == "example.org" && s.spfDomain == "mail.example.org" && s.authServer == "mx1.spboucher.ai")392        #expect(s.risk(fromDomain: "example.org") == .authenticated)393    }394    @Test func detectsSpoofing() {395        let fail = AuthenticationSummary.parse(["mx; spf=fail smtp.mailfrom=evil.test; dkim=none; dmarc=fail header.from=banque.example"])396        #expect(fail.risk(fromDomain: "banque.example") == .spoofingSuspected && fail.dmarc.isFailure)397        // everything "passes", but for the attacker's own domain, and there is no DMARC verdict398        let other = AuthenticationSummary.parse(["mx; spf=pass smtp.mailfrom=evil.test; dkim=pass header.d=evil.test"])399        #expect(other.risk(fromDomain: "banque.example") == .spoofingSuspected)400        #expect(other.risk(fromDomain: "evil.test") == .authenticated)401        #expect(AuthenticationSummary.parse(["mx; dkim=fail header.d=x.test; dkim=pass header.d=sub.example.org"]).risk(fromDomain: "example.org") == .authenticated)402    }403    @Test func unknownAndUnverified() {404        #expect(AuthenticationSummary.parse([]).risk(fromDomain: "example.org") == .unknown)405        #expect(AuthenticationSummary.parse(["mx; none"]).risk(fromDomain: "example.org") == .unknown)406        let weak = AuthenticationSummary.parse(["mx;\r\n spf=none smtp.mailfrom=example.org;\r\n dkim=none;\r\n dmarc=none"])407        #expect(weak.spf == .none && weak.risk(fromDomain: "example.org") == .unverified)408        #expect(AuthenticationSummary.parse(["mx; spf=softfail smtp.mailfrom=example.org"]).risk(fromDomain: "example.org") == .spoofingSuspected)409    }410}411
Files main
Menu

Theme