Skip to content

Commit bee6865

Browse files
committed
Improve checking urls for URLProtocol usage
1 parent 2e41c63 commit bee6865

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Sources/InstanaAgent/Monitors/HTTP/InstanaURLProtocol.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ class InstanaURLProtocol: URLProtocol {
3737

3838
override class func canInit(with request: URLRequest) -> Bool {
3939
guard mode == .enabled else { return false }
40-
guard let url = request.url, let scheme = url.scheme, !IgnoreURLHandler.shouldIgnore(url) else { return false }
41-
return ["http", "https"].contains(scheme)
40+
guard request.url?.host != nil else { return false }
41+
guard let url = request.url, let scheme = url.scheme else { return false }
42+
guard ["http", "https"].contains(scheme) else { return false }
43+
guard !IgnoreURLHandler.shouldIgnore(url) else { return false }
44+
return true
4245
}
4346

4447
private var canMark: Bool {

Tests/InstanaAgentTests/Monitors/HTTP/InstanaURLProtocolTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ class InstanaURLProtocolTests: InstanaTestCase {
2727
XCTAssertTrue(InstanaURLProtocol.canInit(with: makeRequest("http://www.a.c")))
2828
XCTAssertFalse(InstanaURLProtocol.canInit(with: makeRequest("www.a.c")))
2929
XCTAssertFalse(InstanaURLProtocol.canInit(with: makeRequest("ws://a")))
30+
XCTAssertFalse(InstanaURLProtocol.canInit(with: makeRequest("some:nohost")))
31+
}
32+
33+
func test_urlProtocol_shouldNotInitForBase64() {
34+
// Given
35+
let base64 = "data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAwBQTFRF7c5J78kt+/Xm78lQ6stH5LI36bQh6rcf7sQp67="
36+
let request = makeRequest(base64)
37+
38+
// Then
39+
XCTAssertFalse(InstanaURLProtocol.canInit(with:request))
40+
XCTAssertEqual(request.url?.absoluteString, base64)
3041
}
3142

3243
func test_urlProtocol_shouldNotInitForIgnoredURL() {

0 commit comments

Comments
 (0)