0

I have a webpage in which the user is awarded X points on clicking a button. The button sends a AJAX request(JQuery) to a PHP file which then awards the points. It uses POST.

As its client side, the php file, parameters are visible to the user.

Can the user automate this process by making a form with the same fields and sending the request ?

How can I avoid this type of CSRF ? Even session authentication is not useful.

Kartik kar2905
  • 56
  • 1
  • 10
  • 1
    http://stackoverflow.com/questions/3315914/is-this-sufficient-to-protect-against-a-csrf-for-an-ajax-driven-application – manugupt1 Feb 18 '11 at 12:11

2 Answers2

2

You should handle that on the server-side, If you really want to prevent multi-vote or prevent the same people from voting several time on the same subject. This is why real votes always use authenticated users and never anonymous votes.

By checking the request is really a XmlHttpRequest (with @Shaun Hare response code or with the linked stackoverflow question in your questions comments) you will eventually block some of the CSRF but you won't prevent a repost from the user, using tools like LiveHttpHeaders 'replay' and such. Everything coming from the client side can be forged, everything.

edit* if it's not a voting system as you commented, the problem is teh same, you nedd 'something' to know if the user is doing this action for the first time, or if he can still do this action. There's a lot of different things available.

You can set a token on your page, use that token in the ajax requests, and invalidate this token for later usage server side. This is one way. the problem is where to store these tokens server-side (sessions, caches, etc)

Another way is to check on the server side the situation is still a valid situation (for example a request asking to update 'something' should maybe handle a hash/marker/timestamp that you can verify with current server side state.

This is a very generic question, solutions depends on the reality of the 'performed action'.

regilero
  • 27,883
  • 6
  • 54
  • 94
  • Okay. Its not a voting system. Its like you do a action and then a AJAX call is made and points are awarded. Is there a foolproof way then ? – Kartik kar2905 Feb 18 '11 at 15:11
-1

Check it is an ajax call in php by checking

$_SERVER['HTTP_X_REQUESTED_WITH']

Shaun Hare
  • 3,633
  • 2
  • 21
  • 35