Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fixed Policies to assert CRL, invalid host, SHA1, and a bunch of othe…
Browse files Browse the repository at this point in the history
…r security flaws in the Trust validation.
  • Loading branch information
Brandon-T committed Nov 19, 2019
1 parent a1fadbd commit 221cafe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,13 @@ class BrowserViewController: UIViewController {
guard let serverTrust = tab.webView?.serverTrust else {
break
}


let policies = [
SecPolicyCreateBasicX509(),
SecPolicyCreateSSL(true, tab.webView?.url?.host as CFString?)
]

SecTrustSetPolicies(serverTrust, policies as CFTypeRef)
SecTrustEvaluateAsync(serverTrust, DispatchQueue.global()) { _, secTrustResult in
switch secTrustResult {
case .proceed, .unspecified:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation
import WebKit
import Shared

class OnboardingWebViewController: UIViewController {
class OnboardingWebViewController: UIViewController, WKNavigationDelegate {

private let url = URL(string: "https://brave.com/terms-of-use/")

Expand Down Expand Up @@ -58,6 +58,7 @@ class OnboardingWebViewController: UIViewController {

KVOs.forEach { webView.addObserver(self, forKeyPath: $0.rawValue, options: .new, context: nil) }

webView.navigationDelegate = self
webView.load(URLRequest(url: url!))

toolbar.exitButton.addTarget(self, action: #selector(onExit), for: .touchUpInside)
Expand Down Expand Up @@ -111,10 +112,14 @@ class OnboardingWebViewController: UIViewController {
if let trust = webView.serverTrust {
toolbar.secureIcon.isHidden = false

let x509 = SecPolicyCreateBasicX509()
let sslPolicy = SecPolicyCreateSSL(true, (webView.url?.host ?? "") as CFString)
SecTrustSetPolicies(trust, [x509, sslPolicy] as CFTypeRef)

var result: SecTrustResultType = .invalid
SecTrustEvaluate(trust, &result)

if result == .proceed || result == .unspecified {
if (result == .proceed || result == .unspecified) && webView.hasOnlySecureContent {
toolbar.secureIcon.tintColor = UX.secureWebPageColor
toolbar.urlLabel.textColor = UX.secureWebPageColor
} else {
Expand All @@ -134,6 +139,15 @@ class OnboardingWebViewController: UIViewController {
toolbar.forwardButton.isEnabled = webView.canGoForward
toolbar.forwardButton.tintColor = webView.canGoForward ? UX.buttonEnabledColor : UX.buttonDisabledColor
}

func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

if let trust = challenge.protectionSpace.serverTrust {
return completionHandler(.useCredential, URLCredential(trust: trust))
}

return completionHandler(.performDefaultHandling, nil)
}
}

extension OnboardingWebViewController {
Expand Down

0 comments on commit 221cafe

Please sign in to comment.