moleculer-db icon indicating copy to clipboard operation
moleculer-db copied to clipboard

sanitizeParams does not filter out non public fields

Open thoaif opened this issue 5 years ago • 0 comments

Problem / motivation

sanitizeParams function does not filter out fields inside query params. This presents a challenge in "hiding" fields in from public access, since an attacker can still query using those fields and gain privileged information.

I am not exactly sure what intention of "fields" setting is, but I sure do hope no one is using them with the assumption that access is restricted to those fields

Example

Suppose I have the following:

module.exports = {
  name: 'example',

  settings: {
    fields: [ '_id' ],
    entityValidator: {
      hiddenField: { type: 'boolean', default: true },
    },
  }
}

I can still use hiddenField in actions with query param. For example:

call example.list --query '{"hiddenField": false}'

will list out all the fields where hiddenField is false. Ideally, querying in this manner should not work.

Suggested Solution

Inside sanitizeParams function after parsing, one could filter by:

if (p.query) {
  const filteredQuery = Object.fromEntries(
    Object.entries(p.query)
      .filter(([field, val]) => 
         this.settings.fields.includes(field)
       )
   )
p.query = filteredQuery;
}

thoaif avatar May 01 '20 19:05 thoaif