In our web application we want to randomize the record IDs. The reason is because we want to hide how many entries there are in the DB already and we have unlisted things. In case IDs would be simple incremental numbers it would be easy to guess the IDs of unlisted things.
As I see it there are three ways to do this:
Algorithm:
"#{id}--#{page_title}
), a UUID would shift this comment all the way to the rightAlgorithm:
nextval
(atomic!)Suggested by @emboss
Suggested by @viktor tron
A second ID for all things that occur in an URL only used to find the record. Internally normal IDs are used (for joins etc).
I think I'll use the third option. Or are there more arguments against it? Is there an even better solution? We use Ruby on Rails 3.x and PostgreSQL 9.x.
Edit: Unlisted does not mean private! It is meant like unlisted videos on YouTube. They are normal videos that just aren't listed in searches or the uploader's profile. So you can't really find them (without trying every possible ID), but everyone who knows the URL can access them. Of course a user that makes something unlisted and sends the link to someone else has to be aware that it might not stay unknown (the URL may be passed on and through linking might end up in a search engine).
We also have another option to make things private. These are two different things. (I see that assuming that everyone knows what "unlisted" means was a mistake.)
Note: this answers the initial version of the question, from which it was not obvious that this is not a replacement for authorization logic.
You think the problem is: users can guess ids of "unlisted" things and use them.
Actual problem is: users can access things without authorization.
Put authorization logic in place, allow user access only to items that he can legitimately access and forbid everything else.
hide how many entries there are in the DB
I think there's no shame in being small, if this is the reason. Anyway, you can start your sequence from 100000 or increment it by N or employ another similar trick :)
I suggest a totally different way: simply do not show record IDs to users. You do not need to. Use another form of identification for url.
Since you say you want pretty urls, you could simply use a slugger/permalink gem, like https://github.com/norman/friendly_id
friendly_id's default slug generator offers functionality to check slug strings for uniqueness and, if necessary, appends a sequence to guarantee it.
Seriously, leave IDs alone :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With