-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Android's WebViewCompat.addWebMessageListener fails when navigating to a local website with http scheme #7570
Labels
Comments
Open
3 tasks
martin-braun
changed the title
[Bug]: Android's WebViewCompat.addWebMessageListener fails when navigating to a remote website
[Bug]: Android's WebViewCompat.addWebMessageListener fails when navigating to a local website with http scheme
Jul 19, 2024
closing as duplicate of #7454 |
This comment was marked as abuse.
This comment was marked as abuse.
This comment was marked as abuse.
This comment was marked as abuse.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Capacitor Version
Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 6.1.1
@capacitor/core: 6.1.1
@capacitor/android: 6.1.1
@capacitor/ios: 6.1.1
Installed Dependencies:
@capacitor/ios: not installed
@capacitor/core: 6.1.1
@capacitor/android: 6.1.1
@capacitor/cli: 6.1.1
[success] Android looking great! 👌
Other API Details
Platforms Affected
Current Behavior
When your app loads a website on the local network, such as
http://192.168.77.166:8080
this will open a new browser untilallowNavigation
is supplied with192.168.77.166
in the Capacitor configuration. When using the legacy bridgeuseLegacyBridge: true
theMessageHandler
would usewebView.addJavascriptInterface
to inject thewindow.androidBridge
object into the remote website, so that the platform is recognized asandroid
and the remote website would be able to communicate with the native plugins of Capactor, see:capacitor/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java
Line 41 in e0f299d
If the
useLegacyBridge: false
(default behavior), theMessageHandler
will attempt to useWebViewCompat.addWebMessageListener
to inject theandroidBridge
object:capacitor/android/capacitor/src/main/java/com/getcapacitor/MessageHandler.java
Line 36 in e0f299d
This however isn't working if the target website has
http://
instead ofhttps://
scheme, thusandroidBridge
is null after navigating to unsafe remote websites.WebViewCompat.addWebMessageListener
explicitly states:So, Capacitor requires
allowNavigation
to be a list of domains without scheme, such asexample.org
and you then makebridge.getAllowedOriginRules()
return an array of such list with the proper scheme, which ishttps://
always. This is the wrong behavior on local websites that don't use SSL for obvious reasons.useLegacyBridge: false
or changing the code to prefixhttp://
instead, native plugins will still not work, until the domain is specified with a port another time within theallowNavigation
list. Otherwise the pattern matching will fail and, despite theandroidBridge
not beingnull
, won't have an implementation for the platform and report so.I was able to bypass both issues by having this in my configuration:
Now
window.androidBridge
is not null on the network website and my native plugins work.Expected Behavior
We need a better way here. This was confusing, wasted a lot of time, is not documented. Providing
http://192.168.0.80:8080
should be enough to satisfy all rules. I was going crazy testing this. There is no documentation explaining this.Project Reproduction
Additional Information
I cannot share my private project, you should be able to reproduce with:
Use your own network IP and host a little web server on
0.0.0.0:8080
to see how it's not working, until you add all theallowNavigation
entries.The text was updated successfully, but these errors were encountered: