Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter] Fix platform exception on iOS 14+ when evaluating any JavaScript that evaluates to null or undefined. #4328

Closed
wants to merge 5 commits into from

Conversation

BeMacized
Copy link
Contributor

@BeMacized BeMacized commented Sep 9, 2021

Currently, a PlatformException is thrown whenever evaluateJavascript is run for any JavaScript that evaluates to null or undefined.

This is due to (I assume to be) a regression in webkit's evaluateJavascript function, which no longer supports null or undefined for the given JavaScript to evaluate to, as of iOS 14. (Still tested working on iOS 11.4 and 12.4)

This PR attempts to fix this problem by instead evaluating JavaScript using callAsyncJavaScript on iOS 14+.

Relevant issue:

Pre-launch Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [relevant style guides] and ran [the auto-formatter]. (Note that unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the [CLA].
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy].
    • NOTE: I have marked this as a patch. However, iOS will now always return "null" for both null and undefined to match the Android side's behavior, rather than "(null)" for null and "<null>" for undefined. I am not entirely certain if this should be considered a breaking change.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test exempt.
  • All existing and new tests are passing.

@google-cla google-cla bot added the cla: yes label Sep 9, 2021
@github-actions github-actions bot added p: webview_flutter Edits files for a webview_flutter plugin platform-ios labels Sep 9, 2021
@stuartmorgan stuartmorgan self-requested a review September 15, 2021 17:20
};
if (@available(iOS 14, *)) {
jsString = [NSString
stringWithFormat:@"return eval(\"%@\");", [self escapeStringForJavaScript:jsString]];
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason for wrapping this in an eval? That seems like something that could cause side-effects. E.g., I believe CSP could completely break this.

Copy link
Contributor Author

@BeMacized BeMacized Sep 27, 2021

Choose a reason for hiding this comment

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

The reason for this is that unlike evaluateJavascript, callAsyncJavaScript executes the given string as the body of an async function, rather than directly evaluating it. As such, it will only return any value that is explicitly returned, not the result of the string being evaluated. I use eval here to make sure the behavior is identical to what evaluateJavascript does on iOS 13 and below and avoid a breaking change.

Personally I have not been able to find any edge cases or side effects, but I've also not tried anything to do with CSP. If you have any examples of this maybe we can find a way around it?

The only other way I can think of to avoid these side effects, assuming they're a thing, is to fix this the other way around:(make evaluateJavaScript behave like callAsyncJavaScript), forcing everyone to explicitly return a value in their JS strings, and consider it a breaking change.

Please let me know what you think. In the mean time I'll update this PR to move the fix to the separated iOS implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've also not tried anything to do with CSP. If you have any examples of this

I don't have significant experience with CSP, but my expectation is that if you tried to run this on a site with CSP enabled and without unsafe-eval set, it would completely fail.

maybe we can find a way around it?

That would be worrying, since it would mean you we bypassing the browser's enforcement of the site's security policy...

Copy link
Contributor

Choose a reason for hiding this comment

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

(make evaluateJavaScript behave like callAsyncJavaScript), forcing everyone to explicitly return a value in their JS strings, and consider it a breaking change.

How would that be better than the current behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have significant experience with CSP, but my expectation is that if you tried to run this on a site with CSP enabled and without unsafe-eval set, it would completely fail.

Alright, I'll see if I can give this a test tomorrow.

How would that be better than the current behavior?

Assuming with "current behavior" you mean this branch rather than master: It would no longer require eval to be used, at the expense of introducing a breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean master; isn't the issue you are trying to fix that a void return causes an error? Can't people already avoid that error right now by explicitly returning a value?

Copy link
Contributor Author

@BeMacized BeMacized Sep 27, 2021

Choose a reason for hiding this comment

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

On master the platform exception can indeed be avoided by ensuring the given string evaluates to a non-null/non-undefined value.

E.g:
Snackbar.postMessage("...") Throws on iOS 14+.
Snackbar.postMessage("...");1 Does not.

@BeMacized
Copy link
Contributor Author

Superseded by #4401

@BeMacized BeMacized closed this Oct 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes p: webview_flutter Edits files for a webview_flutter plugin platform-ios
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants