views:

152

answers:

5

I have many lists e.g. a todo list, a shopping list etc. on my web page. I am using AJAX to add or delete the items. For example, for a todo list my HTML is like:

<tr id="todo_userttypea_23"> <td>name</td><td>Delete</td></tr>

Note if the users press delete then I am deleting that row.

I get the id of the row and then break it to find which operation to perform and which id to delete.

But the I have found that if I use firebug then I can change the id dynamically to any number and I have found that it is possible to delete any id, even if does not belong to that user, by editing the HTML.

What should I do to prevent this?

+4  A: 

Perhaps you should check to see, on a delete ajax request, whether the user doing the deletion is permitted to delete the item.

Greg Hewgill
+6  A: 

I think you are confusing Javascript functionality with security. If your user is not allowed to delete AuntMarysShoppingList#32, then the server shouldn't let him no matter what the client requests.

You can obfuscate your JS code, but on some level, you have to assume your user is an honest broker, and isn't going to go out of their way to delete something (the hard way, by hacking JS) that they have the rights to delete anyway.

Josh Pearce
+5  A: 

You need to add Authorization checking on the server side. Whether the request is ajax or otherwise is irrelevant.

Andy Gaskell
+6  A: 

The main principle is "never trust incoming data". Any data you get sent from outside can be manipulated - Parameters, Headers, Referers, everything. A good and safe system does not trust any of these.

If you have multiple users working on the same data base, you will probably need to implement an authorization system that defines clearly who is allowed to do what to which record.

That is usually done using a session-based login system of some sort, based on one of the scripting languages like PHP, Ruby, ASP or Perl. There are pre-built solutions available.

Pekka
Actually i was thinking that user can't change the id of the elements so i made the functions like that But now i have realised that data can be changed so firstly i will authorize the delete functions and then i will move towards other functions
Mirage
ONe more questions is it possible to edit the data being Posted , i mean stop the request and then chnage the data midway and then post
Mirage
I'm not sure what you mean but in general, when the data has arrived in your script (where you fetch it using $_GET[xyz] in PHP for example), from that point it can *not* be changed anymore.
Pekka
I mean to say , when you click button then data is posted and firebug console shows which variables are you posting , then there is it possible by any method that you edit the posted variables and then submit the form to scripti mean to intercept the data before submitting to php script
Mirage
I suppose in theory, it would be possible to intercept and alter the data at any point it goes through: Your computer, your router, and the various points along the route to the server. "Man in the middle" attacks impersonating a start or end point are of that category. But I don't think that should be of concern right now. What counts right now is securing the server side.
Pekka
I think you are right , I am now encoding the userid and sending it with every post and then on server side i can match that
Mirage
That doesn't sound safe to me yet. Does the user have to log in? Does he get a unique session ID when logging in?
Pekka
+1  A: 

You basically need something like this on the server-side:

if (itemBelongsToUser(itemId, currentUserId)) {
    deleteItem(itemId);
}
David Thibault