Repository navigation
fix(ios): restrict native bridge handlers to the main frame - #8740
Open
basnijholt wants to merge 1 commit into
Open
basnijholt wants to merge 1 commit into
basnijholt wants to merge 1 commit into
Conversation
Assisted-by: MindRoom DevAgent:google/gemini-4-neon <https://github.com/mindroom-ai/mindroom>
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.