Treat result of binding function as JSON, not JS #1002
Merged
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.
While the documentation of
webview_return()
states that the result must be valid JSON, it doesn't validate or parse the result as JSON, instead treats it as JavaScript code and evaluates it.Not only can it be confusing to someone who expects the function to treat the result as JSON, it's also a potential security risk because it can allow loading of arbitrary JS code.
This work implements JSON string escaping and calls
JSON.parse()
on the JS side without evaluating the result as JS code.Tests broke because of binding functions returning JS code that were expected to be evaluated, and has been adjusted to call
webview::eval()
instead.If the string returned from a binding function is empty then the result on the JS side will be the primitive value
undefined
to retain the existing behavior.If the string returned from a binding function can't be parsed by
JSON.parse()
then the promise on the JS side is rejected.In any other case the promise on the JS side resolves with the parsed JSON value if the status value is 0; otherwise the promise rejects.