@@ -69,21 +69,6 @@ extension BrowserViewController: WKNavigationDelegate {
69
69
if let selectedTab = tabManager. selectedTab,
70
70
selectedTab. url? . origin != webView. url? . origin
71
71
{
72
- if let url = webView. url {
73
- if !InternalURL. isValid ( url: url) {
74
- // reset secure content state to unknown until page can be evaluated
75
- selectedTab. sslPinningError = nil
76
- selectedTab. sslPinningTrust = nil
77
- selectedTab. secureContentState = . unknown
78
- logSecureContentState (
79
- tab: selectedTab,
80
- details:
81
- " DidStartProvisionalNavigation - Reset secure content state to unknown until page can be evaluated "
82
- )
83
-
84
- updateToolbarSecureContentState ( . unknown)
85
- }
86
- }
87
72
88
73
// new site has a different origin, hide wallet icon.
89
74
tabManager. selectedTab? . isWalletIconVisible = false
@@ -746,7 +731,9 @@ extension BrowserViewController: WKNavigationDelegate {
746
731
download. delegate = self
747
732
}
748
733
749
- nonisolated public func webView(
734
+ @MainActor
735
+
736
+ public func webView(
750
737
_ webView: WKWebView ,
751
738
respondTo challenge: URLAuthenticationChallenge
752
739
) async -> ( URLSession . AuthChallengeDisposition , URLCredential ? ) {
@@ -756,11 +743,16 @@ extension BrowserViewController: WKNavigationDelegate {
756
743
let host = challenge. protectionSpace. host
757
744
let origin = " \( host) : \( challenge. protectionSpace. port) "
758
745
if challenge. protectionSpace. authenticationMethod == NSURLAuthenticationMethodServerTrust,
759
- let trust = challenge. protectionSpace. serverTrust,
760
- let cert = ( SecTrustCopyCertificateChain ( trust) as? [ SecCertificate ] ) ? . first,
761
- profile. certStore. containsCertificate ( cert, forOrigin: origin)
746
+ let trust = challenge. protectionSpace. serverTrust
762
747
{
763
- return ( . useCredential, URLCredential ( trust: trust) )
748
+
749
+ let cert = await Task < SecCertificate ? , Never > . detached {
750
+ return ( SecTrustCopyCertificateChain ( trust) as? [ SecCertificate ] ) ? . first
751
+ } . value
752
+
753
+ if let cert = cert, profile. certStore. containsCertificate ( cert, forOrigin: origin) {
754
+ return ( . useCredential, URLCredential ( trust: trust) )
755
+ }
764
756
}
765
757
766
758
// Certificate Pinning
@@ -782,13 +774,8 @@ extension BrowserViewController: WKNavigationDelegate {
782
774
if result == Int32 . min {
783
775
// Cert is POTENTIALLY invalid and cannot be pinned
784
776
785
- await MainActor . run {
786
- // Handle the potential error later in `didFailProvisionalNavigation`
787
- self . tab ( for: webView) ? . sslPinningTrust = serverTrust
788
- }
789
-
790
777
// Let WebKit handle the request and validate the cert
791
- // This is the same as calling `BraveCertificateUtils.evaluateTrust`
778
+ // This is the same as calling `BraveCertificateUtils.evaluateTrust` but with more error info provided by WebKit
792
779
return ( . performDefaultHandling, nil )
793
780
}
794
781
@@ -802,7 +789,7 @@ extension BrowserViewController: WKNavigationDelegate {
802
789
userInfo: [ " _kCFStreamErrorCodeKey " : Int ( errorCode) ]
803
790
)
804
791
805
- let error = await NSError (
792
+ let error = NSError (
806
793
domain: kCFErrorDomainCFNetwork as String ,
807
794
code: Int ( errorCode) ,
808
795
userInfo: [
@@ -812,10 +799,8 @@ extension BrowserViewController: WKNavigationDelegate {
812
799
]
813
800
)
814
801
815
- await MainActor . run {
816
- // Handle the error later in `didFailProvisionalNavigation`
817
- self . tab ( for: webView) ? . sslPinningError = error
818
- }
802
+ // Handle the error later in `didFailProvisionalNavigation`
803
+ self . tab ( for: webView) ? . sslPinningError = error
819
804
820
805
return ( . cancelAuthenticationChallenge, nil )
821
806
}
@@ -825,39 +810,38 @@ extension BrowserViewController: WKNavigationDelegate {
825
810
let protectionSpace = challenge. protectionSpace
826
811
let credential = challenge. proposedCredential
827
812
let previousFailureCount = challenge. previousFailureCount
828
- return await Task { @MainActor in
829
- guard
830
- protectionSpace. authenticationMethod == NSURLAuthenticationMethodHTTPBasic
831
- || protectionSpace. authenticationMethod == NSURLAuthenticationMethodHTTPDigest
832
- || protectionSpace. authenticationMethod == NSURLAuthenticationMethodNTLM,
833
- let tab = tab ( for: webView)
834
- else {
835
- return ( . performDefaultHandling, nil )
836
- }
837
813
838
- // The challenge may come from a background tab, so ensure it's the one visible.
839
- tabManager. selectTab ( tab)
814
+ guard
815
+ protectionSpace. authenticationMethod == NSURLAuthenticationMethodHTTPBasic
816
+ || protectionSpace. authenticationMethod == NSURLAuthenticationMethodHTTPDigest
817
+ || protectionSpace. authenticationMethod == NSURLAuthenticationMethodNTLM,
818
+ let tab = tab ( for: webView)
819
+ else {
820
+ return ( . performDefaultHandling, nil )
821
+ }
840
822
841
- do {
842
- let credentials = try await Authenticator . handleAuthRequest (
843
- self ,
844
- credential: credential,
845
- protectionSpace: protectionSpace,
846
- previousFailureCount: previousFailureCount
847
- )
823
+ // The challenge may come from a background tab, so ensure it's the one visible.
824
+ tabManager. selectTab ( tab)
848
825
849
- if BasicAuthCredentialsManager . validDomains. contains ( host) {
850
- BasicAuthCredentialsManager . setCredential (
851
- origin: origin,
852
- credential: credentials. credentials
853
- )
854
- }
826
+ do {
827
+ let credentials = try await Authenticator . handleAuthRequest (
828
+ self ,
829
+ credential: credential,
830
+ protectionSpace: protectionSpace,
831
+ previousFailureCount: previousFailureCount
832
+ )
855
833
856
- return ( . useCredential, credentials. credentials)
857
- } catch {
858
- return ( . rejectProtectionSpace, nil )
834
+ if BasicAuthCredentialsManager . validDomains. contains ( host) {
835
+ BasicAuthCredentialsManager . setCredential (
836
+ origin: origin,
837
+ credential: credentials. credentials
838
+ )
859
839
}
860
- } . value
840
+
841
+ return ( . useCredential, credentials. credentials)
842
+ } catch {
843
+ return ( . rejectProtectionSpace, nil )
844
+ }
861
845
}
862
846
863
847
public func webView( _ webView: WKWebView , didCommit navigation: WKNavigation ! ) {
@@ -991,20 +975,6 @@ extension BrowserViewController: WKNavigationDelegate {
991
975
) {
992
976
guard let tab = tab ( for: webView) else { return }
993
977
994
- // WebKit does not update certs on cancellation of a frame load
995
- // So manually trigger the notification with the current cert
996
- // Also, when Chromium cert validation passes, BUT Apple cert validation fails, the request is cancelled automatically by WebKit
997
- // In such a case, the webView.serverTrust is `nil`. The only time we have a valid trust is when we received the challenge
998
- // so we need to update the URL-Bar to show that serverTrust when WebKit's is nil.
999
- logSecureContentState ( tab: tab, details: " ObserveValue trigger in didFailProvisionalNavigation " )
1000
-
1001
- observeValue (
1002
- forKeyPath: KVOConstants . serverTrust. keyPath,
1003
- of: webView,
1004
- change: [ . newKey: webView. serverTrust ?? tab. sslPinningTrust as Any , . kindKey: 1 ] ,
1005
- context: nil
1006
- )
1007
-
1008
978
// Ignore the "Frame load interrupted" error that is triggered when we cancel a request
1009
979
// to open an external application and hand it over to UIApplication.openURL(). The result
1010
980
// will be that we switch to the external app, for example the app store, while keeping the
@@ -1036,23 +1006,10 @@ extension BrowserViewController: WKNavigationDelegate {
1036
1006
1037
1007
if let url = error. userInfo [ NSURLErrorFailingURLErrorKey] as? URL {
1038
1008
1039
- // The certificate came from the WebKit SSL Handshake validation and the cert is untrusted
1040
- if webView. serverTrust == nil , let serverTrust = tab. sslPinningTrust,
1041
- error. userInfo [ " NSErrorPeerCertificateChainKey " ] == nil
1042
- {
1043
- // Build a cert chain error to display in the cert viewer in such cases, as we aren't given one by WebKit
1044
- var userInfo = error. userInfo
1045
- userInfo [ " NSErrorPeerCertificateChainKey " ] =
1046
- SecTrustCopyCertificateChain ( serverTrust) as? [ SecCertificate ] ?? [ ]
1047
- userInfo [ " NSErrorPeerUntrustedByApple " ] = true
1048
- error = NSError ( domain: error. domain, code: error. code, userInfo: userInfo)
1009
+ if tab == self . tabManager. selectedTab {
1010
+ self . topToolbar. hideProgressBar ( )
1049
1011
}
1050
1012
1051
- ErrorPageHelper ( certStore: profile. certStore) . loadPage ( error, forUrl: url, inWebView: webView)
1052
- // Submitting same errornous URL using toolbar will cause progress bar get stuck
1053
- // Reseting the progress bar in case there is an error is necessary
1054
- topToolbar. hideProgressBar ( )
1055
-
1056
1013
// If the local web server isn't working for some reason (Brave cellular data is
1057
1014
// disabled in settings, for example), we'll fail to load the session restore URL.
1058
1015
// We rely on loading that page to get the restore callback to reset the restoring
@@ -1118,16 +1075,7 @@ extension BrowserViewController {
1118
1075
// External dialog should not be shown for non-active tabs #6687 - #7835
1119
1076
let isVisibleTab = tab? . isTabVisible ( ) == true
1120
1077
1121
- // Check user trying to open on NTP like external link browsing
1122
- var isAboutHome = false
1123
- if let url = tab? . url {
1124
- isAboutHome = InternalURL ( url) ? . isAboutHomeURL == true
1125
- }
1126
-
1127
- // Finally check non-active tab
1128
- let isNonActiveTab = isAboutHome ? false : tab? . url? . host != topToolbar. currentURL? . host
1129
-
1130
- if !isVisibleTab || isNonActiveTab {
1078
+ if !isVisibleTab {
1131
1079
return false
1132
1080
}
1133
1081
0 commit comments