SPB Git

Repository Public License

URLHygieneTests.swift 7.6 KB 122 lines swift

1import Foundation2import NavigatorCore3import Persistence4import Testing5@testable import PrivacyEngine67@Suite struct TrackingParameterTests {8    @Test(arguments: [9        ("https://example.com/a?utm_source=x&utm_medium=y&id=42", "https://example.com/a?id=42"),10        ("https://example.com/a?id=42&fbclid=AbC123", "https://example.com/a?id=42"),11        ("https://example.com/?gclid=1&gbraid=2&wbraid=3&msclkid=4&dclid=5", "https://example.com/"),12        ("https://example.com/p?q=caf%C3%A9+au+lait&UTM_Campaign=Z&x=a%26b", "https://example.com/p?q=caf%C3%A9+au+lait&x=a%26b"),13        ("https://example.com/p?a=1&mc_eid=9&b=&c#section", "https://example.com/p?a=1&b=&c#section"),14        ("https://example.com/p?igshid=1#utm_source=kept-in-fragment", "https://example.com/p#utm_source=kept-in-fragment"),15        ("https://example.com/p?_hsenc=1&_hsmi=2&mkt_tok=3&pk_campaign=4&mtm_source=5&hsa_cam=6", "https://example.com/p"),16        ("https://example.com/p?utm%5Fsource=encoded-name&k=v", "https://example.com/p?k=v"),17        ("http://example.com/?ttclid=1&twclid=2&yclid=3&li_fat_id=4&srsltid=5&keep=1", "http://example.com/?keep=1"),18    ])19    func trackingParametersAreRemoved(input: String, expected: String) throws {20        let result = TrackingParameterStripper.strip(try #require(URL(string: input)))21        #expect(result.url.absoluteString == expected)22        #expect(!result.removed.isEmpty)23    }2425    @Test(arguments: [26        "https://example.com/a?id=42&ref=home", "https://example.com/a", "https://example.com/a?", "https://example.com/?utmost=1&gclidx=2&myfbclid=3",27        "https://example.com/?q=utm_source", "https://example.com/utm_source=x/page", "mailto:someone@example.com?utm_source=x",28        "file:///tmp/a?utm_source=x", "orix://settings?utm_source=x", "https://example.com/?a=1&&b=2",29    ])30    func everythingElseIsLeftByteForByte(input: String) throws {31        let url = try #require(URL(string: input))32        let result = TrackingParameterStripper.strip(url)33        #expect(result.url == url && result.removed.isEmpty)34    }3536    @Test func removedNamesAreReported() throws {37        let result = TrackingParameterStripper.strip(try #require(URL(string: "https://e.com/?utm_source=a&x=1&fbclid=b")))38        #expect(result.removed == ["utm_source", "fbclid"])39        #expect(TrackingParameterStripper.isTracking("UTM_TERM") && !TrackingParameterStripper.isTracking("id"))40    }4142    @Test func strippingIsOptIn() async throws {43        let database = try await NavigatorDatabase.inMemory()44        let url = try #require(URL(string: "https://example.com/?utm_source=a&id=1"))45        #expect(await TrackingParameterStripper.urlForSharing(url, database: database) == url)46        try await database.settings.set(PrivacySettings.stripTrackingParameters, to: true)47        #expect(await TrackingParameterStripper.urlForSharing(url, database: database).absoluteString == "https://example.com/?id=1")48    }49}5051@Suite struct PunycodeTests {52    /// Sample strings of RFC 3492 section 7.1 and well-known IDN labels.53    @Test(arguments: [54        ("bcher-kva", "bücher"), ("mnchen-3ya", "münchen"), ("80ak6aa92e", "аррӏе"), ("maana-pta", "mañana"), ("fiqs8s", "中国"), ("fsq", "例"),55        ("e1afmkfd", "пример"), ("hxajbheg2az3al", "παράδειγμα"), ("egbpdaj6bu4bxfgehfvwxn", "ليهمابتكلموشعربي؟"),56        ("ihqwcrb4cv8a8dqg056pqjye", "他们为什么不说中文"), ("-with-leading-hyphen-", "-with-leading-hyphen"), ("abc-", "abc"),57    ])58    func decodesKnownVectors(encoded: String, expected: String) {59        #expect(Punycode.decode(encoded) == expected)60    }6162    @Test(arguments: ["bücher", "a!b", "99999999999999999999", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "a-b-\u{7F}"])63    func rejectsInvalidInput(encoded: String) { #expect(Punycode.decode(encoded) == nil) }6465    @Test func hostsAreDecodedLabelByLabel() {66        #expect(Punycode.unicodeHost("www.xn--bcher-kva.example") == "www.bücher.example")67        #expect(Punycode.unicodeHost("XN--MNCHEN-3YA.de") == "münchen.de")68        #expect(Punycode.unicodeHost("plain.example") == "plain.example")69        #expect(Punycode.unicodeHost("xn--!!!.example") == "xn--!!!.example")70    }71}7273@Suite struct HomographDetectorTests {74    @Test(arguments: ["example.com", "www.bücher.de", "xn--bcher-kva.de", "münchen.de", "пример.рф", "例え.jp", "東京カフェ.jp", "한국어.kr",75                      "παράδειγμα.gr", "español.es", "a-b-c.example.co.uk", "中文abc.cn", "日本語tokyo.jp", "яндекс.рф", "москва.ru"])76    func ordinaryNamesAreNotFlagged(host: String) {77        let assessment = HomographDetector.assess(host: host)78        #expect(!assessment.isSuspicious, "\(host): \(assessment.warnings)")79        #expect(assessment.displayHost == assessment.unicodeHost)80        #expect(assessment.asciiHost.utf8.allSatisfy { $0 < 0x80 })81    }8283    @Test func mixedLatinAndCyrillicIsFlagged() {84        // "pаypal" with a Cyrillic "а".85        let assessment = HomographDetector.assess(host: "p\u{430}ypal.com")86        #expect(assessment.warnings == [.mixedScripts(label: "p\u{430}ypal", scripts: [.cyrillic, .latin])])87        #expect(assessment.asciiHost == "xn--pypal-4ve.com" && assessment.displayHost == "xn--pypal-4ve.com")88        #expect(HomographDetector.assess(host: "xn--pypal-4ve.com").warnings == assessment.warnings)89        #expect(HomographDetector.assess(host: "g\u{3BF}\u{3BF}gle.com").isSuspicious)        // Greek omicrons90        #expect(HomographDetector.assess(host: "\u{4E2D}\u{6587}\u{430}.com").isSuspicious)   // Han + Cyrillic91        #expect(HomographDetector.assess(host: "login.p\u{430}ypal.example.org").isSuspicious) // any label92    }9394    @Test func wholeScriptLookalikesUnderALatinTLDAreFlagged() {95        // "аррӏе.com": entirely Cyrillic, reads "apple".96        let assessment = HomographDetector.assess(host: "xn--80ak6aa92e.com")97        #expect(assessment.warnings == [.latinLookalike(label: "аррӏе")])98        #expect(assessment.displayHost == "xn--80ak6aa92e.com")99        #expect(HomographDetector.assess(host: "\u{3BF}\u{3C1}\u{3B1}.com").warnings == [.latinLookalike(label: "ορα")])100        // The same letters under a Cyrillic TLD are an ordinary Cyrillic name; real words use other letters too.101        #expect(!HomographDetector.assess(host: "аррӏе.рф").isSuspicious)102        #expect(!HomographDetector.assess(host: "привет.com").isSuspicious)103        #expect(!HomographDetector.assess(host: "сосо.ru").isSuspicious)    // look-alike letters, but a Cyrillic-speaking registry104        #expect(HomographDetector.assess(host: "сосо.com").isSuspicious)105    }106107    @Test func invisibleCharactersAndBadPunycodeAreFlagged() {108        #expect(HomographDetector.assess(host: "exam\u{200B}ple.com").warnings.contains(.invisibleCharacters))109        #expect(HomographDetector.assess(host: "exam\u{202E}ple.com").warnings.contains(.invisibleCharacters))110        #expect(HomographDetector.assess(host: "exa\u{AD}mple.com").warnings.contains(.invisibleCharacters))111        #expect(HomographDetector.assess(host: "xn--!!!.com").warnings == [.malformedPunycode(label: "xn--!!!")])112        #expect(HomographDetector.assess(host: "xn--.com").isSuspicious)113    }114115    @Test func urlsAreAssessedByHost() throws {116        #expect(HomographDetector.assess(try #require(URL(string: "https://xn--80ak6aa92e.com/login")))?.isSuspicious == true)117        #expect(HomographDetector.assess(try #require(URL(string: "https://example.com/p\u{430}ypal")))?.isSuspicious == false)118        #expect(HomographDetector.assess(try #require(URL(string: "mailto:a@b.c"))) == nil)119        #expect(HomographDetector.assess(host: "EXAMPLE.com").asciiHost == "example.com")120    }121}122
Files main
Menu

Theme