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

White list not work on advanced filter object #119

Open
ngdnghia28 opened this issue Dec 30, 2020 · 2 comments
Open

White list not work on advanced filter object #119

ngdnghia28 opened this issue Dec 30, 2020 · 2 comments

Comments

@ngdnghia28
Copy link

import * as aqp from 'api-query-params';

const query = aqp('filter={"$or":[{"key1":"value1"},{"key2":"value2"}]}', {
    whitelist: ['key1']
});
console.log(JSON.stringify(query, null, 2));

Will print out:

{
  "filter": {
    "$or": [
      {
        "key1": "value1"
      },
      {
        "key2": "value2"
      }
    ]
  }
}

Expected result:

{
  "filter": {
    "$or": [
      {
        "key1": "value1"
      }
    ]
  }
}
@loris
Copy link
Owner

loris commented Oct 26, 2021

Hi @ngdnghia28 I'm not so sure how to handle your request safely. As the filter query param is not traversed (because it's structure is totally unknown to the library) but simply JSON parsed. It can be tricky to exclude/include keys since the keys position can be deep in the JSON tree (if you combine $and, $or, $elemMatch, etc operators)
Any idea?

@molaeiali
Copy link

Isn't it possible to recursively traverse the object, and if a key is not started with a $ sign, and is not in the whitelist array, then remove it? I'm not sure of other edge cases it might have, but it seems possible

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

No branches or pull requests

3 participants