From 68ddb1b9bd9078d9018155d970c854d4bfbd1599 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Sat, 21 Nov 2026 16:35:33 -0800 Subject: [PATCH 1/2] fix(ios): restrict native bridge handlers to the main frame Assisted-by: MindRoom DevAgent:google/gemini-4-neon --- .../Capacitor/WebViewDelegationHandler.swift | 16 ++- .../HttpInterceptorNavigationTests.swift | 17 ++- .../WebViewDelegationHandlerTests.swift | 127 ++++++++++++++++++ 3 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift diff --git a/ios/Sources/Capacitor/WebViewDelegationHandler.swift b/ios/Sources/Capacitor/WebViewDelegationHandler.swift index 93310d456..27b73a983 100644 --- a/ios/Sources/Capacitor/WebViewDelegationHandler.swift +++ b/ios/Sources/Capacitor/WebViewDelegationHandler.swift @@ -190,6 +190,11 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat // MARK: - WKScriptMessageHandler open func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + guard message.frameInfo.isMainFrame else { + CAPLog.print("⚡️ Plugin execution is allowed in Main Frame only") + return + } + guard let bridge = bridge else { return } @@ -261,9 +266,9 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat open func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) { - // Check if this is synchronous cookie or http call - do { - if let dataFromString = prompt.data(using: .utf8, allowLossyConversion: false) { + // Check if this is synchronous cookie call (main frame only) + if frame.isMainFrame, let dataFromString = prompt.data(using: .utf8, allowLossyConversion: false) { + do { if let payload = try JSONSerialization.jsonObject(with: dataFromString, options: .fragmentsAllowed) as? [String: AnyObject] { let type = payload["type"] as? String @@ -282,12 +287,13 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat return } } + } catch { + // Continue with regular prompt } - } catch { - // Continue with regular prompt } guard let viewController = bridge?.viewController else { + completionHandler(nil) return } diff --git a/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift b/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift index b7dac2455..ee6a95612 100644 --- a/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift +++ b/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift @@ -3,13 +3,20 @@ import XCTest @testable import Capacitor -private class StubFrameInfo: WKFrameInfo { - override var isMainFrame: Bool { false } +class StubFrameInfo: WKFrameInfo { + private let stubbedIsMainFrame: Bool + override var isMainFrame: Bool { stubbedIsMainFrame } + + private init(isMainFrame: Bool) { + self.stubbedIsMainFrame = isMainFrame + super.init() + } // WKFrameInfo has no public initializer, so super.init() leaves WebKit's internal state - // unset and -[WKFrameInfo dealloc] then dereferences garbage. Keeping a single instance - // alive for the process avoids ever deallocating one. - static let shared = StubFrameInfo() + // unset and -[WKFrameInfo dealloc] then dereferences garbage. Keeping instances alive + // for the process avoids ever deallocating them. + static let shared = StubFrameInfo(isMainFrame: false) + static let main = StubFrameInfo(isMainFrame: true) } private class StubNavigationAction: WKNavigationAction { diff --git a/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift b/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift new file mode 100644 index 000000000..b0a23b577 --- /dev/null +++ b/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift @@ -0,0 +1,127 @@ +import UIKit +import WebKit +import XCTest + +@testable import Capacitor + +private class StubScriptMessage: WKScriptMessage { + private let stubbedBody: Any + private let stubbedFrameInfo: WKFrameInfo + private static var retained: [StubScriptMessage] = [] + + private init(body: Any, frameInfo: WKFrameInfo) { + self.stubbedBody = body + self.stubbedFrameInfo = frameInfo + super.init() + } + + static func make(body: Any, isMainFrame: Bool) -> StubScriptMessage { + let message = StubScriptMessage( + body: body, + frameInfo: isMainFrame ? StubFrameInfo.main : StubFrameInfo.shared + ) + retained.append(message) + return message + } + + override var body: Any { stubbedBody } + override var frameInfo: WKFrameInfo { stubbedFrameInfo } +} + +private class RecordingBridgeViewController: CAPBridgeViewController { + var presentedControllers: [UIViewController] = [] + + override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) { + presentedControllers.append(viewControllerToPresent) + completion?() + } +} + +class WebViewDelegationHandlerTests: XCTestCase { + private var bridge: MockBridge! + private var handler: WebViewDelegationHandler! + private var viewController: RecordingBridgeViewController? + private let webView = WKWebView() + + override func setUp() { + super.setUp() + let descriptor = InstanceDescriptor() + handler = WebViewDelegationHandler() + let vc = RecordingBridgeViewController() + viewController = vc + bridge = MockBridge( + with: InstanceConfiguration(with: descriptor, isDebug: true), + delegate: vc, + assetHandler: MockAssetHandler(router: CapacitorRouter()), + delegationHandler: handler + ) + } + + func testSubframeScriptMessageIsIgnored() { + var received: [String: Any]? + bridge.registerCallInterceptor("test.interceptor") { received = $0 } + + let message = StubScriptMessage.make(body: ["type": "test.interceptor"], isMainFrame: false) + handler.userContentController(handler.contentController, didReceive: message) + + XCTAssertNil(received) + } + + func testMainFrameScriptMessageIsDispatched() { + var received: [String: Any]? + bridge.registerCallInterceptor("test.interceptor") { received = $0 } + + let message = StubScriptMessage.make(body: ["type": "test.interceptor"], isMainFrame: true) + handler.userContentController(handler.contentController, didReceive: message) + + XCTAssertEqual(received?["type"] as? String, "test.interceptor") + } + + func testSubframePromptDoesNotInvokeCookiesBridge() { + var completionCalled = false + handler.webView( + webView, + runJavaScriptTextInputPanelWithPrompt: #"{"type":"CapacitorCookies.get"}"#, + defaultText: nil, + initiatedByFrame: StubFrameInfo.shared + ) { _ in + completionCalled = true + } + + XCTAssertFalse(completionCalled) + XCTAssertEqual(viewController?.presentedControllers.count, 1) + } + + func testSubframeCookiePromptCompletesOnceWithNilWhenNoPresenter() { + // bridge.bridgeDelegate is weak, so releasing viewController leaves the bridge alive + // with a nil viewController. + viewController = nil + XCTAssertNotNil(handler.bridge) + XCTAssertNil(handler.bridge?.viewController) + + var completions: [String?] = [] + handler.webView( + webView, + runJavaScriptTextInputPanelWithPrompt: #"{"type":"CapacitorCookies.get"}"#, + defaultText: nil, + initiatedByFrame: StubFrameInfo.shared + ) { result in + completions.append(result) + } + + XCTAssertEqual(completions, [nil]) + } + + func testMainFramePromptHandlesCapacitorCookiesGet() { + var result: String? + handler.webView( + webView, + runJavaScriptTextInputPanelWithPrompt: #"{"type":"CapacitorCookies.get"}"#, + defaultText: nil, + initiatedByFrame: StubFrameInfo.main + ) { result = $0 } + + XCTAssertNotNil(result) + XCTAssertEqual(viewController?.presentedControllers.count, 0) + } +} From 5680cca346b507c47eb2ee803306032ed3a359c5 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Fri, 27 Nov 2026 08:13:26 -0800 Subject: [PATCH 2/2] fix(ios): complete confirm panel without a view controller; share StubFrameInfo Assisted-by: MindRoom DevAgent:anthropic/claude-opus-5.5 --- .../Capacitor/WebViewDelegationHandler.swift | 1 + .../CapacitorTests/FileProxyRouteTests.swift | 9 +------- .../HttpInterceptorNavigationTests.swift | 4 ++-- .../WebViewDelegationHandlerTests.swift | 22 ++++++++++++++++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/ios/Sources/Capacitor/WebViewDelegationHandler.swift b/ios/Sources/Capacitor/WebViewDelegationHandler.swift index 6fd47bb6a..461181d66 100644 --- a/ios/Sources/Capacitor/WebViewDelegationHandler.swift +++ b/ios/Sources/Capacitor/WebViewDelegationHandler.swift @@ -248,6 +248,7 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat open func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) { guard let viewController = bridge?.viewController else { + completionHandler(false) return } diff --git a/ios/Tests/CapacitorTests/FileProxyRouteTests.swift b/ios/Tests/CapacitorTests/FileProxyRouteTests.swift index 03403ee03..66ef916d6 100644 --- a/ios/Tests/CapacitorTests/FileProxyRouteTests.swift +++ b/ios/Tests/CapacitorTests/FileProxyRouteTests.swift @@ -3,19 +3,12 @@ import XCTest @testable import Capacitor -private class StubFrameInfo: WKFrameInfo { - override var isMainFrame: Bool { false } - - // See HttpInterceptorNavigationTests: WKFrameInfo must never be deallocated. - static let shared = StubFrameInfo() -} - private class StubNavigationAction: WKNavigationAction { private let stubbedRequest: URLRequest private let stubbedTargetFrame: WKFrameInfo? init(url: String, subframe: Bool) { self.stubbedRequest = URLRequest(url: URL(string: url)!) - self.stubbedTargetFrame = subframe ? StubFrameInfo.shared : nil + self.stubbedTargetFrame = subframe ? StubFrameInfo.subframe : nil super.init() } override var request: URLRequest { stubbedRequest } diff --git a/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift b/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift index ee6a95612..25a82e814 100644 --- a/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift +++ b/ios/Tests/CapacitorTests/HttpInterceptorNavigationTests.swift @@ -15,7 +15,7 @@ class StubFrameInfo: WKFrameInfo { // WKFrameInfo has no public initializer, so super.init() leaves WebKit's internal state // unset and -[WKFrameInfo dealloc] then dereferences garbage. Keeping instances alive // for the process avoids ever deallocating them. - static let shared = StubFrameInfo(isMainFrame: false) + static let subframe = StubFrameInfo(isMainFrame: false) static let main = StubFrameInfo(isMainFrame: true) } @@ -24,7 +24,7 @@ private class StubNavigationAction: WKNavigationAction { private let stubbedTargetFrame: WKFrameInfo? init(url: String, subframe: Bool = false) { self.stubbedRequest = URLRequest(url: URL(string: url)!) - self.stubbedTargetFrame = subframe ? StubFrameInfo.shared : nil + self.stubbedTargetFrame = subframe ? StubFrameInfo.subframe : nil super.init() } override var request: URLRequest { stubbedRequest } diff --git a/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift b/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift index b0a23b577..f31b63baa 100644 --- a/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift +++ b/ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift @@ -18,7 +18,7 @@ private class StubScriptMessage: WKScriptMessage { static func make(body: Any, isMainFrame: Bool) -> StubScriptMessage { let message = StubScriptMessage( body: body, - frameInfo: isMainFrame ? StubFrameInfo.main : StubFrameInfo.shared + frameInfo: isMainFrame ? StubFrameInfo.main : StubFrameInfo.subframe ) retained.append(message) return message @@ -83,7 +83,7 @@ class WebViewDelegationHandlerTests: XCTestCase { webView, runJavaScriptTextInputPanelWithPrompt: #"{"type":"CapacitorCookies.get"}"#, defaultText: nil, - initiatedByFrame: StubFrameInfo.shared + initiatedByFrame: StubFrameInfo.subframe ) { _ in completionCalled = true } @@ -104,7 +104,7 @@ class WebViewDelegationHandlerTests: XCTestCase { webView, runJavaScriptTextInputPanelWithPrompt: #"{"type":"CapacitorCookies.get"}"#, defaultText: nil, - initiatedByFrame: StubFrameInfo.shared + initiatedByFrame: StubFrameInfo.subframe ) { result in completions.append(result) } @@ -112,6 +112,22 @@ class WebViewDelegationHandlerTests: XCTestCase { XCTAssertEqual(completions, [nil]) } + func testConfirmCompletesOnceWithFalseWhenNoPresenter() { + viewController = nil + XCTAssertNil(handler.bridge?.viewController) + + var completions: [Bool] = [] + handler.webView( + webView, + runJavaScriptConfirmPanelWithMessage: "Continue?", + initiatedByFrame: StubFrameInfo.main + ) { result in + completions.append(result) + } + + XCTAssertEqual(completions, [false]) + } + func testMainFramePromptHandlesCapacitorCookiesGet() { var result: String? handler.webView(