0

How can I protect the following code from CSRF?

$.post("lib/newEmployee.php",{employee_id: employee_id, employee_name: employee_name},
 function (output){
       //code                        
});

And here is my form (it is submitted after being validated with jquery validate):

<form id="new-employee" class="inline-input" action="#" method="post"/>
     <div class="employee-container">
         <div class="field-box">
             <input type="text" class="input-block-level" id="employee_id" name="employee_id" autocomplete="off">
         </div>
         <div class="field-box">
             <input type="text" class="input-block-level" id="employee_name" name="employee_name" autocomplete="off">
         </div>
         <div class="field-box">
             <button type="submit" class="btn btn-success btn-block">New Employee></button>
         </div>
     </div>
</form>

1 Answer 1

1

The simplest way is to check for the X-Requested-With header being set. An alternative is to check the Origin for supported browsers.

Alternatively, check out the other solutions on the Cross-Site Request Forgery (CSRF) Prevention Cheat Sheet.

Double Submit Cookies might be appropriate here as you will not require server side state, although the Synchronizer Token Pattern is the most secure approach as the token value is generated server side.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .