Repository navigation
fix(ios): restrict native bridge handlers to the main frame - #8740
Open
basnijholt wants to merge 3 commits into
Open
basnijholt wants to merge 3 commits into
basnijholt wants to merge 3 commits into
Conversation
Assisted-by: MindRoom DevAgent:google/gemini-4-neon <https://github.com/mindroom-ai/mindroom>
andredestro
reviewed
Nov 27, 2026
andredestro
left a comment
Contributor
There was a problem hiding this comment.
Thanks for this! I ran it locally against current main (Xcode 27.2, iPhone 17 / iOS 27.0 simulator) and have a few questions:
- Once rebased on main, the test target doesn't compile:
FileProxyRouteTests.swift(added after this PR was opened) declares its ownprivate class StubFrameInfo, which now clashes with the shared one (invalid redeclaration of 'StubFrameInfo'). Could we drop the private copy there and use the shared stub? With that change the full suite passes for me (XCTest 32/32, Swift Testing 132/132). runJavaScriptConfirmPanelWithMessagealso returns without calling its completion handler when there's no view controller, like the prompt case you fixed. Should we callcompletionHandler(false)there too?- Nit: now that
StubFrameInfohas both variants, would.subframeread better than.shared?
I also checked in a real WKWebView that calls made through parent (like parent.Capacitor) are still attributed to the main frame, while direct posts from an iframe, sandboxed or not, come in as subframe. So the iframe impact described in the PR holds.
…bFrameInfo Assisted-by: MindRoom DevAgent:anthropic/claude-opus-5.5 <https://github.com/mindroom-ai/mindroom>
Author
|
Thanks for testing it on a real WKWebView! I merged
On Xcode 27.2 with the iPhone 17 simulator, |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Enforce main-frame-only execution in
WebViewDelegationHandlerusing WebKit's native frame metadata:userContentController(_:didReceive:), return early (logging"⚡️ Plugin execution is allowed in Main Frame only") when!message.frameInfo.isMainFrame.webView(_:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:), only intercept synchronousCapacitorCookies.get/CapacitorCookies.setprompts whenframe.isMainFrameistrue, letting regular subframewindow.prompt()calls fall through normally.webView(_:runJavaScriptTextInputPanelWithPrompt:...), callcompletionHandler(nil)before returning whenbridge?.viewControllerisnil, so a subframe cookie prompt (or any prompt without a presenter) never drops WebKit's completion handler.Added regression tests in
ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift(sharingStubFrameInfowithHttpInterceptorNavigationTests.swift).Change Type
Rationale / Problems Fixed
Although
JSExportinjects all iOS bridge scripts withforMainFrameOnly: trueand Android'sMessageHandler(WebMessageListenerpath) enforcesif (isMainFrame)before dispatching messages, WebKit exposeswindow.webkit.messageHandlers.bridgeandrunJavaScriptTextInputPanelWithPromptto every frame in theWKWebView(including cross-origin and sandboxed iframes). Without anisMainFramecheck inWebViewDelegationHandler, subframes can invoke native plugins directly viawebkit.messageHandlers.bridge.postMessage(...)or read/write native cookies viawindow.prompt(...).No related issue: security hardening, see description.
Tests or Reproductions
Added
ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swiftcovering:WKScriptMessageis ignored byuserContentController(_:didReceive:).WKScriptMessageis dispatched normally.window.promptwith aCapacitorCookies.getpayload does not invokeCapacitorCookieManagerand falls through to normal prompt presentation.window.promptwith aCapacitorCookies.getpayload invokescompletionHandler(nil)exactly once whenbridgeis alive butviewControllerisnil.window.promptwithCapacitorCookies.getstill resolves synchronously without presenting an alert.Built and tested on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator):
xcodebuild build -scheme Capacitor-Package -destination 'generic/platform=iOS Simulator'—BUILD SUCCEEDEDxcodebuild test -scheme Capacitor-Package -destination 'platform=iOS Simulator,name=iPhone 17,OS=26.5' -collect-test-diagnostics never—TEST SUCCEEDED:CapacitorObjCTests.xctest: 6/6 passedCapacitorTests.xctest(XCTest): 22/22 passed (including 5/5 inWebViewDelegationHandlerTestsand 3/3 inHttpInterceptorNavigationTests)Not run locally:
npm run swiftlint -- lint(unverified; CI will check).Screenshots / Media
N/A
Platforms Affected
Notes / Comments
parent.Capacitorshould be unaffected (calls are sent through the parent window's handler and prompt); only code posting directly towebkit.messageHandlers.bridge(or forgedCapacitorCookies.*prompts) from a subframe stops working, matching Android'sMessageHandlerWebMessageListenerpath (Android's legacyaddJavascriptInterfacefallback andCapacitorCookiesAndroidInterfacedo not check frames).mainas a security bug fix; maintainers may prefernextif they treat blocking direct subframe bridge posts as a breaking change (and8.xhas the same code underios/Capacitor/Capacitor/WebViewDelegationHandler.swiftif a backport is wanted).google/gemini-4-neon); thexcodebuild buildandxcodebuild testcommands listed under Tests were run on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator); SwiftLint was not run locally.