Skip to content

Commit c8d899f

Browse files
authored
Merge pull request #2223 from DataDog/maciey/RUM-8931/host-sanitizer-fix
RUM-8931 Simplify host sanitizer logic
2 parents 4795fc9 + 90e35ce commit c8d899f

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

DatadogInternal/Sources/NetworkInstrumentation/HostsSanitizer.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,21 @@ public protocol HostsSanitizing {
1515
}
1616

1717
public struct HostsSanitizer: HostsSanitizing {
18-
private let urlRegex = #"^(http|https)://(.*)"#
19-
private let hostRegex = #"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])\.)+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$"#
18+
private let hostRegex = #"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"#
2019
private let ipRegex = #"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"#
2120

2221
public init() { }
2322

2423
private func sanitize(host: String, warningMessage: String) -> (String?, String?) {
25-
if host.range(of: urlRegex, options: .regularExpression) != nil {
24+
if let sanitizedHost = URL(string: host)?.host {
2625
// if an URL is given instead of the host, take its `host` part
27-
if let sanitizedHost = URL(string: host)?.host {
28-
let warning = "'\(host)' is an url and will be sanitized to: '\(sanitizedHost)'."
29-
return (sanitizedHost, warning)
30-
} else {
31-
// otherwise, drop
32-
let warning = "'\(host)' is not a valid host name and will be dropped."
33-
return (nil, warning)
34-
}
35-
} else if host.range(of: hostRegex, options: .regularExpression) != nil {
36-
// if a valid host name is given, accept it
37-
return (host, nil)
26+
let warning = "'\(host)' is an url and will be sanitized to: '\(sanitizedHost)'."
27+
return (sanitizedHost, warning)
3828
} else if host.range(of: ipRegex, options: .regularExpression) != nil {
3929
// if a valid IP address is given, accept it
4030
return (host, nil)
41-
} else if host == "localhost" {
42-
// if "localhost" given, accept it
31+
} else if host.range(of: hostRegex, options: .regularExpression) != nil {
32+
// if a valid host name is given, accept it
4333
return (host, nil)
4434
} else {
4535
// otherwise, drop

DatadogInternal/Tests/NetworkInstrumentation/HostsSanitizerTests.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,30 @@ class HostsSanitizerTests: XCTestCase {
2121
"https://first-party.com/v2/api", // sanitize to → "first-party.com"
2222
"https://192.168.0.1/api", // sanitize to → "192.168.0.1"
2323
"https://192.168.0.2", // sanitize to → "192.168.0.2"
24-
"invalid-host-name", // drop
24+
"invalid_host_name", // drop
2525
"192.168.0.3:8080", // drop
2626
"", // drop
2727
"localhost", // accept
2828
"192.168.0.4", // accept
2929
"valid-host-name.com", // accept
30+
"customprotocol://name" // accept
3031
]
3132

3233
// Then
3334
let sanitizer = HostsSanitizer()
3435
let sanitizedHosts = sanitizer.sanitized(hosts: hosts, warningMessage: "Host is not valid")
3536

36-
XCTAssertEqual(
37-
sanitizedHosts,
38-
[
39-
"first-party.com",
40-
"api.first-party.com",
41-
"localhost",
42-
"192.168.0.1",
43-
"192.168.0.2",
44-
"localhost",
45-
"192.168.0.4",
46-
"valid-host-name.com"
47-
]
48-
)
37+
XCTAssertEqual(sanitizedHosts.count, 8)
38+
XCTAssertTrue(sanitizedHosts.contains("first-party.com"))
39+
XCTAssertTrue(sanitizedHosts.contains("api.first-party.com"))
40+
XCTAssertTrue(sanitizedHosts.contains("192.168.0.1"))
41+
XCTAssertTrue(sanitizedHosts.contains("192.168.0.2"))
42+
XCTAssertTrue(sanitizedHosts.contains("localhost"))
43+
XCTAssertTrue(sanitizedHosts.contains("192.168.0.4"))
44+
XCTAssertTrue(sanitizedHosts.contains("valid-host-name.com"))
45+
XCTAssertTrue(sanitizedHosts.contains("name"))
46+
47+
XCTAssertEqual(printFunction.printedMessages.count, 9)
4948

5049
XCTAssertTrue(
5150
printFunction.printedMessages.contains("⚠️ Host is not valid: '192.168.0.3:8080' is not a valid host name and will be dropped.")
@@ -66,11 +65,13 @@ class HostsSanitizerTests: XCTestCase {
6665
printFunction.printedMessages.contains("⚠️ Host is not valid: 'https://first-party.com/v2/api' is an url and will be sanitized to: 'first-party.com'.")
6766
)
6867
XCTAssertTrue(
69-
printFunction.printedMessages.contains("⚠️ Host is not valid: 'invalid-host-name' is not a valid host name and will be dropped.")
68+
printFunction.printedMessages.contains("⚠️ Host is not valid: 'invalid_host_name' is not a valid host name and will be dropped.")
7069
)
7170
XCTAssertTrue(
7271
printFunction.printedMessages.contains("⚠️ Host is not valid: 'https://192.168.0.2' is an url and will be sanitized to: '192.168.0.2'.")
7372
)
74-
XCTAssertEqual(printFunction.printedMessages.count, 8)
73+
XCTAssertTrue(
74+
printFunction.printedMessages.contains("⚠️ Host is not valid: 'customprotocol://name' is an url and will be sanitized to: 'name'.")
75+
)
7576
}
7677
}

0 commit comments

Comments
 (0)