REST HTTP Response Code for Unauthorized Resource Access

httprestSecurity

Suppose user A creates a private resource at, for example, /books/somebooktitle

If user B attempts to access the resource at /books/somebooktitle what code should be returned?

HTTP403: Permission denied seems obvious BUT, does this not leak information to user B that someone has created a book and named that book with the title "somebooktitle"? That's two pieces of information that user B shouldn't technically be entitled to.

HTTP404: hides the fact that the book exists.

Is there best practice?

Best Answer

There is no best practice for precisely the reason you cited in your question. It becomes a judgement call based on the kind of information you might expose. If hinting that "somebooktitle" might leak security-sensitive information, or violate a copyright or other kind a law, then go with a 404 Not Found response. You also have the option of redirecting the user to a general purpose search page that results in a 200 OK response. Which kind of response you choose will be driven by security, legal and business considerations. There is no one-size-fits-all solution here.