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

[@polka/url] Incorrect query params parsing when param contains encoded "&" #150

Open
hoangvvo opened this issue Oct 21, 2020 · 2 comments
Labels

Comments

@hoangvvo
Copy link

hoangvvo commented Oct 21, 2020

Consider the url below:

/?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DeVTXPUF4Oz4%26list%3DPLlqZM4covn1G3hqrvNwpRy19pGDTYkUK6"

@polka/url parse the above into:

{
  url: 'https://www.youtube.com/watch?v=eVTXPUF4Oz4',
  list: 'PLlqZM4covn1G3hqrvNwpRy19pGDTYkUK6'
}

which is incorrect.

Reproduction

const parser = require("@polka/url");
const querystring = require("querystring");

const url = `/?url=${encodeURIComponent(
  "https://www.youtube.com/watch?v=eVTXPUF4Oz4&list=PLlqZM4covn1G3hqrvNwpRy19pGDTYkUK6"
)}`;

console.log(parser({ url }, true)?.query);
// {
//   url: 'https://www.youtube.com/watch?v=eVTXPUF4Oz4',
//   list: 'PLlqZM4covn1G3hqrvNwpRy19pGDTYkUK6'
// }

const queryparamsstr = url.substring(url.indexOf("?") + 1);
console.log(querystring.parse(queryparamsstr));
// [Object: null prototype] {
//   url: 'https://www.youtube.com/watch?v=eVTXPUF4Oz4&list=PLlqZM4covn1G3hqrvNwpRy19pGDTYkUK'
// }
@hoangvvo
Copy link
Author

try { nxt = decodeURIComponent(url) } catch (e) {/* bad */}

This line decode the url too early causing the encoded & to turn into a param separator.

@istarkov
Copy link

Also incorrect query param parsing if param contains +
i.e '?prop=a+b' must be parsed as { prop: 'a b'}

lukeed added a commit that referenced this issue May 23, 2021
* break(url): handle decodes correctly; remove output keys;

- Closes #150

* chore(url): update readme

* break(url): use named `parse` export

* fix(polka): rebuild `req.url` manually;

- includes decoded `req.path` and ENCODED `req.search` segments

* fix(polka): adjust `Request` types

* chore(url): update benchmark

* chore(url): update benchmark results

* chore(url): add "raw performance" results

* chore: linter
@lukeed lukeed added the has fix label May 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants