Skip to content

Avoid erroring when getQueryArgs processes a malformed URL - #45561

Merged
danielbachhuber merged 8 commits into
WordPress:trunkfrom
kozer:fix/get_query_args_ignore_malformed
Jan 13, 2023
Merged

danielbachhuber merged 8 commits into
WordPress:trunkfrom
kozer:fix/get_query_args_ignore_malformed

Conversation

@kozer

@kozer kozer commented Nov 4, 2022 •

Copy link
Copy Markdown
Contributor

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

codesandbox Bot commented Nov 4, 2022

Copy link
Copy Markdown

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

github-actions Bot commented Nov 4, 2022

Copy link
Copy Markdown

👋 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

kozer commented Jan 3, 2023

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@danielbachhuber danielbachhuber left a comment

Copy link
Copy Markdown
Member

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 thread packages/url/src/get-query-args.js Outdated
Comment on lines +88 to +93
.map( ( kv ) => {
try {
return decodeURIComponent( kv );
} catch ( uriComponentError ) {}
return kv;
} );

Copy link
Copy Markdown
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.

Comment thread packages/url/src/test/index.js Resolved

@tyxla tyxla left a comment

Copy link
Copy Markdown
Member

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! 🙌

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

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

Copy link
Copy Markdown
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 🙌

Co-authored-by: Marin Atanasov <8436925+tyxla@users.noreply.github.com>
@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
Copy Markdown
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

tyxla commented Jan 16, 2023

Copy link
Copy Markdown
Member

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
konnen916 added a commit to konnen916/gutenberg that referenced this pull request Aug 8, 2026
normalizePath decoded each query parameter with bare decodeURIComponent,
which throws a URIError when a value contains a malformed percent
sequence such as `?search=50%off`.

This is the same failure that was fixed for getQueryArgs in WordPress#45561, by
switching to the package's own safeDecodeURIComponent. normalizePath was
not updated at the time. Well formed input is unaffected, and the
function stays order stable, which is what it exists for.

It matters past the function itself because the api-fetch preloading
middleware calls normalizePath on every preload key and on every request
path, so the throw takes out the middleware rather than failing a single
request.
konnen916 added a commit to konnen916/gutenberg that referenced this pull request Aug 14, 2026
normalizePath decoded each query parameter with bare decodeURIComponent,
which throws a URIError when a value contains a malformed percent
sequence such as `?search=50%off`.

This is the same failure that was fixed for getQueryArgs in WordPress#45561, by
switching to the package's own safeDecodeURIComponent. normalizePath was
not updated at the time. Well formed input is unaffected, and the
function stays order stable, which is what it exists for.

It matters past the function itself because the api-fetch preloading
middleware calls normalizePath on every preload key and on every request
path, so the throw takes out the middleware rather than failing a single
request.
hbhalodia added a commit that referenced this pull request Aug 14, 2026
)

normalizePath decoded each query parameter with bare decodeURIComponent,
which throws a URIError when a value contains a malformed percent
sequence such as `?search=50%off`.

This is the same failure that was fixed for getQueryArgs in #45561, by
switching to the package's own safeDecodeURIComponent. normalizePath was
not updated at the time. Well formed input is unaffected, and the
function stays order stable, which is what it exists for.

It matters past the function itself because the api-fetch preloading
middleware calls normalizePath on every preload key and on every request
path, so the throw takes out the middleware rather than failing a single
request.

Co-authored-by: konnen916 <300166086+konnen916@users.noreply.github.com>
Co-authored-by: Hit Bhalodia <58802366+hbhalodia@users.noreply.github.com>
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