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

URL search params.has method returns incorrect value #7

Open
federico-hv opened this issue Dec 1, 2024 · 1 comment
Open

URL search params.has method returns incorrect value #7

federico-hv opened this issue Dec 1, 2024 · 1 comment

Comments

@federico-hv
Copy link

federico-hv commented Dec 1, 2024

Hi,

I was using Expo and noticed that I was getting a different result when validating a URL on web, nodejs and in my react-native app with whatwg-url-without-unicode. It seems that Expo uses this library as an implementation of URL here https://github.com/expo/expo/blob/sdk-52/packages/expo/src/winter/url.ts. and while digging I realized that there seems to be an issue with the url.searchParams.has method. When trying to match with a second argument which is the value of the search param it returns true even if the value is incorrect. I made a gist here that explains the issue:

https://gist.github.com/federico-hv/8a5459b23867635b66f97e0605e73b57

Essentially the library is matching true for a value that is not the right one -> url.searchParams.has('fruit', 'banana') is true for fruit=apple

@federico-hv
Copy link
Author

The type definition of URLSearchParams from nodejs shows that the has method has an optional second argument called value:

/**
 * Checks if the `URLSearchParams` object contains key-value pair(s) based on `name` and an optional `value` argument.
 *
 * If `value` is provided, returns `true` when name-value pair with
 * same `name` and `value` exists.
 *
 * If `value` is not provided, returns `true` if there is at least one name-value
 * pair whose name is `name`.
 */
has(name: string, value?: string): boolean;

To fix the issue 2 things are needed:

  1. Updating src/URLSearchParams-impl.js line 78 method has will guarantee a check if a value argument is passed
has(name, value) {
    for (const tuple of this._list) {
      if (!value) {
        if (tuple[0] === name) {
          return true;
        }
      } else {
        if (tuple[0] === name && tuple[1] === value) {
          return true;
        }
      }
    }
    return false;
  }
  1. It seems there's a webidl file reference of UseSearchParams which is being loaded too and results in dist/URLSearchParams.js output but since this blueprint contains a URLSearchParams.has method with only the name argument it would have to be updated to receive 2 arguments. The URLSearchParams.has method ends up calling the URLSearchParams-impl.js -> has method and will only call it with one argument if The blueprint is not updated.
Screenshot 2024-12-01 at 2 34 17 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant