-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Avoid erroring when getQueryArgs processes a malformed URL #45561
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
👋 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. |
@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', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
qs
library- 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!
There was a problem hiding this comment.
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 usingqs
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 🙌
There was a problem hiding this 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
packages/url/src/get-query-args.js
Outdated
.map( ( kv ) => { | ||
try { | ||
return decodeURIComponent( kv ); | ||
} catch ( uriComponentError ) {} | ||
return kv; | ||
} ); |
There was a problem hiding this comment.
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.
.map( ( kv ) => { | |
try { | |
return decodeURIComponent( kv ); | |
} catch ( uriComponentError ) {} | |
return kv; | |
} ); | |
.map( safeDecodeURIComponent ); |
Note that we'll need to import it in this module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const url = 'https://andalouses.example/beach?foo=bar&baz=%E0%A4%A'; | ||
|
||
expect( getQueryArgs( url ) ).toEqual( { | ||
foo: 'bar', |
There was a problem hiding this comment.
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 🙌
Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com>
@tyxla In Managing Packages, I see:
When will this happen next for |
They're usually every 2 weeks, so you can expect them within today and the next couple of days. |
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
http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A
usinggetQueryArgs('http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A')
After
http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A
usinggetQueryArgs('http://calypso.localhost:3000/start/domains?ref=calypso-selector&source=my-home&siteSlug=%E0%A4%A')
Screenshots or screencast