feathersjs-ecosystem / feathers-hooks-common

Useful hooks for use with FeathersJS services.
https://hooks-common.feathersjs.com
MIT License
193 stars 90 forks source link

paramsFromClient pollutes context by default #730

Open FossPrime opened 1 year ago

FossPrime commented 1 year ago

https://github.com/feathersjs-ecosystem/feathers-hooks-common/blob/15ddaa6f12df8410bdfd2461ac02d46cbdd086ed/src/hooks/params-from-client.ts#L22

While whitelisting is a good security feature, it is the adapter's job to complain about invalid query parameters, not ours.

It makes it difficult to compose hooks with this as it damages the context if used at app level/around/provider and also in services.

Proposal:

   const foreignParamCount = 0
    // in an Object.keys(ctx.params.query?.$client) loop, foreignParamCount++
    if (foreignParamCount === 0) {
      delete ctx.params.query.$client
    }

The alternative is to dump repetitive code like this in all over userland hooks:

delete ctx.params.query.$client.sudo
if (Object.keys(ctx.params.query.$client).length === 0) {
  delete ctx.params.query.$client
}