Search code examples
htmldatabase-designsecuritydatamodel

Why should primary keys of DB not be shown in html code, e.g. in select fields?


anywhere I read that values in select boxes (or anything else in the html code) should not be the primary key of the database table. For example:

<select>
       <option value="1">Value 1</option>
       <option value="2">Value 2</option>
</select>

In the database there are lookup tables with these values as primary key (1, 2, 3,....). So the data from the select box I store in a table which references this lookup table is a number like 1, 2, 3.... (as the value of the options fields). I read to better not use the same values in html and as key due to security reasons, but what's the matter with that? I don't understand why this should be a security reason?


Solution

  • Sounds like security-through-obscurity, aka no security at all to me.

    A good primary key in a database is purely for uniqueness in the system and shouldn't be related to the meaning of the data. If the primary key was related to the data (say people's social security numbers, stuff like that) then you've got a security issue in exposing the keys, as they are exposing information that could be used maliciously. In that case, whilst you could argue that the best approach from a technical point of view might be to change the application to stop it using those meaningful keys, it may be a more palatable approach to map the keys to some other meaningless key to overcome the issue.

    Another scenario that springs to mind where exposing the keys might be interpreted as a security issue is where inadequate authentication and authorisation is in place for writable data in your application/data layer, allowing someone with knowledge of those keys to interfere with the data in the application. Again, securing the system is the better approach.

    Aside from security, I can't think of a specific issue if the keys really do identify the data being interacted with and your application is looking up the keys when it generates the page.