Skip to content

Commit 71ab49a

Browse files
committed
Adding non animated dismiss to handle TabTray blocking external alert presantation
1 parent 694aa3f commit 71ab49a

File tree

6 files changed

+61
-63
lines changed

6 files changed

+61
-63
lines changed

ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+ShareActivity.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,10 @@ extension BrowserViewController {
301301
}
302302

303303
// Display Certificate Activity
304-
if let tabURL = tabManager.selectedTab?.webView?.url, tabManager.selectedTab?.webView?.serverTrust != nil || ErrorPageHelper.hasCertificates(for: tabURL) {
304+
if let tabURL = tabManager.selectedTab?.webView?.url,
305+
tabManager.selectedTab?.webView?.serverTrust != nil
306+
|| ErrorPageHelper.hasCertificates(for: tabURL)
307+
{
305308
if let selectedTab = tabManager.selectedTab {
306309
logSecureContentState(tab: selectedTab, details: "Display Certificate Activity Settings")
307310
}
@@ -316,7 +319,7 @@ extension BrowserViewController {
316319
)
317320
}
318321

319-
// Report Web-compat Issue Actibity
322+
// Report Web-compat Issue Activity
320323
activities.append(
321324
BasicMenuActivity(
322325
title: Strings.Shields.reportABrokenSite,

ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ extension BrowserViewController: WKNavigationDelegate {
6767

6868
// check if web view is loading a different origin than the one currently loaded
6969
if let selectedTab = tabManager.selectedTab,
70-
selectedTab.url?.origin != webView.url?.origin {
71-
70+
selectedTab.url?.origin != webView.url?.origin
71+
{
72+
7273
// new site has a different origin, hide wallet icon.
7374
tabManager.selectedTab?.isWalletIconVisible = false
7475
// new site, reset connected addresses
@@ -730,20 +731,25 @@ extension BrowserViewController: WKNavigationDelegate {
730731
download.delegate = self
731732
}
732733

733-
734734
@MainActor
735-
public func webView(_ webView: WKWebView, respondTo challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
735+
736+
public func webView(
737+
_ webView: WKWebView,
738+
respondTo challenge: URLAuthenticationChallenge
739+
) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
740+
736741
// If this is a certificate challenge, see if the certificate has previously been
737742
// accepted by the user.
738743
let host = challenge.protectionSpace.host
739744
let origin = "\(host):\(challenge.protectionSpace.port)"
740745
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
741-
let trust = challenge.protectionSpace.serverTrust {
742-
746+
let trust = challenge.protectionSpace.serverTrust
747+
{
748+
743749
let cert = await Task<SecCertificate?, Never>.detached {
744750
return (SecTrustCopyCertificateChain(trust) as? [SecCertificate])?.first
745751
}.value
746-
752+
747753
if let cert = cert, profile.certStore.containsCertificate(cert, forOrigin: origin) {
748754
return (.useCredential, URLCredential(trust: trust))
749755
}
@@ -777,19 +783,25 @@ extension BrowserViewController: WKNavigationDelegate {
777783
Logger.module.error("CERTIFICATE_INVALID")
778784
let errorCode = CFNetworkErrors.braveCertificatePinningFailed.rawValue
779785

780-
let underlyingError = NSError(domain: kCFErrorDomainCFNetwork as String,
781-
code: Int(errorCode),
782-
userInfo: ["_kCFStreamErrorCodeKey": Int(errorCode)])
783-
784-
let error = NSError(domain: kCFErrorDomainCFNetwork as String,
785-
code: Int(errorCode),
786-
userInfo: [NSURLErrorFailingURLErrorKey: webView.url as Any,
787-
"NSErrorPeerCertificateChainKey": certificateChain,
788-
NSUnderlyingErrorKey: underlyingError])
786+
let underlyingError = NSError(
787+
domain: kCFErrorDomainCFNetwork as String,
788+
code: Int(errorCode),
789+
userInfo: ["_kCFStreamErrorCodeKey": Int(errorCode)]
790+
)
791+
792+
let error = NSError(
793+
domain: kCFErrorDomainCFNetwork as String,
794+
code: Int(errorCode),
795+
userInfo: [
796+
NSURLErrorFailingURLErrorKey: webView.url as Any,
797+
"NSErrorPeerCertificateChainKey": certificateChain,
798+
NSUnderlyingErrorKey: underlyingError,
799+
]
800+
)
789801

790802
// Handle the error later in `didFailProvisionalNavigation`
791803
self.tab(for: webView)?.sslPinningError = error
792-
804+
793805
return (.cancelAuthenticationChallenge, nil)
794806
}
795807
}
@@ -799,14 +811,15 @@ extension BrowserViewController: WKNavigationDelegate {
799811
let credential = challenge.proposedCredential
800812
let previousFailureCount = challenge.previousFailureCount
801813

802-
guard protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic ||
803-
protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest ||
804-
protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM,
805-
let tab = tab(for: webView)
814+
guard
815+
protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic
816+
|| protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest
817+
|| protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM,
818+
let tab = tab(for: webView)
806819
else {
807820
return (.performDefaultHandling, nil)
808821
}
809-
822+
810823
// The challenge may come from a background tab, so ensure it's the one visible.
811824
tabManager.selectTab(tab)
812825

@@ -817,11 +830,14 @@ extension BrowserViewController: WKNavigationDelegate {
817830
protectionSpace: protectionSpace,
818831
previousFailureCount: previousFailureCount
819832
)
820-
833+
821834
if BasicAuthCredentialsManager.validDomains.contains(host) {
822-
BasicAuthCredentialsManager.setCredential(origin: origin, credential: credentials.credentials)
835+
BasicAuthCredentialsManager.setCredential(
836+
origin: origin,
837+
credential: credentials.credentials
838+
)
823839
}
824-
840+
825841
return (.useCredential, credentials.credentials)
826842
} catch {
827843
return (.rejectProtectionSpace, nil)
@@ -993,7 +1009,7 @@ extension BrowserViewController: WKNavigationDelegate {
9931009
if tab == self.tabManager.selectedTab {
9941010
self.topToolbar.hideProgressBar()
9951011
}
996-
1012+
9971013
// If the local web server isn't working for some reason (Brave cellular data is
9981014
// disabled in settings, for example), we'll fail to load the session restore URL.
9991015
// We rely on loading that page to get the restore callback to reset the restoring
@@ -1059,16 +1075,7 @@ extension BrowserViewController {
10591075
// External dialog should not be shown for non-active tabs #6687 - #7835
10601076
let isVisibleTab = tab?.isTabVisible() == true
10611077

1062-
// Check user trying to open on NTP like external link browsing
1063-
var isAboutHome = false
1064-
if let url = tab?.url {
1065-
isAboutHome = InternalURL(url)?.isAboutHomeURL == true
1066-
}
1067-
1068-
// Finally check non-active tab
1069-
let isNonActiveTab = isAboutHome ? false : tab?.url?.host != topToolbar.currentURL?.host
1070-
1071-
if !isVisibleTab || isNonActiveTab {
1078+
if !isVisibleTab {
10721079
return false
10731080
}
10741081

ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,8 @@ public class BrowserViewController: UIViewController {
20722072
logSecureContentState(tab: tab, path: path)
20732073

20742074
guard let url = webView.url,
2075-
let serverTrust = webView.serverTrust else {
2075+
let serverTrust = webView.serverTrust
2076+
else {
20762077
if let url = webView.url {
20772078
if InternalURL.isValid(url: url),
20782079
let internalUrl = InternalURL(url),
@@ -2166,9 +2167,10 @@ public class BrowserViewController: UIViewController {
21662167
}
21672168
break
21682169
}
2169-
2170+
21702171
guard let scheme = url.scheme,
2171-
let host = url.host else {
2172+
let host = url.host
2173+
else {
21722174
tab.secureContentState = .unknown
21732175
logSecureContentState(tab: tab, path: path, details: "No webview URL host scheme)")
21742176

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

2318-
public func switchToTabForURLOrOpen(
2319-
_ url: URL,
2320-
isPrivate: Bool = false,
2321-
isPrivileged: Bool,
2322-
isExternal: Bool = false
2323-
) {
2324-
if !isExternal {
2325-
popToBVC()
2326-
}
2320+
public func switchToTabForURLOrOpen(_ url: URL, isPrivate: Bool = false, isPrivileged: Bool) {
2321+
popToBVC(isAnimated: false)
23272322

23282323
if let tab = tabManager.getTabForURL(url, isPrivate: isPrivate) {
23292324
tabManager.selectTab(tab)
@@ -2461,11 +2456,11 @@ public class BrowserViewController: UIViewController {
24612456
present(settingsNavigationController, animated: true)
24622457
}
24632458

2464-
func popToBVC(completion: (() -> Void)? = nil) {
2459+
func popToBVC(isAnimated: Bool = true, completion: (() -> Void)? = nil) {
24652460
guard let currentViewController = navigationController?.topViewController else {
24662461
return
24672462
}
2468-
currentViewController.dismiss(animated: true, completion: completion)
2463+
currentViewController.dismiss(animated: isAnimated, completion: completion)
24692464

24702465
if currentViewController != self {
24712466
_ = self.navigationController?.popViewController(animated: true)

ios/brave-ios/Sources/Brave/Frontend/Browser/Helpers/ErrorPageHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ extension ErrorPageHelper {
184184
static func hasCertificates(for url: URL) -> Bool {
185185
return (url as NSURL).valueForQueryParameter(key: "badcerts") != nil
186186
}
187-
187+
188188
static func serverTrust(from errorURL: URL) throws -> SecTrust? {
189189
guard let internalUrl = InternalURL(errorURL),
190190
internalUrl.isErrorPage,

ios/brave-ios/Sources/Brave/Frontend/Browser/NavigationRouter.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ public enum NavigationPath: Equatable {
9191

9292
private static func handleURL(url: URL?, isPrivate: Bool, with bvc: BrowserViewController) {
9393
if let newURL = url {
94-
bvc.switchToTabForURLOrOpen(
95-
newURL,
96-
isPrivate: isPrivate,
97-
isPrivileged: false,
98-
isExternal: true
99-
)
100-
bvc.popToBVC()
94+
bvc.switchToTabForURLOrOpen(newURL, isPrivate: isPrivate, isPrivileged: false)
10195
} else {
10296
bvc.openBlankNewTab(attemptLocationFieldFocus: false, isPrivate: isPrivate)
10397
}

ios/brave-ios/Sources/CertificateUtilities/BraveCertificateUtils.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ extension BraveCertificateUtils {
221221
}
222222

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

245245
/// Verifies ServerTrust using Brave-Core which verifies only SSL Pinning Status
246-
static func verifyTrust(_ trust: SecTrust, host: String, port: Int) async -> Int {
247-
246+
public static func verifyTrust(_ trust: SecTrust, host: String, port: Int) async -> Int {
248247
return Int(BraveCertificateUtility.verifyTrust(trust, host: host, port: port))
249248
}
250249
}

0 commit comments

Comments
 (0)