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

Avoid erroring when getQueryArgs processes a malformed URL #45561

Merged

Conversation

kozer
Copy link
Contributor

@kozer kozer commented Nov 4, 2022

What?

Related to #45558

When getQueryArgs parses a malformed URL it throws an error.

Why?

The current functionality breaks calypso
Please check this issue for more info

How?

Ignore the malformed parameters and continue parsing the url

Testing Instructions

Before

  1. Try to parse the following url: http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A using getQueryArgs('http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A')
  2. Validate that function throws an error

After

  1. Try to parse the following url: http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A using getQueryArgs('http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A')
  2. Validate that the function doesn't throw an error, and the parsing continues

Screenshots or screencast

@codesandbox
Copy link

codesandbox bot commented Nov 4, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 4, 2022
@github-actions
Copy link

github-actions bot commented Nov 4, 2022

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @kozer! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@kozer kozer marked this pull request as draft November 4, 2022 16:12
@kozer kozer marked this pull request as ready for review November 4, 2022 16:22
@kozer
Copy link
Contributor Author

kozer commented Jan 3, 2023

@danielbachhuber Can you please take a look into this? It's part of this issue.

Thanks!

const url = 'https://andalouses.example/beach?foo=bar&baz=%E0%A4%A';

expect( getQueryArgs( url ) ).toEqual( {
foo: 'bar',
Copy link
Member

Choose a reason for hiding this comment

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

PHP's parse_str() returns the malformed query var:

wp shell
wp> parse_str( 'foo=bar&baz=%E0%A4%A', $args );
=> NULL
$ wp> $args
=> array(2) {
  ["foo"]=>
  string(3) "bar"
  ["baz"]=>
  string(4) "�%A"
}

getQueryArgs() was originally designed to emulate parse_str() (#20693). Would it be possible to return the malformed value instead, for consistent 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.

Thanks, @danielbachhuber for pointing this out. I wasn't familiar with that.
I did the changes as requested.

Please note, that although this is a step to the right direction, will minimally affect the issue we have in calypso.

Calypso uses two ways to parse URLs:

  1. qs library
  2. Using the following import:
import { getQueryArgs } from 'calypso/lib/query-args';

This internally uses the get-query-args I'm about to fix here, but to fix the issue related to this pr, we need to stop using qs and start using our function as stated above.

I proposed to add that inside the calypso README file for other developers to be aware.

We can create a ticket to remove qs library of course, but this is a huge take and I think it's better if we do it incrementally.

Maybe we should communicate that also to the calypso channel as well?

Any thoughts?

Thank you!

Copy link
Member

Choose a reason for hiding this comment

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

This internally uses the get-query-args I'm about to fix here, but to fix the issue related to this pr, we need to stop using qs and start using our function as stated above.

I proposed to add that inside the calypso README file for other developers to be aware.

We can create a ticket to remove qs library of course, but this is a huge take and I think it's better if we do it incrementally.

@kozer I'm not sure README is quite the right approach. Can you create a new issue in https://github.com/Automattic/wp-calypso and we can discuss with the Calypso team?

Copy link
Contributor

Choose a reason for hiding this comment

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

getQueryArgs() was originally designed to emulate parse_str() (#20693). Would it be possible to return the malformed value instead, for consistent behavior?

I kind of agree with this. Is there is a reason not to return the value? You could just replace decodeURIComponent with safeDecodeURIComponent then.

Copy link
Member

Choose a reason for hiding this comment

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

Raising another hand of agreement. I think using safeDecodeURIComponent was the intention here since it does exactly what we're aiming for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ntsekouras , @tyxla I did the changes you proposed. Thanks so much!
I already have created an issue here. Do you think it make sense to open a new one?

Copy link
Member

Choose a reason for hiding this comment

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

I already have created an issue here. Do you think it make sense to open a new one?

@kozer No, that's sufficient.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for taking care of that, @kozer, looks great 🙌

@danielbachhuber danielbachhuber added the [Package] Url /packages/url label Jan 10, 2023
@danielbachhuber danielbachhuber changed the title Ignore malformed url params in get-query-args function Avoid erroring when getQueryArgs processes a malformed URL Jan 10, 2023
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

Looks good to me, @kozer ! Flagging for a second review from @tyxla or @ntsekouras

Comment on lines 88 to 93
.map( ( kv ) => {
try {
return decodeURIComponent( kv );
} catch ( uriComponentError ) {}
return kv;
} );
Copy link
Member

Choose a reason for hiding this comment

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

As @ntsekouras also brought up, we should be able to use safeDecodeURIComponent() here since it does exactly what we're aiming for.

Suggested change
.map( ( kv ) => {
try {
return decodeURIComponent( kv );
} catch ( uriComponentError ) {}
return kv;
} );
.map( safeDecodeURIComponent );

Note that we'll need to import it in this module.

packages/url/src/test/index.js Show resolved Hide resolved
Copy link
Member

@tyxla tyxla left a comment

Choose a reason for hiding this comment

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

Works well and the code looks good. 👍

I've left a minor optional suggestion.

Thanks, @kozer! 🙌

packages/url/src/get-query-args.js Outdated Show resolved Hide resolved
const url = 'https://andalouses.example/beach?foo=bar&baz=%E0%A4%A';

expect( getQueryArgs( url ) ).toEqual( {
foo: 'bar',
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for taking care of that, @kozer, looks great 🙌

@danielbachhuber danielbachhuber merged commit 88ecd2d into WordPress:trunk Jan 13, 2023
@github-actions github-actions bot added this to the Gutenberg 15.1 milestone Jan 13, 2023
@danielbachhuber
Copy link
Member

@tyxla In Managing Packages, I see:

Publishing WordPress packages to npm is automated by synchronizing it with the bi-weekly Gutenberg plugin RC1 release.

When will this happen next for @wordpress/url? Was it supposed to happen a couple days ago?

image

@tyxla
Copy link
Member

tyxla commented Jan 16, 2023

They're usually every 2 weeks, so you can expect them within today and the next couple of days.

@juanmaguitar juanmaguitar added the [Type] Bug An existing feature does not function as intended label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Url /packages/url [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants