Skip to content

fix(ios): restrict native bridge handlers to the main frame - #8740

Open
basnijholt wants to merge 1 commit into
ionic-team:mainfrom
basnijholt:fix/ios-bridge-main-frame-only
Open

basnijholt wants to merge 1 commit into
ionic-team:mainfrom
basnijholt:fix/ios-bridge-main-frame-only

Conversation

@basnijholt

Copy link
Copy Markdown

Description

Enforce main-frame-only execution in WebViewDelegationHandler using WebKit's native frame metadata:

  • In userContentController(_:didReceive:), return early (logging "⚡️ Plugin execution is allowed in Main Frame only") when !message.frameInfo.isMainFrame.
  • In webView(_:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:), only intercept synchronous CapacitorCookies.get / CapacitorCookies.set prompts when frame.isMainFrame is true, letting regular subframe window.prompt() calls fall through normally.
  • In webView(_:runJavaScriptTextInputPanelWithPrompt:...), call completionHandler(nil) before returning when bridge?.viewController is nil, 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 (sharing StubFrameInfo with HttpInterceptorNavigationTests.swift).

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

Although JSExport injects all iOS bridge scripts with forMainFrameOnly: true and Android's MessageHandler (WebMessageListener path) enforces if (isMainFrame) before dispatching messages, WebKit exposes window.webkit.messageHandlers.bridge and runJavaScriptTextInputPanelWithPrompt to every frame in the WKWebView (including cross-origin and sandboxed iframes). Without an isMainFrame check in WebViewDelegationHandler, subframes can invoke native plugins directly via webkit.messageHandlers.bridge.postMessage(...) or read/write native cookies via window.prompt(...).

No related issue: security hardening, see description.

Tests or Reproductions

Added ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift covering:

  • Subframe WKScriptMessage is ignored by userContentController(_:didReceive:).
  • Main-frame WKScriptMessage is dispatched normally.
  • Subframe window.prompt with a CapacitorCookies.get payload does not invoke CapacitorCookieManager and falls through to normal prompt presentation.
  • Subframe window.prompt with a CapacitorCookies.get payload invokes completionHandler(nil) exactly once when bridge is alive but viewController is nil.
  • Main-frame window.prompt with CapacitorCookies.get still 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 SUCCEEDED
  • xcodebuild test -scheme Capacitor-Package -destination 'platform=iOS Simulator,name=iPhone 17,OS=26.5' -collect-test-diagnostics never — TEST SUCCEEDED:
    • CapacitorObjCTests.xctest: 6/6 passed
    • CapacitorTests.xctest (XCTest): 22/22 passed (including 5/5 in WebViewDelegationHandlerTests and 3/3 in HttpInterceptorNavigationTests)
    • Swift Testing: 132/132 passed in 19 suites

Not run locally:

  • npm run swiftlint -- lint (unverified; CI will check).

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

  • Iframe impact: Same-origin iframes calling plugins through parent.Capacitor should be unaffected (calls are sent through the parent window's handler and prompt); only code posting directly to webkit.messageHandlers.bridge (or forged CapacitorCookies.* prompts) from a subframe stops working, matching Android's MessageHandler WebMessageListener path (Android's legacy addJavascriptInterface fallback and CapacitorCookiesAndroidInterface do not check frames).
  • Target branch: Targeted at main as a security bug fix; maintainers may prefer next if they treat blocking direct subframe bridge posts as a breaking change (and 8.x has the same code under ios/Capacitor/Capacitor/WebViewDelegationHandler.swift if a backport is wanted).
  • AI assistance: Written with MindRoom DevAgent (google/gemini-4-neon); the xcodebuild build and xcodebuild test commands listed under Tests were run on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator); SwiftLint was not run locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant