IDs in Resource URLs – Database ID or Per-User ID?

httpresturl

For REST interfaces or webapps with nice-looking URLs, I constantly ask myself on what numbers to use if we want to refer to specific resources/pages.

A typical approach seems to be to use the unique database ID of a row representing the resource as an index like http://www.mypage.com/questions/4/answers.

Using the database ID would be straightforward, however, it leaks information for resources of other users. I could also imagine that one might generate custom IDs that are only unique to one user to avoid this leaking.

It there any best practice/advice available?

Best Answer

If your primary concern is preventing user X from accessing a resource A that's unique to user Y, then per-user ids don't solve this. What you really need is authentication, i.e. a secure way of verifying that a request for resource A is actually being made by user Y, not just some other user pretending to by Y.

If you don't have authentication, then per-user ids are merely security through obscurity. As soon as user X finds out what user Y's magic id number for resource A is, he can get resource A. If you do have authentication, then per-user ids are unnecessary, as your server can simply refuse to send resource A to anyone not authenticated as user Y.

The only time per-user ids would help is if the id itself is secret information, or indirectly exposes secret information. I can't really think of a scenario where this would actually be the case, but just to be safe I would generate GUIDs rather than incrementing a sequence number, since the sequence number reveals a little information (the order the resources were created in) while a properly-generated GUID reveals essentially nothing.

tl;dr I don't know of any reason why a per-user id would be beneficial, unless for some inexplicable reason you are unable to implement proper authentication or GUID generation.

Related Topic