开发者

How to secure this PHP form

I have a form for editing users. The user ID is passed to the client (in a hidden field) so that I know which user to update when the form is posted back to the server. My question is, how can I guard against users changing the ID in the DOM, thereby updating a record to which they should not have access?

The only ways I can think of are:

  1. Save the user ID in the session. (Painful).
  2. Run a salted hash on the user ID (and perhaps other form elements) and include it also as a hidden form element. (Not particularly secure?)

Are there other approaches?

Thanks!

EDIT: Hey, some great responses coming in. Note that the logged in user and the user being edited may be two different users, e.g. 开发者_JS百科a Manager is editing a Staff record.


The best way to do this is to just check after submission if the user has the right to edit that user.

Don't pollute the session with this data, because it can get messy, for example when a user opens the same page multiple times.


It depends somewhat on the conditions under which the user is allowed to edit it.

At the heart of it, it comes down to:

  1. Authenticate the user
  2. Check if the user is authorized to make that change

Authenticating the user is usually a case of "Do the username and password match?" or "Is there an active session with a logged in user associated with it?"

Authorization depends on your business logic. It might be "Is the logged in user the same as the user being edited?" or "Does the user being edited have a manger field containing the id of the logged in user?" and so on.

In the first case, storing the user id in the session shouldn't be painful. In the second case, you just do a database lookup as one of the first things you do in the script.


Why are you relying on a hidden field for knowing which record to update? If the user is logged in you should already have the user_id of him with you on the session. So you can just find which record to update by finding which user is logged in.


As mentioned, the fastest and painless way to sort this would be to stick the USER_ID in the session, period.

Comments saying that you "pollute" session with that information are plainly uneducated, ignore them.

The other thing I noticed in comments is the "check if the user has the rights to edit the entry" which implies there's some sort of hierarchical system in place, which seems not to be true.

Alternative to session storage would be, as you assumed already, obfuscating the USER_ID value in the hidden field somehow. You could either encrypt it, or instead of integer ID - you could use GUIDs but that has implications of its own, tho it makes it incredibly hard for someone to "guess" the correct GUID to mess around with the records.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜