Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malware protection 5: Refactor Special Error Types #3642

Merged
merged 7 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11251,7 +11251,7 @@
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = revision;
revision = ba5ac555da0fcaa5bf8de69490a5dc65770e9178;
revision = 22f3d8f4497d33cbb3fb0920f55bc05a8f655671;
};
};
9F8FE9472BAE50E50071E372 /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "ba5ac555da0fcaa5bf8de69490a5dc65770e9178"
"revision" : "22f3d8f4497d33cbb3fb0920f55bc05a8f655671"
}
},
{
Expand Down
22 changes: 9 additions & 13 deletions DuckDuckGo/TabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1705,20 +1705,16 @@ extension TabViewController: WKNavigationDelegate {

private func loadSpecialErrorPageIfNeeded(error: NSError) {
guard featureFlagger.isFeatureOn(.sslCertificatesBypass),
error.code == NSURLErrorServerCertificateUntrusted,
let errorCode = error.userInfo["_kCFStreamErrorCodeKey"] as? Int32,
let failedURL = error.failedUrl else {
return
}
error.isServerCertificateUntrusted,
let errorType = error.sslErrorType,
let failedURL = error.failedUrl,
let host = failedURL.host else { return }

let tld = storageCache.tld
let errorType = SSLErrorType.forErrorCode(Int(errorCode))
self.failedURL = failedURL
errorData = SpecialErrorData(kind: .ssl,
errorType: errorType.rawValue,
domain: failedURL.host,
eTldPlus1: tld.eTLDplus1(failedURL.host))
errorData = SpecialErrorData.ssl(type: errorType, domain: host, eTldPlus1: tld.eTLDplus1(host))
loadSpecialErrorPage(url: failedURL)
Pixel.fire(pixel: .certificateWarningDisplayed(errorType.rawParameter))
Pixel.fire(pixel: .certificateWarningDisplayed(errorType.pixelParameter))
}

private func loadSpecialErrorPage(url: URL) {
Expand Down Expand Up @@ -3138,7 +3134,7 @@ extension UserContentController {

extension TabViewController: SpecialErrorPageUserScriptDelegate {

func leaveSite() {
func leaveSiteAction() {
Pixel.fire(pixel: .certificateWarningLeaveClicked)
guard webView?.canGoBack == true else {
delegate?.tabDidRequestClose(self)
Expand All @@ -3147,7 +3143,7 @@ extension TabViewController: SpecialErrorPageUserScriptDelegate {
_ = webView?.goBack()
}

func visitSite() {
func visitSiteAction() {
Pixel.fire(pixel: .certificateWarningProceedClicked)
isSpecialErrorPageVisible = false
shouldBypassSSLError = true
Expand Down
30 changes: 9 additions & 21 deletions DuckDuckGoTests/SpecialErrorPageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final class SpecialErrorPageTests: XCTestCase {

func testWhenCertificateExpiredThenExpectedErrorPageIsShown() {
// GIVEN
let error = NSError(domain: "test",
let error = NSError(domain: NSURLErrorDomain,
code: NSURLErrorServerCertificateUntrusted,
userInfo: ["_kCFStreamErrorCodeKey": errSSLCertExpired,
NSURLErrorFailingURLErrorKey: URL(string: "https://expired.badssl.com")!])
Expand All @@ -91,18 +91,15 @@ final class SpecialErrorPageTests: XCTestCase {

// THEN
XCTAssertEqual(sut.failedURL, URL(string: "https://expired.badssl.com")!)
XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl,
errorType: "expired",
domain: "expired.badssl.com",
eTldPlus1: "badssl.com"))
XCTAssertEqual(sut.errorData, SpecialErrorData.ssl(type: .expired, domain: "expired.badssl.com", eTldPlus1: "badssl.com"))
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error, "Expectation was not fulfilled in time")
}
}

func testWhenCertificateWrongHostThenExpectedErrorPageIsShown() {
// GIVEN
let error = NSError(domain: "test",
let error = NSError(domain: NSURLErrorDomain,
code: NSURLErrorServerCertificateUntrusted,
userInfo: ["_kCFStreamErrorCodeKey": errSSLHostNameMismatch,
NSURLErrorFailingURLErrorKey: URL(string: "https://wrong.host.badssl.com")!])
Expand All @@ -123,18 +120,15 @@ final class SpecialErrorPageTests: XCTestCase {

// THEN
XCTAssertEqual(sut.failedURL, URL(string: "https://wrong.host.badssl.com")!)
XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl,
errorType: "wrongHost",
domain: "wrong.host.badssl.com",
eTldPlus1: "badssl.com"))
XCTAssertEqual(sut.errorData, SpecialErrorData.ssl(type: .wrongHost, domain: "wrong.host.badssl.com", eTldPlus1: "badssl.com"))
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error, "Expectation was not fulfilled in time")
}
}

func testWhenCertificateSelfSignedThenExpectedErrorPageIsShown() {
// GIVEN
let error = NSError(domain: "test",
let error = NSError(domain: NSURLErrorDomain,
code: NSURLErrorServerCertificateUntrusted,
userInfo: ["_kCFStreamErrorCodeKey": errSSLXCertChainInvalid,
NSURLErrorFailingURLErrorKey: URL(string: "https://self-signed.badssl.com")!])
Expand All @@ -155,18 +149,15 @@ final class SpecialErrorPageTests: XCTestCase {

// THEN
XCTAssertEqual(sut.failedURL, URL(string: "https://self-signed.badssl.com")!)
XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl,
errorType: "selfSigned",
domain: "self-signed.badssl.com",
eTldPlus1: "badssl.com"))
XCTAssertEqual(sut.errorData, SpecialErrorData.ssl(type: .selfSigned, domain: "self-signed.badssl.com", eTldPlus1: "badssl.com"))
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error, "Expectation was not fulfilled in time")
}
}

func testWhenOtherCertificateIssueThenExpectedErrorPageIsShown() {
// GIVEN
let error = NSError(domain: "test",
let error = NSError(domain: NSURLErrorDomain,
code: NSURLErrorServerCertificateUntrusted,
userInfo: ["_kCFStreamErrorCodeKey": errSSLUnknownRootCert,
NSURLErrorFailingURLErrorKey: URL(string: "https://untrusted-root.badssl.com")!])
Expand All @@ -187,10 +178,7 @@ final class SpecialErrorPageTests: XCTestCase {

// THEN
XCTAssertEqual(sut.failedURL, URL(string: "https://untrusted-root.badssl.com")!)
XCTAssertEqual(sut.errorData, SpecialErrorData(kind: .ssl,
errorType: "invalid",
domain: "untrusted-root.badssl.com",
eTldPlus1: "badssl.com"))
XCTAssertEqual(sut.errorData, SpecialErrorData.ssl(type: .invalid, domain: "untrusted-root.badssl.com", eTldPlus1: "badssl.com"))
waitForExpectations(timeout: 1) { error in
XCTAssertNil(error, "Expectation was not fulfilled in time")
}
Expand Down Expand Up @@ -248,7 +236,7 @@ final class SpecialErrorPageTests: XCTestCase {
func testWhenDidReceiveChallengeIfChallengeForCertificateValidationAndUserRequestBypassThenReturnsCredentials() async {
let protectionSpace = URLProtectionSpace(host: "", port: 4, protocol: nil, realm: nil, authenticationMethod: NSURLAuthenticationMethodServerTrust)
let challenge = URLAuthenticationChallenge(protectionSpace: protectionSpace, proposedCredential: nil, previousFailureCount: 0, failureResponse: nil, error: nil, sender: ChallengeSender())
await sut.visitSite()
await sut.visitSiteAction()
await sut.webView(webView, didReceive: challenge) { _, credential in
XCTAssertNotNil(credential)
}
Expand Down
Loading