tags:

views:

68

answers:

4

Hello,

Many a times i do some weird things while using jquery ajax. I mean, i keep a hidden variable which contains id and then when somebody clicks button, i run a javascript function which passes ajax request along with the id that is contained in hidden field. Is this normal? What if somebody uses firebug or any such tool and changes the javascript function and passes some other ids? It will update and delete other records which may not belong to that user? How do you all handle this?

+9  A: 

You need to secure this server-side, you can't protect it on the client-side, nor should you.

JavaScript is viewable, executable, dynamic, open...it's everything you would want when doing...well, whatever you want with it, which is a very bad thing for security. You need to check the passed id against what the user should have access to on the server when processing the request.

Anything, and I mean anything you do on the client is a deterrent, not a solution, and really there are no effective JavaScript deterrents I've ever seen. Even if you could secure it, I can just open Firebug, Fiddler, Wireshark, Chrome console or one of a dozen other tools to see what the request is ultimately sending anyway.

Nick Craver
So this means that there is no secure way for sending xmlHttpRequests ?
adardesign
@adardesign - *Sending*, sure, SSL for instance...*what* they're sending, no, the client can shove anything in there, and it's *very* easy to change what it's doing in JavaScript. Never trust your input, verify it :)
Nick Craver
+6  A: 

Never trust your users' input: validate the id on the server.

Tomas Lycken
+1  A: 

You should always be checking the input on the server side when the data is submitted. For example if a user was editing their profile on the site, you would not put the profile ID in a hidden variable, you would derive the profile ID based on the users cookie/session when the data was submitted. The key phrase is absolutely never trust the client.

Fosco
Hi Fosco, i got you. And i am doing that only if it's a single record. What if some table is displayed on the page which has 10 records. Say user has permissions to edit those albums since they are his own. Now these albums have their own album ids. How do i handle this? Should i dump all of the album id's in session and while submitting check the album ids? There are many such cases where i need tables. Should i put all those ids on each instance in session? If yes, then when should i put them in session? Just when he logs in? That in essence means i should do unnecessary joins which are...
Ankit Rathod
not required when the user logs in? I hope i am clear. Please let me know if i am unclear.
Ankit Rathod
If you're able to display the albums that are 'his,' then you're able to determine it is 'his' on form submission. You should not need to store anything extra in the session to accomplish this.
Fosco
No, I am not. Since i only store `userid` in session. And when a page is requested i fetch his `userid` from session and then make a join with albums table `select * from album where userid = useridFromSession`. Now when the user submits the form, shall i again query the database to see whether this album is his own?
Ankit Rathod
@Nitesh - If it's not in memory, then yes you should query again...this should be a very fast query :)
Nick Craver
@Nick Craver Thanks.
Ankit Rathod
+1  A: 

You must do server-side validation to ensure that the current user is authorized to perform the action based on the current user and context. As you have noted, anyone with valid credentials could modify the values that are being passed back -- they need not even modify your code, they can simply craft a request containing any sort of values they want if they have the correct cookie information.

tvanfosson