Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[webview_flutter_wkwebview] Fixes crash when sending undefined message via javascript channel #8776

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

Merged
merged 12 commits into from
Mar 31, 2025

Conversation

LinXunFeng
Copy link
Member

Fixes flutter/flutter#162352

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

* main:
  [webview_flutter_wkwebview] Fixes crash with nil `WKFrameInfo.request` (flutter#8766)

# Conflicts:
#	packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
@MyBlueford16
Copy link

Is there an update on when this PR will be approved and released? Its causing a blocking production issue for us

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stuartmorgan / @tarrinneal for secondary review.

@@ -906,7 +906,7 @@ class WebKitJavaScriptChannelParams extends JavaScriptChannelParams {
return (_, __, WKScriptMessage message) {
if (weakReference.target != null) {
weakReference.target!(
JavaScriptMessage(message: message.body!.toString()),
JavaScriptMessage(message: message.body?.toString() ?? ''),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was '' chosen as the value here? Is this what the iOS implementation returned prior to the proxy API migration? Is it what webview_android returns? Other?

The linked issue notably says that it expects a different value (the string undefined), so I'd like to make sure we're picking '' for a clear reason.

Copy link
Contributor

@bparrishMines bparrishMines Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing, passing undefined to the JavaScriptChannel leads to a null value in this callback. So in practice, undefined seems indistinguishable from a null value. However, I could see a rational for passing the string 'null' instead. I will check what happens on Android.

Copy link
Contributor

@bparrishMines bparrishMines Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Android returns the string 'undefined' and crashes when passing null.

Copy link
Member Author

@LinXunFeng LinXunFeng Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I can't get the undefined passed through postMessage in the userContentController method, I've now overridden the postMessage method to preserve the undefined and null output.

image

Since I am not familiar with Android, I did not handle the crash issue when passing null. This may require another PR to handle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Injecting JS into every single page is problematic; it can lead to a lot of unexpected and hard-to-debug edge cases. It should be a method of absolutely last resort only if there is no other way to accomplish necessary goals. Polyfilling slight differences between iOS and Android behaviors is not sufficiently critical to warrant such an approach.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used webview_flutter: 2.8.0 and the value of message.message is "(null)", as shown in the screenshot of the code below.

image image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message.body was handled on the OC side in the older version of the iOS plugin, But now the it is passed directly to the Dart side, maybe we can use "${message.body}" to handle it like OC, what do you all think?

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to return the string null when WKScriptMessage.body == null. I think this is better because a user could still differentiate between receiving a null or an empty string the majority of the time.

I think the main problem is that JavaScriptMessage.message is non-null in the platform interface, but changing this is a breaking change.

Your thoughts @stuartmorgan?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main problem is that JavaScriptMessage.message is non-null in the platform interface, but changing this is a breaking change.

Yes, ideally we should have designed this to be more consistent with runJavaScript, where we distinguish unsupported values (in that case with an error, but we'd need something else here).

Given the constraints we have now, I think I would vote for '(null)' for consistency with previous implementations on iOS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, now I've adjusted it to return '(null)' when WKScriptMessage.body == null.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging for changes to ensure that the PR doesn't land as currently written.

Copy link
Contributor

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with nits.

@bparrishMines Are you okay with '(null)'?

@@ -18,7 +18,7 @@
/// ) {
/// weakReference.target?.onJavascriptChannelMessage(
/// message.name,
/// message.body!.toString(),
/// message.body?.toString() ?? '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading example code as written, since .toString can be called on null; the ?? '' part can never execute. You could just remove the ! from the previous code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

JavaScriptMessage(
message: message.body == null
? '(null)'
: message.body!.toString()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ! shouldn't be needed here, for the same reason.

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 28, 2025
@auto-submit auto-submit bot merged commit 7b31262 into flutter:main Mar 31, 2025
82 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 31, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 31, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Mar 31, 2025
vashworth pushed a commit to vashworth/packages that referenced this pull request Mar 31, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 2, 2025
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Apr 2, 2025
flutter/packages@125c117...07496eb

2025-04-02 [email protected] [multicast_dns]
MDnsClient::listen supports onError callback (flutter/packages#8888)
2025-04-02 [email protected] Upgrade tests to
use Xcode 16 and iOS 18 (flutter/packages#8968)
2025-04-01 [email protected] Manual roll Flutter from
1d954f4 to 05b5e79 (225 revisions) (flutter/packages#8960)
2025-04-01 [email protected] Drop deprecated HTML head
meta tags (flutter/packages#8970)
2025-04-01 [email protected] Adjust PR checklist formatter
discussion (flutter/packages#8924)
2025-04-01 [email protected] Update CODEOWNERS username
(flutter/packages#8933)
2025-04-01 [email protected]
[flutter_markdown] Added sizedImageBuilder to Markdown widget
(flutter/packages#6739)
2025-04-01 [email protected] [google_maps_flutter] Skip impl copy
of iOS tests (flutter/packages#8975)
2025-04-01 [email protected] [google_maps_flutter] Skip more
hanging iOS tests (flutter/packages#8967)
2025-04-01 [email protected] [url_launcher] When not fully loaded,
clicking close causes the callback to not be triggered correctly.
(flutter/packages#8582)
2025-04-01 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 2 (flutter/packages#8892)
2025-03-31 [email protected] [webview_flutter]
Skip flaky legacy tests on iOS (flutter/packages#8911)
2025-03-31 [email protected] [camera_android] Don't override
default fps range when not recording (flutter/packages#8891)
2025-03-31 [email protected] [google_maps_flutter] Fix
typo and remove duplicitous CHANGELOG entry (flutter/packages#8754)
2025-03-31 [email protected] [extension_gsi] Support the
latest version of googleapis_auth (flutter/packages#8931)
2025-03-31 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.android.tools.build:gradle from 8.0.0 to 8.9.1 in
/packages/pigeon/platform_tests/test_plugin/android
(flutter/packages#8955)
2025-03-31 [email protected] [webview_flutter_wkwebview] Fixes crash
when sending undefined message via javascript channel
(flutter/packages#8776)
2025-03-31 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.android.tools.build:gradle from 8.0.0 to 8.9.1 in
/packages/pigeon/platform_tests/alternate_language_test_plugin/android
(flutter/packages#8946)
2025-03-31 [email protected]
[interactive_media_ads] Fixes `AdEventType`s not triggering on iOS in
release mode (flutter/packages#8918)
2025-03-31 [email protected] [google_maps_flutter] Skip test that
hangs iOS CI (flutter/packages#8958)
2025-03-28 [email protected] Manual roll Flutter from
b16430b to 1d954f4 (114 revisions) (flutter/packages#8922)
2025-03-28 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 3 (flutter/packages#8912)
2025-03-27 [email protected] Use a more deterministic way of
waiting for ad widgets to be ready. (flutter/packages#8920)
2025-03-27 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 1 (flutter/packages#8890)
2025-03-27 [email protected] [pigeon] correct usage of extended
generics in generator methods (flutter/packages#8910)
2025-03-27 [email protected] [video_player] Fix
layout issue caused by `Transform.rotate` not affecting space
calculation. (flutter/packages#8685)
2025-03-27 [email protected]
[shared_preferences] Fix a late initialized error with the example app
(flutter/packages#8540)
2025-03-26 [email protected] [various] Disable sandbox in Chrome
dart tests (flutter/packages#8909)
2025-03-25 [email protected] [tool] Move changed file detection to
base command class (flutter/packages#8730)
2025-03-25 [email protected] [Camera] Add lens
type information (iOS) (flutter/packages#8723)
2025-03-25 [email protected] [pigeon] kotlin equality methods
(flutter/packages#8887)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md

---------

Co-authored-by: Maurice Parrish <[email protected]>
github-merge-queue bot pushed a commit to flutter/flutter that referenced this pull request Apr 3, 2025
flutter/packages@125c117...07496eb

2025-04-02 [email protected] [multicast_dns]
MDnsClient::listen supports onError callback (flutter/packages#8888)
2025-04-02 [email protected] Upgrade tests to
use Xcode 16 and iOS 18 (flutter/packages#8968)
2025-04-01 [email protected] Manual roll Flutter from
1d954f4 to 05b5e79 (225 revisions) (flutter/packages#8960)
2025-04-01 [email protected] Drop deprecated HTML head
meta tags (flutter/packages#8970)
2025-04-01 [email protected] Adjust PR checklist formatter
discussion (flutter/packages#8924)
2025-04-01 [email protected] Update CODEOWNERS username
(flutter/packages#8933)
2025-04-01 [email protected]
[flutter_markdown] Added sizedImageBuilder to Markdown widget
(flutter/packages#6739)
2025-04-01 [email protected] [google_maps_flutter] Skip impl copy
of iOS tests (flutter/packages#8975)
2025-04-01 [email protected] [google_maps_flutter] Skip more
hanging iOS tests (flutter/packages#8967)
2025-04-01 [email protected] [url_launcher] When not fully loaded,
clicking close causes the callback to not be triggered correctly.
(flutter/packages#8582)
2025-04-01 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 2 (flutter/packages#8892)
2025-03-31 [email protected] [webview_flutter]
Skip flaky legacy tests on iOS (flutter/packages#8911)
2025-03-31 [email protected] [camera_android] Don't override
default fps range when not recording (flutter/packages#8891)
2025-03-31 [email protected] [google_maps_flutter] Fix
typo and remove duplicitous CHANGELOG entry (flutter/packages#8754)
2025-03-31 [email protected] [extension_gsi] Support the
latest version of googleapis_auth (flutter/packages#8931)
2025-03-31 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.android.tools.build:gradle from 8.0.0 to 8.9.1 in
/packages/pigeon/platform_tests/test_plugin/android
(flutter/packages#8955)
2025-03-31 [email protected] [webview_flutter_wkwebview] Fixes crash
when sending undefined message via javascript channel
(flutter/packages#8776)
2025-03-31 49699333+dependabot[bot]@users.noreply.github.com
[dependabot]: Bump com.android.tools.build:gradle from 8.0.0 to 8.9.1 in
/packages/pigeon/platform_tests/alternate_language_test_plugin/android
(flutter/packages#8946)
2025-03-31 [email protected]
[interactive_media_ads] Fixes `AdEventType`s not triggering on iOS in
release mode (flutter/packages#8918)
2025-03-31 [email protected] [google_maps_flutter] Skip test that
hangs iOS CI (flutter/packages#8958)
2025-03-28 [email protected] Manual roll Flutter from
b16430b to 1d954f4 (114 revisions) (flutter/packages#8922)
2025-03-28 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 3 (flutter/packages#8912)
2025-03-27 [email protected] Use a more deterministic way of
waiting for ad widgets to be ready. (flutter/packages#8920)
2025-03-27 [email protected] [camera_avfoundation] Test utils
and mocks swift migration - part 1 (flutter/packages#8890)
2025-03-27 [email protected] [pigeon] correct usage of extended
generics in generator methods (flutter/packages#8910)
2025-03-27 [email protected] [video_player] Fix
layout issue caused by `Transform.rotate` not affecting space
calculation. (flutter/packages#8685)
2025-03-27 [email protected]
[shared_preferences] Fix a late initialized error with the example app
(flutter/packages#8540)
2025-03-26 [email protected] [various] Disable sandbox in Chrome
dart tests (flutter/packages#8909)
2025-03-25 [email protected] [tool] Move changed file detection to
base command class (flutter/packages#8730)
2025-03-25 [email protected] [Camera] Add lens
type information (iOS) (flutter/packages#8723)
2025-03-25 [email protected] [pigeon] kotlin equality methods
(flutter/packages#8887)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC [email protected] on the revert to ensure that a
human
is aware of the problem.

To file a bug in Flutter:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md

---------

Co-authored-by: Maurice Parrish <[email protected]>
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 3, 2025
@LinXunFeng LinXunFeng deleted the fix_crash_when_send_undefined branch April 9, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: webview_flutter platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[WebViewController] ios crash when page send undefined message via javascriptChannel
4 participants