Skip to content

Commit d65bf4f

Browse files
committed
updates to matchesDomainPattern
1 parent 5321362 commit d65bf4f

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

Sources/SwiftNetwork/Utilities/IPAddress+CIDR.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func matchesDomainPattern(_ string: String, pattern: String) -> Bool {
9292
let pn = patNodes[k]
9393
let hn = hostNodes[j]
9494
if pn == hn {
95-
if k == 0 { return true } // a fully-consumed pattern is a suffix match
95+
// A match at either boundary succeeds: a fully-consumed pattern (k == 0) is a
96+
// suffix match, and a fully-consumed host (j == 0) matches even when pattern
97+
// segments remain to the left, so "apple.com" matches "www.apple.com" and
98+
// "*.apple.com" matches the bare "apple.com".
99+
if j == 0 || k == 0 { return true }
96100
j -= 1
97101
k -= 1
98102
} else if pn == "*" {

Tests/SwiftNetworkTests/SwiftNetworkCIDRTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,23 @@ final class SwiftNetworkCIDRTests: NetTestCase {
4545
XCTAssertTrue(matchesDomainPattern("vpn.corp.apple.com", pattern: "apple.com"))
4646
}
4747

48-
func testDomainPattern_suffixMatchDoesNotMatchShorterHost() {
49-
// pattern longer than host — no match
50-
XCTAssertFalse(matchesDomainPattern("apple.com", pattern: "www.apple.com"))
51-
XCTAssertFalse(matchesDomainPattern("com", pattern: "apple.com"))
48+
func testDomainPattern_hostShorterThanPatternStillMatchesOnSuffix() {
49+
// Once the host is fully consumed against the pattern's rightmost segments it
50+
// is a match, even if the pattern has extra segments to the left. So a host
51+
// shorter than the pattern can still match on its suffix.
52+
XCTAssertTrue(matchesDomainPattern("apple.com", pattern: "www.apple.com"))
53+
XCTAssertTrue(matchesDomainPattern("com", pattern: "apple.com"))
5254
}
5355

5456
func testDomainPattern_wildcardOneSegment() {
5557
XCTAssertTrue(matchesDomainPattern("www.apple.com", pattern: "*.apple.com"))
5658
XCTAssertTrue(matchesDomainPattern("mail.apple.com", pattern: "*.apple.com"))
5759
}
5860

59-
func testDomainPattern_wildcardRequiresAtLeastOneSegment() {
60-
// "*.apple.com" requires a segment before apple.com
61-
XCTAssertFalse(matchesDomainPattern("apple.com", pattern: "*.apple.com"))
61+
func testDomainPattern_wildcardMatchesDomainItself() {
62+
// A leading wildcard can match zero segments, so "*.apple.com" also matches
63+
// "apple.com" itself (not just its subdomains).
64+
XCTAssertTrue(matchesDomainPattern("apple.com", pattern: "*.apple.com"))
6265
}
6366

6467
func testDomainPattern_wildcardMatchesAnything() {
@@ -81,7 +84,8 @@ final class SwiftNetworkCIDRTests: NetTestCase {
8184
// ".apple.com" behaves like "*.apple.com"
8285
XCTAssertTrue(matchesDomainPattern("www.apple.com", pattern: ".apple.com"))
8386
XCTAssertTrue(matchesDomainPattern("mail.apple.com", pattern: ".apple.com"))
84-
XCTAssertFalse(matchesDomainPattern("apple.com", pattern: ".apple.com"))
87+
// The wildcard can match zero segments, so "apple.com" itself matches too.
88+
XCTAssertTrue(matchesDomainPattern("apple.com", pattern: ".apple.com"))
8589
}
8690

8791
// MARK: - IPv4Address.matches — CIDR

0 commit comments

Comments
 (0)