1

Lets say I have a few fields that I don't want modified. In my case my users can utilize a PATCH request which invokes this method:

Ad.findByIdAndUpdate(req.params.id, req.body, {new: true})

Technically I can "manually" filter the "req.body" object and remove everything that should not be updated even if they specifically send those fields in the request, but is there a better way, perhaps, adding a "protected" flag in the relevant schema something like this:

title: {
    type: String,
    required: true,
    protected: true
}
Adam
  • 3,829
  • 1
  • 21
  • 31
luiquao
  • 1,094
  • 4
  • 21
  • 46

1 Answers1

0

Some concerns:

  • Wouldn't you validate user's input and throw on errors? Is it still a correct request, if the user sends arbitrary data?
  • If you don't validate, how do you sanitize?
  • If you don't validate, why don't you filter the request?

I'd strongly recommend doing validation (e.g. with some json-schema-library) and perform sanitization on the values afterwards. On top of that, you can use Monogram to disallow having some properties being updated. In this case, a request with unexpected params in the payload, would throw and return some error (e.g. BAD REQUEST)

Figedi
  • 383
  • 1
  • 2
  • 11