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

Commit

Permalink
Fix #4967: Guard iOS 14.3 and up to be sandboxed. iOS 14.2 and below …
Browse files Browse the repository at this point in the history
…has a crash: "Fatal error: Bug in WebKit: Received neither result or failure.: file WebKit/WebKitSwiftOverlay.swift, line 66" (#4968)
  • Loading branch information
Brandon-T authored and iccub committed Feb 9, 2022
1 parent da0cf5c commit 0f08fb3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Client/Frontend/Browser/DomainUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ enum DomainUserScript: CaseIterable {
alteredSource = alteredSource.replacingOccurrences(of: "$<setJS>", with: "ABSSJ\(token)",
options: .literal)

return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
return WKUserScript.create(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
case .archive:
return WKUserScript(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
return WKUserScript.create(source: source, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
case .braveSearch:
var alteredSource = source

Expand All @@ -111,7 +111,7 @@ enum DomainUserScript: CaseIterable {
options: .literal)
.replacingOccurrences(of: "$<security_token>", with: securityToken)

return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
return WKUserScript.create(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
case .braveTalk:
var alteredSource = source

Expand All @@ -122,7 +122,7 @@ enum DomainUserScript: CaseIterable {
options: .literal)
.replacingOccurrences(of: "$<security_token>", with: securityToken)

return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
return WKUserScript.create(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/MetadataParserHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MetadataParserHelper: TabEventHandler {
tab.pageMetadata = nil
return
}

webView.evaluateSafeJavaScript(functionName: "__firefox__.metadata && __firefox__.metadata.getMetadata()", contentWorld: .defaultClient, asFunction: false) { (result, error) in
guard error == nil else {
// TabEvent.post(.pageMetadataNotAvailable, for: tab)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class PlaylistWebLoader: UIView {
replacements.forEach({
alteredSource = alteredSource.replacingOccurrences(of: $0.key, with: $0.value, options: .literal)
})
return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
return WKUserScript.create(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false, in: .page)
}()

private weak var certStore: CertStore?
Expand Down
8 changes: 6 additions & 2 deletions Client/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ class Tab: NSObject {
}
if let path = Bundle.main.path(forResource: fileName, ofType: type),
let source = try? String(contentsOfFile: path) {
let userScript = WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: mainFrameOnly, in: contentWorld)
let userScript = WKUserScript.create(source: source, injectionTime: injectionTime, forMainFrameOnly: mainFrameOnly, in: contentWorld)
webView.configuration.userContentController.addUserScript(userScript)
}
}
Expand Down Expand Up @@ -691,7 +691,11 @@ private class TabContentScriptManager: NSObject, WKScriptMessageHandler {
// If this helper handles script messages, then get the handler name and register it. The Tab
// receives all messages and then dispatches them to the right TabHelper.
if let scriptMessageHandlerName = helper.scriptMessageHandlerName() {
tab.webView?.configuration.userContentController.add(self, contentWorld: contentWorld, name: scriptMessageHandlerName)
if #available(iOS 14.3, *) {
tab.webView?.configuration.userContentController.add(self, contentWorld: contentWorld, name: scriptMessageHandlerName)
} else {
tab.webView?.configuration.userContentController.add(self, name: scriptMessageHandlerName)
}
}
}

Expand Down
20 changes: 10 additions & 10 deletions Client/Frontend/Browser/UserScriptManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class UserScriptManager {
let source = try? NSString(contentsOfFile: path, encoding: String.Encoding.utf8.rawValue) as String {
let wrappedSource = "(function() { const SECURITY_TOKEN = '\(UserScriptManager.messageHandlerTokenString)'; \(source) })()"

return WKUserScript(source: wrappedSource,
return WKUserScript.create(source: wrappedSource,
injectionTime: injectionTime,
forMainFrameOnly: mainFrameOnly,
in: sandboxed ? .defaultClient : .page)
Expand All @@ -154,7 +154,7 @@ class UserScriptManager {
}
var alteredSource = source
alteredSource = alteredSource.replacingOccurrences(of: "$<handler>", with: "FingerprintingProtection\(messageHandlerTokenString)", options: .literal)
return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand All @@ -166,7 +166,7 @@ class UserScriptManager {
return nil
}

return WKUserScript(source: source,
return WKUserScript.create(source: source,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand All @@ -188,7 +188,7 @@ class UserScriptManager {
alteredSource = alteredSource.replacingOccurrences(of: "$<paymentreqcallback>", with: "PaymentRequestCallback\(securityTokenString)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<handler>", with: "PaymentRequest\(messageHandlerTokenString)", options: .literal)

return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand All @@ -204,7 +204,7 @@ class UserScriptManager {
alteredSource = alteredSource.replacingOccurrences(of: "$<downloadManager>", with: "D\(securityTokenString)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<handler>", with: "ResourceDownloadManager\(messageHandlerTokenString)", options: .literal)

return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentEnd,
forMainFrameOnly: false,
in: .defaultClient)
Expand All @@ -224,7 +224,7 @@ class UserScriptManager {
alteredSource = alteredSource.replacingOccurrences(of: "$<windowRenderer>", with: "W\(securityTokenString)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<handler>", with: "WindowRenderHelper\(messageHandlerTokenString)", options: .literal)

return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .defaultClient)
Expand All @@ -236,7 +236,7 @@ class UserScriptManager {
return nil
}

return WKUserScript(source: source,
return WKUserScript.create(source: source,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand All @@ -249,7 +249,7 @@ class UserScriptManager {
return nil
}

return WKUserScript(source: source,
return WKUserScript.create(source: source,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand Down Expand Up @@ -294,7 +294,7 @@ class UserScriptManager {
alteredSource = alteredSource.replacingOccurrences(of: $0.key, with: $0.value, options: .literal)
})

return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand All @@ -318,7 +318,7 @@ class UserScriptManager {
alteredSource = alteredSource.replacingOccurrences(of: $0.key, with: $0.value, options: .literal)
})

return WKUserScript(source: alteredSource,
return WKUserScript.create(source: alteredSource,
injectionTime: .atDocumentStart,
forMainFrameOnly: false,
in: .page)
Expand Down
34 changes: 28 additions & 6 deletions Shared/Extensions/WKWebViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ enum JavascriptError: Error {
case invalid
}

public extension WKUserScript {
static func create(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool, in contentWorld: WKContentWorld) -> WKUserScript {

if #available(iOS 14.3, *) {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: contentWorld)
} else {
return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly)
}
}
}

public extension WKWebView {
func generateJSFunctionString(functionName: String, args: [Any?], escapeArgs: Bool = true) -> (javascript: String, error: Error?) {
var sanitizedArgs = [String]()
Expand Down Expand Up @@ -58,13 +69,24 @@ public extension WKWebView {
javascript = js.javascript
}

// swiftlint:disable:next safe_javascript
evaluateJavaScript(javascript, in: nil, in: contentWorld) { result in
switch result {
case .success(let value):
completion?(value, nil)
case .failure(let error):
if #available(iOS 14.3, *) {
// swiftlint:disable:next safe_javascript
evaluateJavaScript(javascript, in: nil, in: contentWorld) { result in
switch result {
case .success(let value):
completion?(value, nil)
case .failure(let error):
completion?(nil, error)
}
}
} else {
// swiftlint:disable:next safe_javascript
evaluateJavaScript(javascript) { result, error in
if let error = error {
completion?(nil, error)
} else {
completion?(result, error)
}
}
}
}
Expand Down

0 comments on commit 0f08fb3

Please sign in to comment.