I'm working on an application which allows a moderator to edit information of user. So, at the moment, I have URL's like


I'm a bit worried here, as I'm directly exposing the users table primary key (ID) from the database. I simply take the ID from the URLs (for ex: 1 and 2 from above URLs), query the database with the ID and get user information (of course I am sanitizing the input - ID from URL).

Please note that I'm validating every request to check if moderator has access to edit that user.

Is what I'm doing safe? If not, how should I be doing it?

I can think of one alternative i.e. have a separate column for users table with 25 character key and use the keys in URLs and query database with those keys.


  • What difference does it make? (Since key is exposed now)
  • Querying by primary key yields result faster than other columns

6 Answers 6


The only piece of information that you could hope to "hide" is the sequence: since a database will allocate primary key values with a counter, people who see they key can make a guess as to when the corresponding user account was created. Apart from that, there is no other information that any obscuring scheme may actually hide. The attacker already knows that distinct users are referenced through distinct keys, and that's the extent of the semantics of the database "primary key".

Therefore, unless you consider that the account creation order is private information that needs to be hidden, I'd say that your obscuring scheme is pointless.

  • 23
    It may still be desirable to prevent end users from enumerating all of the records that they have access to. You may want to support viewing any single user's profile, but discourage mass automated scraping of data from many users' profiles. If you use integers from 0 to n then it's trivial to download every user's profile.
    – Rag
    Commented Apr 23, 2014 at 0:19
  • 4
    If you want to use user IDs in your URL, but want to obscure the sequence, you might be able to use a guid. would also improve scaling options because it's basically impossible to run out of possible options (compared to, say, an int or even a long).
    – Nzall
    Commented Apr 23, 2014 at 11:24
  • 14
    You would also reveal the number of IDs (someone keeps reading until they find the lowest non-existent ID) and the current rate of creation (someone creates one, then a set time later finds the new maximum). This might be of interest in the case of user accounts, as eg. it gives clues as to the financial viability of your application to a competitor. Commented Apr 23, 2014 at 12:42

One easy way would be to use the method that youtube and other websites use. This is sqids (formerly hashids) - https://sqids.org.

With this method you can give links like: http://www.example.org/user/fce7db/edit while fce7db would equal to a number e.g.: 12

This has the advantage of performance in contrary to generating another random hash in the database, because you only have to translate it back once and then you can search in the database by the original primary key as you did before.

To make it more difficult for users to randomly find other IDs you can use a secret salt for one and a minimum length to prevent “brute-forcing” them.

By having one secret salt people can still interchange the URLs in cases like http://www.example.com/image/c8yDa .

  • You can create a salt per user to make it more difficult to decypher.
    – SPRBRN
    Commented Apr 23, 2014 at 7:58
  • Never, ever use the same salt for two different records; that is doing it wrong. In security, if one is going to do it wrong, it is better to not do it at all, as a false sense of security will arise.
    – BryanH
    Commented Apr 23, 2014 at 16:26
  • 1
    This may be an incredibly ignorant question, and I apologize if it is, but since this is run with JavaScript (and is thus client side), couldn't an attacker just View Source and check for what's passed into the hash function? Or at least see the salt, if only decryption is happening?
    – asteri
    Commented Apr 23, 2014 at 17:51
  • You'd generate the hashids server side and send them to the client. As far as the client is concerned it only knows the hashid, with the salt safely managed on the server. There's implementations for many languages/runtimes on the site.
    – afrazier
    Commented Apr 23, 2014 at 18:20
  • @afrazier Ah, so the JavaScript is just an example.
    – asteri
    Commented Apr 23, 2014 at 18:39

No. One way or another, you need to identify the specific record with a unique identifier in the URL. That can be the primary key or something else. If you need to hide the order or sequence you can use a second column with a random and unique string like Z2wDKo0ubb1D2VngFh4N.

If you want more security, use SSL. This doesn't prevent the user from seeing the URL and the parameters though, as @eggyal noted.

Using SSL, the URL paramaters are encrypted and prevent eavesdropping. And yes we know by now that SSL is flawed, not really that secure, but it gives more security than not using it. But do not use the URL for passwords! Use POST for logging in and submitting data, GET for retrieving information, like always.

See https://stackoverflow.com/questions/499591/are-https-urls-encrypted

Reasons not to use secret information in a URL: DNS requests are probably not encrypted (but as @tgies notes in the comments, the request portion is not included so no ID here), browser history and server logs.

  • 10
    SSL only hides the requested URI from eavesdroppers, not from the user (whom I had understood would be the source of any attack).
    – eggyal
    Commented Apr 22, 2014 at 19:28
  • 1
    The request portion of the URI does not appear in any DNS query.
    – tgies
    Commented Apr 22, 2014 at 21:43
  • "yes we know by now that SSL is flawed" - can you support that statement with some evidence or something ? As someone who is NOT in the security world, I find that to be very.... worrying. Thanks. Commented Jan 11, 2017 at 8:30

I've created a few systems that either do or don't use the primary key (or other identifier) in the URL. Which we use is totally dependant on the harm-factor -- for instance, for a site that is data driven read only, we use the primary key in the URL; we don't care if someone goes through the products sequentially.

For systems that have security requirements, instead of using a sequential predictable id, we have used one of two schemes:

  1. We keep the key information the browser's session. Simple, straightforward and if our server isn't secure, we have bigger problems.
  2. We have generated a random number (and checked for uniqueness) either when the record is created OR for the session only; and used that random number within the URL.

Good luck,



Yes, you should obfuscate those IDs, since you're leaking information.

If your application is perfectly secure, the only information (ab)users may learn is a guesstimate of the number of users through the use of something called the Doomsday Argument. Also, if they learn the IDs of several users, they'll know who signed up first and can guess how far apart that happened. And they can make educated guesses of the IDs of other users as well.

If your application is perfectly secure.

If it isn't, and you should always assume it may not be, you've given out useful information. If a user ID was just a random number, an attacker could not easily figure out another valid ID. With the (mostly consecutive) numbers usually given out by an automatic primary key, it's trivial.

You could either change the primary key, or add a secondary key, if you want to retain the ease of use of small numbers for internal use. This new key should typically be a GUID.

‡: Of course it's an xkcd link.


You don't provide enough information about the application to know whether you should obscure it or not. As such, this part of your question isn't answerable. However, if you have to ask, then the answer is probably yes, and even if it isn't then going above and beyond the call of duty provides more protection than the alternative.

Rather than hiding or obscuring the primary key, consider changing it. There's no need for it to be sequential. When you create a new user, make a random 32 bit number, do a quick select to make sure it's not in use, and use it as your primary key.

If anyone is editing a user, they won't be able to gain any information from the user number, and trying numbers immediately adjacent to the number they currently have access to is likely to yield a non-existing record.

The overhead/burden of this system is minimal. It requires one additional select during account creation, which is likely rare, and doesn't impose any additional overhead afterwords - you're still providing the primary key in the URL, so DB searches are still fast, but the key has no information.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .