Repository navigation
feat(android): allow scoping WebView bridge origin policy - #8701
Open
deggertsen wants to merge 1 commit into
Open
deggertsen wants to merge 1 commit into
deggertsen wants to merge 1 commit into
Conversation
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.
What
Apps using
server.urloften list additional hosts inserver.allowNavigation(such as OAuth providers or marketing pages) so top-level flows stay inside the WebView instead of opening the system browser.Today on Android,
Bridge.setAllowedOriginRules()feeds everyserver.allowNavigationentry intoMessageHandler'sWebViewCompat.addWebMessageListener(...)registration, andMessageHandlerfalls back towebView.addJavascriptInterface(this, "androidBridge")ifWEB_MESSAGE_LISTENERis unavailable or registration fails.CapacitorCookies.load()also unconditionally attachesCapacitorCookiesAndroidInterfaceviaaddJavascriptInterface, which has no caller-origin or frame check.This adds an opt-in
Bridge.Builder.setWebMessagePolicy(...)hook (MessageHandler.WebMessagePolicy) so Android host apps can scope native bridge access to a narrower set of trusted origins thanserver.allowNavigationwithout changing default behavior for existing apps:BridgeActivity/Bridge.Buildercallers to supply aMessageHandler.WebMessagePolicywith explicitWebMessageListeneroriginRulesand a runtimesourceOriginpredicate (acceptsOrigin).javaScriptReplyProxybefore dispatching accepted main-frame messages.scopedWebMessagesis configured: skips registeringaddJavascriptInterface(this, "androidBridge")and skipsevaluateJavascriptlegacy responses whenWEB_MESSAGE_LISTENERis unavailable or listener registration fails.CapacitorCookiesAndroidInterfaceinCapacitorCookies.load()whenbridge.hasScopedWebMessages()is enabled (native-bridge.tsalready guardstypeof win.CapacitorCookiesAndroidInterface !== 'undefined').Verification
MessageHandlerTestandCapacitorCookiesTestcovering trusted main-frame dispatch, subframe/untrusted/null-origin rejection, fail-closed handling whenWEB_MESSAGE_LISTENERis unsupported or throws, empty origin rules, andCapacitorCookiesAndroidInterfaceregistration gating.npm run eslintandnpm run prettier -- --check(0 errors).npm run verify -w android(./gradlew clean lint build test -b capacitor/build.gradleon JDK 21): Android Lint found no new issues, and bothtestDebugUnitTestandtestReleaseUnitTestpass.