CakePHP: Cross-Site Request Forgery (CSRF) Test

CakePHP: Cross-Site Request Forgery (CSRF) Test
Social sharing

Cross-site request forgery (CSRF), also known as one-click attack or session riding is a type of malicious exploit of a website where unauthorized commands are transmitted from a user that the website trusts. Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user’s browser.


How It Works?

There are various techniques used for CSRF attacks. Let’s take the example of changing an email address of a logged-in user, by using a form with the required fields to change.


<form name="myform" action="" method="POST">
<input name="email" type="text" value="[email protected]" />
<input name="name" type="text" value="john" />
<input type="submit" value="Save"/>

Here the victim has to submit the form.


<body onload="document.forms[0].submit()">
<form name="myform" action="" method="POST">
<input  name="email"  type="hidden" value="[email protected]"/>
<input  type="hidden" name="name"  value="john"/>
<input type="submit" value="Save"/>

Here, the form gets automatically submitted when loaded in the browser. The attacker can now change the authenticated users of “” email address easily.

Note: it is not easy for attackers to use ajax call to perform the above operation since most of the modern browsers have same-origin policy restrictions.

How To Prevent Such Attacks:

Let’s take the same example as above & prevent the CSRF attack.

Here are the Steps to be followed:


Generate a unique token (256 char string) for each session, i.e. for every user login to the system, there should be a unique token associated. Store the token in the session.

                                                $tokn = $this->Format->genRandomStringCustom(25);
                                                $_SESSION['CSRFTOKEN'] = $tokn;

For CakePHP, do this in App Controller.


Add this token to every form while doing any operations such as saving your profile information like email, password, first name or the last name. For this follow the below guidelines:

Take profile.ctp for our example and add the below code to profile.ctp:

Add a hidden field to the form.


<form  id=”formId” method="POST" action="">
<input id=”csrftoken” type="hidden"  value=” ” name=”csrftoken"  />
<input value="Save" type="button"  onclick=”addCsrfToken(‘formId’)”/>

Set the ”csrftokenl” value at the run when you submit the form.

function addCsrfToken(formid){
                        $('.csrftoken).val(‘<?php echo $_SESSION['CSRFTOKEN']; ?>’);


Check the csrf token in the respective controller & action for every successful operation.

Let’s take the user’s controller and profile action:

function profile(){
            if($_SESSION['CSRFTOKEN'] === “requested token from the form”){ //$this->data[‘csrftoken’];
            //perform the rest of the operation here
                        //unauthorized access, do not save anything

Over To You, Now!

These kind of one-click attacks are quickly becoming the new modes of Cyber attacks. It would be nice if you can share your experiences related to the same at [email protected].

Let’s make the web a safer place for everyone!

Your recently viewed posts:

Jayadev Das - Post Author

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘About Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

    Contact Us

    We’d love to help & work with you

    When do you want to start ?

    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box