Skip to content
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

Fixed missing encoding via URLSearchParams #29834

Closed
wants to merge 1 commit into from

Conversation

scarlac
Copy link
Contributor

@scarlac scarlac commented Sep 2, 2020

Fixes #28966

Summary

URLSearchParams was largely useless, as it did not encode the parameters at all. This PR simply encodes the parameters as per the standard.

Changelog

[General] [Fixed] - URLSearchParams now URL encodes passed values

Test Plan

Ran the new code using jsc (/System/iOSSupport/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc)

_searchParams=[['size', 42],['limit', false],['baz','boing+foo&bang']]; last = this._searchParams.length - 1;
_searchParams.reduce((acc, [key, value], index) => {
  return `${acc}${key}=${encodeURIComponent(value)}${index === last ? '' : '&'}`;
}, '');
// which yields: 
// 'size=42&limit=false&baz=boing%2Bfoo%26bang'

And compared it running similar code but with a browser-shipped URLSearchParams in the browser console of this page:

_searchParams=[['size', 42],['limit', false],['baz','boing+foo&bang']]; last = this._searchParams.length - 1;
new URLSearchParams(_searchParams).toString()
// which yields:
// "size=42&limit=false&baz=boing%2Bfoo%26bang"

Comparing the two:

'size=42&limit=false&baz=boing%2Bfoo%26bang' === "size=42&limit=false&baz=boing%2Bfoo%26bang"
// returns true

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 2, 2020
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: b5b4a70

@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,208,597 -20
android hermes armeabi-v7a 6,857,765 -24
android hermes x86 7,643,225 -20
android hermes x86_64 7,534,177 -12
android jsc arm64-v8a 9,368,250 28
android jsc armeabi-v7a 9,009,554 36
android jsc x86 9,230,974 36
android jsc x86_64 9,808,118 28

Base commit: b5b4a70

@charpeni
Copy link
Contributor

URLSearchParams has the same issue as URLas the implementation inside React Native is all hardcoded and not following the spec.

I would highly recommend migrating over to react-native-url-polyfill instead.

Actually, I should deprecate React Native's URL and URLSearchParam.

@scarlac
Copy link
Contributor Author

scarlac commented Sep 10, 2020

@charpeni I'm not sure what you are saying. Nothing is hard-coded about the PR or implementation and this solves an immediate issue with have the implementation. Unless we can integrate a polyfill from react-native-url-polyfill that ships with React Native, I don't see the point of removing it. We'd be needlessly breaking a lot of code bases for no good reason. The feature is helpful it just has this 1 very important bug.

@charpeni
Copy link
Contributor

charpeni commented Sep 10, 2020

Hey @scarlac.

Sorry for the confusion here, your pull request is definitely the right implementation.

The hard-coded things are coming from React Native itself which is trying to define its own implementation of URL and URLSearchParams. Unfortunately, this process can't scale well and we'll always have issues with unhandled cases and corner cases. (See: #26019, #25717, #24428, #16434). As others pull requests, we can't assert that this pull request isn't breaking any existing code (relying on the bad behavior), and merging PRs that are fixing our custom implementation is a back and forth process.

I'm not against merging this PR, though. I think it's a safe™ and nice fix, just flagging the issue with those.

What I'm advocating for, is to deprecate RN's URL and URLSearchParams in favor of react-native-url-polyfill (which is spec compliant). Of course, as you said we can't remove them right away, but we can display a LogBox warning saying that it is deprecated.

Why we can't ship React Native with react-native-url-polyfill.

How does that sound?

@scarlac
Copy link
Contributor Author

scarlac commented Sep 10, 2020

@charpeni Yeah it looks like it's ~40k extra. I don't like it but it's a reasonable trade-off. Agree with your approach then. Thanks for the thorough explanation.

@scarlac
Copy link
Contributor Author

scarlac commented Dec 3, 2021

Closing this PR. As per @charpeni 's recommendation an app-installed polyfill is preferred and we should be removing the existing implementation.

@scarlac scarlac closed this Dec 3, 2021
@scarlac scarlac deleted the patch-1 branch December 3, 2021 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape URI components for URLSearchParams
5 participants