@@ -67,24 +67,8 @@ extension BrowserViewController: WKNavigationDelegate {
67
67
68
68
// check if web view is loading a different origin than the one currently loaded
69
69
if let selectedTab = tabManager. selectedTab,
70
- selectedTab. url? . origin != webView. url? . origin
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
-
70
+ selectedTab. url? . origin != webView. url? . origin {
71
+
88
72
// new site has a different origin, hide wallet icon.
89
73
tabManager. selectedTab? . isWalletIconVisible = false
90
74
// new site, reset connected addresses
@@ -746,21 +730,23 @@ extension BrowserViewController: WKNavigationDelegate {
746
730
download. delegate = self
747
731
}
748
732
749
- nonisolated public func webView(
750
- _ webView: WKWebView ,
751
- respondTo challenge: URLAuthenticationChallenge
752
- ) async -> ( URLSession . AuthChallengeDisposition , URLCredential ? ) {
753
733
734
+ @MainActor
735
+ public func webView( _ webView: WKWebView , respondTo challenge: URLAuthenticationChallenge ) async -> ( URLSession . AuthChallengeDisposition , URLCredential ? ) {
754
736
// If this is a certificate challenge, see if the certificate has previously been
755
737
// accepted by the user.
756
738
let host = challenge. protectionSpace. host
757
739
let origin = " \( host) : \( challenge. protectionSpace. port) "
758
740
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)
762
- {
763
- return ( . useCredential, URLCredential ( trust: trust) )
741
+ let trust = challenge. protectionSpace. serverTrust {
742
+
743
+ let cert = await Task < SecCertificate ? , Never > . detached {
744
+ return ( SecTrustCopyCertificateChain ( trust) as? [ SecCertificate ] ) ? . first
745
+ } . value
746
+
747
+ if let cert = cert, profile. certStore. containsCertificate ( cert, forOrigin: origin) {
748
+ return ( . useCredential, URLCredential ( trust: trust) )
749
+ }
764
750
}
765
751
766
752
// Certificate Pinning
@@ -782,41 +768,28 @@ extension BrowserViewController: WKNavigationDelegate {
782
768
if result == Int32 . min {
783
769
// Cert is POTENTIALLY invalid and cannot be pinned
784
770
785
- await MainActor . run {
786
- // Handle the potential error later in `didFailProvisionalNavigation`
787
- self . tab ( for: webView) ? . sslPinningTrust = serverTrust
788
- }
789
-
790
771
// Let WebKit handle the request and validate the cert
791
- // This is the same as calling `BraveCertificateUtils.evaluateTrust`
772
+ // This is the same as calling `BraveCertificateUtils.evaluateTrust` but with more error info provided by WebKit
792
773
return ( . performDefaultHandling, nil )
793
774
}
794
775
795
776
// Cert is invalid and cannot be pinned
796
777
Logger . module. error ( " CERTIFICATE_INVALID " )
797
778
let errorCode = CFNetworkErrors . braveCertificatePinningFailed. rawValue
798
779
799
- let underlyingError = NSError (
800
- domain: kCFErrorDomainCFNetwork as String ,
801
- code: Int ( errorCode) ,
802
- userInfo: [ " _kCFStreamErrorCodeKey " : Int ( errorCode) ]
803
- )
804
-
805
- let error = await NSError (
806
- domain: kCFErrorDomainCFNetwork as String ,
807
- code: Int ( errorCode) ,
808
- userInfo: [
809
- NSURLErrorFailingURLErrorKey: webView. url as Any ,
810
- " NSErrorPeerCertificateChainKey " : certificateChain,
811
- NSUnderlyingErrorKey: underlyingError,
812
- ]
813
- )
814
-
815
- await MainActor . run {
816
- // Handle the error later in `didFailProvisionalNavigation`
817
- self . tab ( for: webView) ? . sslPinningError = error
818
- }
819
-
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] )
789
+
790
+ // Handle the error later in `didFailProvisionalNavigation`
791
+ self . tab ( for: webView) ? . sslPinningError = error
792
+
820
793
return ( . cancelAuthenticationChallenge, nil )
821
794
}
822
795
}
@@ -825,39 +798,34 @@ extension BrowserViewController: WKNavigationDelegate {
825
798
let protectionSpace = challenge. protectionSpace
826
799
let credential = challenge. proposedCredential
827
800
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
801
838
- // The challenge may come from a background tab, so ensure it's the one visible.
839
- tabManager . selectTab ( tab )
840
-
841
- do {
842
- let credentials = try await Authenticator . handleAuthRequest (
843
- self ,
844
- credential : credential ,
845
- protectionSpace : protectionSpace ,
846
- previousFailureCount : previousFailureCount
847
- )
848
-
849
- if BasicAuthCredentialsManager . validDomains . contains ( host ) {
850
- BasicAuthCredentialsManager . setCredential (
851
- origin : origin ,
852
- credential: credentials . credentials
853
- )
854
- }
855
-
856
- return ( . useCredential , credentials . credentials )
857
- } catch {
858
- return ( . rejectProtectionSpace , nil )
802
+ guard protectionSpace . authenticationMethod == NSURLAuthenticationMethodHTTPBasic ||
803
+ protectionSpace . authenticationMethod == NSURLAuthenticationMethodHTTPDigest ||
804
+ protectionSpace . authenticationMethod == NSURLAuthenticationMethodNTLM ,
805
+ let tab = tab ( for : webView )
806
+ else {
807
+ return ( . performDefaultHandling , nil )
808
+ }
809
+
810
+ // The challenge may come from a background tab, so ensure it's the one visible.
811
+ tabManager . selectTab ( tab )
812
+
813
+ do {
814
+ let credentials = try await Authenticator . handleAuthRequest (
815
+ self ,
816
+ credential: credential ,
817
+ protectionSpace : protectionSpace ,
818
+ previousFailureCount : previousFailureCount
819
+ )
820
+
821
+ if BasicAuthCredentialsManager . validDomains . contains ( host ) {
822
+ BasicAuthCredentialsManager . setCredential ( origin : origin , credential : credentials . credentials )
859
823
}
860
- } . value
824
+
825
+ return ( . useCredential, credentials. credentials)
826
+ } catch {
827
+ return ( . rejectProtectionSpace, nil )
828
+ }
861
829
}
862
830
863
831
public func webView( _ webView: WKWebView , didCommit navigation: WKNavigation ! ) {
@@ -991,20 +959,6 @@ extension BrowserViewController: WKNavigationDelegate {
991
959
) {
992
960
guard let tab = tab ( for: webView) else { return }
993
961
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
962
// Ignore the "Frame load interrupted" error that is triggered when we cancel a request
1009
963
// to open an external application and hand it over to UIApplication.openURL(). The result
1010
964
// will be that we switch to the external app, for example the app store, while keeping the
@@ -1036,23 +990,10 @@ extension BrowserViewController: WKNavigationDelegate {
1036
990
1037
991
if let url = error. userInfo [ NSURLErrorFailingURLErrorKey] as? URL {
1038
992
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)
993
+ if tab == self . tabManager. selectedTab {
994
+ self . topToolbar. hideProgressBar ( )
1049
995
}
1050
-
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
-
996
+
1056
997
// If the local web server isn't working for some reason (Brave cellular data is
1057
998
// disabled in settings, for example), we'll fail to load the session restore URL.
1058
999
// We rely on loading that page to get the restore callback to reset the restoring
0 commit comments