Skip to content

Commit

Permalink
Adding non animated dismiss to handle TabTray blocking external alert…
Browse files Browse the repository at this point in the history
… presantation
  • Loading branch information
soner-yuksel committed Feb 27, 2024
1 parent 694aa3f commit 71ab49a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ extension BrowserViewController {
}

// Display Certificate Activity
if let tabURL = tabManager.selectedTab?.webView?.url, tabManager.selectedTab?.webView?.serverTrust != nil || ErrorPageHelper.hasCertificates(for: tabURL) {
if let tabURL = tabManager.selectedTab?.webView?.url,
tabManager.selectedTab?.webView?.serverTrust != nil
|| ErrorPageHelper.hasCertificates(for: tabURL)
{
if let selectedTab = tabManager.selectedTab {
logSecureContentState(tab: selectedTab, details: "Display Certificate Activity Settings")
}
Expand All @@ -316,7 +319,7 @@ extension BrowserViewController {
)
}

// Report Web-compat Issue Actibity
// Report Web-compat Issue Activity
activities.append(
BasicMenuActivity(
title: Strings.Shields.reportABrokenSite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ extension BrowserViewController: WKNavigationDelegate {

// check if web view is loading a different origin than the one currently loaded
if let selectedTab = tabManager.selectedTab,
selectedTab.url?.origin != webView.url?.origin {

selectedTab.url?.origin != webView.url?.origin
{

// new site has a different origin, hide wallet icon.
tabManager.selectedTab?.isWalletIconVisible = false
// new site, reset connected addresses
Expand Down Expand Up @@ -730,20 +731,25 @@ extension BrowserViewController: WKNavigationDelegate {
download.delegate = self
}


@MainActor
public func webView(_ webView: WKWebView, respondTo challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {

public func webView(
_ webView: WKWebView,
respondTo challenge: URLAuthenticationChallenge
) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {

// If this is a certificate challenge, see if the certificate has previously been
// accepted by the user.
let host = challenge.protectionSpace.host
let origin = "\(host):\(challenge.protectionSpace.port)"
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let trust = challenge.protectionSpace.serverTrust {

let trust = challenge.protectionSpace.serverTrust
{

let cert = await Task<SecCertificate?, Never>.detached {
return (SecTrustCopyCertificateChain(trust) as? [SecCertificate])?.first
}.value

if let cert = cert, profile.certStore.containsCertificate(cert, forOrigin: origin) {
return (.useCredential, URLCredential(trust: trust))
}
Expand Down Expand Up @@ -777,19 +783,25 @@ extension BrowserViewController: WKNavigationDelegate {
Logger.module.error("CERTIFICATE_INVALID")
let errorCode = CFNetworkErrors.braveCertificatePinningFailed.rawValue

let underlyingError = NSError(domain: kCFErrorDomainCFNetwork as String,
code: Int(errorCode),
userInfo: ["_kCFStreamErrorCodeKey": Int(errorCode)])

let error = NSError(domain: kCFErrorDomainCFNetwork as String,
code: Int(errorCode),
userInfo: [NSURLErrorFailingURLErrorKey: webView.url as Any,
"NSErrorPeerCertificateChainKey": certificateChain,
NSUnderlyingErrorKey: underlyingError])
let underlyingError = NSError(
domain: kCFErrorDomainCFNetwork as String,
code: Int(errorCode),
userInfo: ["_kCFStreamErrorCodeKey": Int(errorCode)]
)

let error = NSError(
domain: kCFErrorDomainCFNetwork as String,
code: Int(errorCode),
userInfo: [
NSURLErrorFailingURLErrorKey: webView.url as Any,
"NSErrorPeerCertificateChainKey": certificateChain,
NSUnderlyingErrorKey: underlyingError,
]
)

// Handle the error later in `didFailProvisionalNavigation`
self.tab(for: webView)?.sslPinningError = error

return (.cancelAuthenticationChallenge, nil)
}
}
Expand All @@ -799,14 +811,15 @@ extension BrowserViewController: WKNavigationDelegate {
let credential = challenge.proposedCredential
let previousFailureCount = challenge.previousFailureCount

guard protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic ||
protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest ||
protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM,
let tab = tab(for: webView)
guard
protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic
|| protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest
|| protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM,
let tab = tab(for: webView)
else {
return (.performDefaultHandling, nil)
}

// The challenge may come from a background tab, so ensure it's the one visible.
tabManager.selectTab(tab)

Expand All @@ -817,11 +830,14 @@ extension BrowserViewController: WKNavigationDelegate {
protectionSpace: protectionSpace,
previousFailureCount: previousFailureCount
)

if BasicAuthCredentialsManager.validDomains.contains(host) {
BasicAuthCredentialsManager.setCredential(origin: origin, credential: credentials.credentials)
BasicAuthCredentialsManager.setCredential(
origin: origin,
credential: credentials.credentials
)
}

return (.useCredential, credentials.credentials)
} catch {
return (.rejectProtectionSpace, nil)
Expand Down Expand Up @@ -993,7 +1009,7 @@ extension BrowserViewController: WKNavigationDelegate {
if tab == self.tabManager.selectedTab {
self.topToolbar.hideProgressBar()
}

// If the local web server isn't working for some reason (Brave cellular data is
// disabled in settings, for example), we'll fail to load the session restore URL.
// We rely on loading that page to get the restore callback to reset the restoring
Expand Down Expand Up @@ -1059,16 +1075,7 @@ extension BrowserViewController {
// External dialog should not be shown for non-active tabs #6687 - #7835
let isVisibleTab = tab?.isTabVisible() == true

// Check user trying to open on NTP like external link browsing
var isAboutHome = false
if let url = tab?.url {
isAboutHome = InternalURL(url)?.isAboutHomeURL == true
}

// Finally check non-active tab
let isNonActiveTab = isAboutHome ? false : tab?.url?.host != topToolbar.currentURL?.host

if !isVisibleTab || isNonActiveTab {
if !isVisibleTab {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,8 @@ public class BrowserViewController: UIViewController {
logSecureContentState(tab: tab, path: path)

guard let url = webView.url,
let serverTrust = webView.serverTrust else {
let serverTrust = webView.serverTrust
else {
if let url = webView.url {
if InternalURL.isValid(url: url),
let internalUrl = InternalURL(url),
Expand Down Expand Up @@ -2166,9 +2167,10 @@ public class BrowserViewController: UIViewController {
}
break
}

guard let scheme = url.scheme,
let host = url.host else {
let host = url.host
else {
tab.secureContentState = .unknown
logSecureContentState(tab: tab, path: path, details: "No webview URL host scheme)")

Expand Down Expand Up @@ -2315,15 +2317,8 @@ public class BrowserViewController: UIViewController {
browser.tabManager.addTabsForURLs([url], zombie: false, isPrivate: isPrivate)
}

public func switchToTabForURLOrOpen(
_ url: URL,
isPrivate: Bool = false,
isPrivileged: Bool,
isExternal: Bool = false
) {
if !isExternal {
popToBVC()
}
public func switchToTabForURLOrOpen(_ url: URL, isPrivate: Bool = false, isPrivileged: Bool) {
popToBVC(isAnimated: false)

if let tab = tabManager.getTabForURL(url, isPrivate: isPrivate) {
tabManager.selectTab(tab)
Expand Down Expand Up @@ -2461,11 +2456,11 @@ public class BrowserViewController: UIViewController {
present(settingsNavigationController, animated: true)
}

func popToBVC(completion: (() -> Void)? = nil) {
func popToBVC(isAnimated: Bool = true, completion: (() -> Void)? = nil) {
guard let currentViewController = navigationController?.topViewController else {
return
}
currentViewController.dismiss(animated: true, completion: completion)
currentViewController.dismiss(animated: isAnimated, completion: completion)

if currentViewController != self {
_ = self.navigationController?.popViewController(animated: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ extension ErrorPageHelper {
static func hasCertificates(for url: URL) -> Bool {
return (url as NSURL).valueForQueryParameter(key: "badcerts") != nil
}

static func serverTrust(from errorURL: URL) throws -> SecTrust? {
guard let internalUrl = InternalURL(errorURL),
internalUrl.isErrorPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,7 @@ public enum NavigationPath: Equatable {

private static func handleURL(url: URL?, isPrivate: Bool, with bvc: BrowserViewController) {
if let newURL = url {
bvc.switchToTabForURLOrOpen(
newURL,
isPrivate: isPrivate,
isPrivileged: false,
isExternal: true
)
bvc.popToBVC()
bvc.switchToTabForURLOrOpen(newURL, isPrivate: isPrivate, isPrivileged: false)
} else {
bvc.openBlankNewTab(attemptLocationFieldFocus: false, isPrivate: isPrivate)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ extension BraveCertificateUtils {
}

/// Verifies ServerTrust using Apple's APIs which validates also the X509 Certificate against the System Trusts
static func evaluateTrust(_ trust: SecTrust, for host: String?) async throws {
public static func evaluateTrust(_ trust: SecTrust, for host: String?) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
BraveCertificateUtils.evaluationQueue.async {
SecTrustEvaluateAsyncWithError(trust, BraveCertificateUtils.evaluationQueue) {
Expand All @@ -243,8 +243,7 @@ extension BraveCertificateUtils {
}

/// Verifies ServerTrust using Brave-Core which verifies only SSL Pinning Status
static func verifyTrust(_ trust: SecTrust, host: String, port: Int) async -> Int {

public static func verifyTrust(_ trust: SecTrust, host: String, port: Int) async -> Int {
return Int(BraveCertificateUtility.verifyTrust(trust, host: host, port: port))
}
}

0 comments on commit 71ab49a

Please sign in to comment.