I have a restAPI which allows for users to submit data to be saved in my database. When data is submitted their userID will be grabbed from the request header and added to the document
In my collection (mongoDB) there is the normal _id
field and a userID
field, which references the user.
My question is in the scenario that a malicious user submits a PUT or DELETE request to a document that does not belong to them what is the best way to handle it.
-
I pull the prediction from the DB just using the submitted
_id
, check theuserID
field against the requestinguserID
and issue a 403 response if they do not match. -
I query the database by both
_id
anduserID
, so it would not be found if the document does not belong to the requesting user. I would then issue a 404 response just as if they had submitted an id that does not exist.
Both choices achieve the same goal, which is to prevent a user from editing or deleting a resource that does not belong to them, but which is "better"?
2
Answers
404 is for "not found", which is clearly not the case.
Use 403.
Quoting from Wikipedia:
I would return
403
if user A has permission to view a resource that belongs to user B (as they are already aware of the existence of this resource). Otherwise, I would return404
for privacy reasons. This is in line with the RFC 9110 standard: