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

/users?filter={"where": {"name": {"like": "%ad%"} }} does not work on LB4 #4015

Closed
iury123 opened this issue Oct 28, 2019 · 6 comments
Closed

Comments

@iury123
Copy link

iury123 commented Oct 28, 2019

Steps to reproduce

Current Behavior

Expected Behavior

Link to reproduction sandbox

Additional information

Related Issues

See Reporting Issues for more tips on writing good issues

@iury123 iury123 added the bug label Oct 28, 2019
@iury123
Copy link
Author

iury123 commented Oct 28, 2019

/users?filter={"where": {"name": {"like": "%ad%"} }} does not work because %ad% is an unknown character when the url is decoded. If I provide anything else such as %Jo%, it works. Only %ad% is not working. Can anybody help me to figure out the solution for it? I am working on loopback 4.

@iury123 iury123 changed the title /users?filter={"where": {"name": {"like": "%ad%"} }} does not work because of %ad% is a unknown character, if I provider anything else such as %Jo% works. Only %ad% is not working. Can anybody help me to figure out the solution for it? /users?filter={"where": {"name": {"like": "%ad%"} }} does not work on LB4 Oct 28, 2019
@jannyHou
Copy link
Contributor

jannyHou commented Oct 28, 2019

reproduced the error, looking. words that begin with the first few letters(abcdef) are failing too.
thrown from the parseJson function

@iury123
Copy link
Author

iury123 commented Oct 28, 2019

@jannyHou Try users?filter={"where": {"name": {"like": "%25admin%25"} }}. It has worked here.

@jannyHou
Copy link
Contributor

jannyHou commented Oct 28, 2019

@iury123 👍

Looks like JSON.parse() cannot parse encoded query string, like "%7B%22where%22:%20%7B%22name%22:%20%7B%22like%22:%20%22%ad%25%22%7D%20%7D%7D".
Tried other parsers like 'query-string' and
querystring, they are able to decode the string.

For query like /users?filter={"where": {"name": {"like": "%ad%"} }} , when the request comes in, the filter in query is encoded from the beginning before parsing happens.
But for normal queries like /users?filter={"where": {"name": {"like": "%25admin%25"} }} the filter query string is a decoded one "{"where": {"name": {"like": "%admin%"} }}"

I am not sure why there is a difference, due to the limit of #3770 I tried the endpoints with postman. Should we switch to other parsers like querystring in @loopback/rest?

cc @strongloop/loopback-maintainers thought?

@hacksparrow
Copy link
Contributor

hacksparrow commented Oct 29, 2019

Whatever is parsing %admin% is reading the first three characters and interpreting it as the unprintable character represented by AD in hexadecimal (let's use ▪︎ to represent it), so the string becomes ▪︎min% from %admin%.

I don't think we should change the parser. Interpreting encoding and intend is a very complex task, we never know what new problems a new parser might introduce. Let's keep the current one but make the instructions clear: Kindly encode all special characters, like how @iury123 has done {"where": {"name": {"like": "%25admin%25"} }}.

words that begin with the first few letters(abcdef) are failing too.

They belong to the hexadecimal set, that's why. %jon%, %zebra, etc., will work because there is no confusion whether the input should be read as hex or not.

@bajtos bajtos added question and removed bug labels Oct 31, 2019
@bajtos
Copy link
Member

bajtos commented Oct 31, 2019

You need to url-encode values in the query string. This is usually done for you automatically when using tools like request or creating the query string via url.format.

Here are the instructions for creating the URL manually:

  1. Convert your filter object to JSON via JSON.stringify
  2. Call encodeURIComponent to escape any special characters
  3. Add the value to your query string.
const filter = {where: {name: {like: '%ad%'}}};
const search = `filter=${encodeURIComponent(JSON.stringify(filter))}`;
const url = `http://localhost:3000/products?${search}`;

@bajtos bajtos closed this as completed Oct 31, 2019
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

4 participants