<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gayan Virantha Perera on Medium]]></title>
        <description><![CDATA[Stories by Gayan Virantha Perera on Medium]]></description>
        <link>https://medium.com/@gayanperera160?source=rss-7b3dcc2f8737------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*p-5hOrtrGaVsWnjvYsn6rw.jpeg</url>
            <title>Stories by Gayan Virantha Perera on Medium</title>
            <link>https://medium.com/@gayanperera160?source=rss-7b3dcc2f8737------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:51:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gayanperera160/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CSRF Migration — Double Submit Cookie Pattern]]></title>
            <link>https://medium.com/@gayanperera160/csrf-migration-double-submit-cookie-pattern-e4499713404e?source=rss-7b3dcc2f8737------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4499713404e</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[double-submit-cookie]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Gayan Virantha Perera]]></dc:creator>
            <pubDate>Thu, 10 Oct 2019 19:27:40 GMT</pubDate>
            <atom:updated>2019-10-12T08:38:58.450Z</atom:updated>
            <content:encoded><![CDATA[<h3>CSRF Mitigation — Double Submit Cookie Pattern</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*nsno3fnkOEAcZEoACkjJzA.jpeg" /></figure><p>I Discussed Cross-Site Request Forgery and about the Synchronizer Token Pattern in the previous blog. Refer to this <a href="https://medium.com/@gayanperera160/csrf-mitigation-synchronizer-token-pattern-1266428c87df"><em>blog</em></a>, if you want more information about CSRF and Synchronizer Token Pattern.</p><p>In this blog, I am going to discuss the Double Submit Cookie Pattern.</p><h3><strong>Double Submit Cookie Pattern</strong></h3><p>When we are maintaining and storing CSRF tokens on the server-side, it can be problematic because the server may cause memory overflows with the growth of users. When using Double Submit Cookie Pattern, the <strong>token is not saved in the server in any form</strong>. It is <strong>stateless</strong> and <strong>easy to implement</strong>. Therefore using Double Submit Cookie Pattern has big advantages in Memory Management compared to Synchronizer Token Pattern.</p><h3>How Does it Work?</h3><p>When a user logs in to a web application, the server verifies the credentials and authenticate the user. Meanwhile, the<strong> Session ID</strong> and a <strong>Token Cookie</strong> are generated by the server and set them as cookies. Then the server stores the Session ID, but the server<strong> should not store the Token Cookie</strong>.</p><p>When the client browser receives the authentication response, it reads the token cookies and embeds the value into a hidden field in a form. When the form is submitted, the Token Cookie and the session ID are sent in the request header, with the Token Cookie’s value in the request body.</p><p>Once the server receives the request. it extracts and compares the token cookie in the request header and token cookie value in the request body and check if they are matching. If they match it will be considered as a valid request and if it doesn’t match the request will be rejected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*3eE6WLqPjPkQ19aO6JL7Ew.png" /></figure><p>I have developed this simple web application using PHP and JavaScript. You can find the source code<em> </em><a href="https://github.com/gayan98/double-submit-cookie"><strong><em>here</em></strong></a></p><p>First, you have to log into the application providing the hardcoded username and password.</p><p><strong><em>Username- admin</em></strong></p><p><strong><em>Password- 1234</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*C6fAwUk9ENXowJ7jmP3Maw.jpeg" /><figcaption>Login Page (index.php)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/1*XxoTgn1zQGUTV83Tms086Q.jpeg" /></figure><p>Once the user submits the user credentials, the server will authenticate the user will be redirected to <strong><em>addinfo.php</em></strong><em> </em>where you can add a name to the application. Also, the server will generate the Session ID and the CSRF token (The server does not save the token). Then the server sets the Session ID and the token into the cookie.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*aE31ibYilYyW5ySTGr_bJQ.jpeg" /><figcaption>addinfo.php</figcaption></figure><p>After the user logs in, the below function will retrieve cookies and split them to get only the CSRF token. Then it will embed it to the hidden field in the form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/979/1*Bgj-NPx0Bz8jxn_wHv8UJQ.jpeg" /><figcaption>Function to retrieve the token</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CPBfxyoUFmcx-dFoUQh-aw.jpeg" /><figcaption>HIdden Field in the form</figcaption></figure><p>The user can enter a name and submit the form at the <em>addinfo.php</em> page. When the form is submitted, Then the server validates session-id which is in the request header using the one that is stored in the server-side, and also it compares the CSRF token in the hidden field with the token in the request header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xJFBHGyUdp4xk1QnsPo8KQ.jpeg" /><figcaption>validation.php</figcaption></figure><p>If the tokens are matching, the request will be accepted by the server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*ezaZ1QfHO7X-tSU6f1b_Cw.jpeg" /><figcaption>Updated Successfully</figcaption></figure><p>if not, there will be an error like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*yl6zRCBiVKPd-YrddGu2gg.jpeg" /><figcaption>When the tokens are not matching.</figcaption></figure><p>Thank you. See you soon</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4499713404e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CSRF Mitigation- Synchronizer Token Pattern]]></title>
            <link>https://medium.com/@gayanperera160/csrf-mitigation-synchronizer-token-pattern-1266428c87df?source=rss-7b3dcc2f8737------2</link>
            <guid isPermaLink="false">https://medium.com/p/1266428c87df</guid>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[csrf-token]]></category>
            <category><![CDATA[synchronizer-token]]></category>
            <dc:creator><![CDATA[Gayan Virantha Perera]]></dc:creator>
            <pubDate>Wed, 09 Oct 2019 16:58:51 GMT</pubDate>
            <atom:updated>2019-10-09T17:00:56.043Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>What is CSRF?</strong></h3><p><strong>Cross-Site Request Forgery </strong>also Know as <strong>CSRF</strong> or <strong>XSRF</strong> is an attack that tricks user web browsers to execute malicious or unwanted actions on web applications that users are already authenticated. This attack can be done by transmitting commands using Hidden Forms and tags or malicious javascript, XML or HTTP requests. This attack can result in unauthorized fund transfers, data or identity theft, or they can hijack sessions.</p><h3><strong>How does a CSRF attack happen?</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*2HkiAh1REC1oBGKO6XZUfQ.png" /></figure><p>For a CSRF attack to work successfully, the victim must be authenticated and logged into the targetted site. For example, let’s assume If <strong><em>abcbank.com</em></strong> is an online banking application and it is vulnerable to CSRF. Also, let’s assume that <strong><em>John </em></strong>is a client of this bank and an authenticated user of this application. If John visits or interacts with a website that contains a CSRF attack which is targetting abcbank.com while he is logged in, the attack will be executed and it will cause devastation.</p><h3><strong>Prevention</strong></h3><p>There are two main security implementations to prevent this prevention. they are Synchronizer token pattern &amp; Double submit cookie.</p><h3><strong>Synchronizer Token Pattern</strong></h3><p>Synchronizer Token Pattern is a technique where a unique token or a value is generated for each session. It is Embedded as a hidden field in HTML forms which will be validated by the server and authorize the request that should be completed. An attacker cannot read or modify cookie values because of the same-origin requirement which will prevent CSRF.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*rnmFfsMqNndjExE7u6wpxw.png" /></figure><p>I have developed this simple web application using PHP and JavaScript. You can find the source code<em> </em><a href="https://github.com/gayan98/synchronizer-token-pattern.git"><strong><em>here</em></strong></a><strong><em>.</em></strong></p><p>First, you have to log into the application providing the hardcoded username and password.</p><p><strong><em>Username- admin</em></strong></p><p><strong><em>Password- 1234</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*C6fAwUk9ENXowJ7jmP3Maw.jpeg" /><figcaption>Login page (index.php)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*luVHeKiS9fznQxh0KYIHOg.jpeg" /><figcaption>index.php</figcaption></figure><p>After validating the credentials and if the user is authenticated, the user will be redirected to <strong><em>addinfo.php</em></strong><em> </em>where you can add a name to the application. Also, the server will generate the session ID and the CSRF token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/725/1*w1i8VnfSNc2h9NwBDoAd5g.jpeg" /><figcaption>addinfo.php</figcaption></figure><p>To retrieve the CSRF token an AJAX call should be executed. The retrieved CSRF token will be stored in a hidden field which is in <strong><em>addinfo.php</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/1*2DfIxkkwm1x7ahlrGzcauw.jpeg" /><figcaption>AJAX call</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e8P4J9sNYlshj5s4ihaa8Q.jpeg" /><figcaption>Hidden field to store the CSRF Token</figcaption></figure><p>The CSRF token will be validated by <strong><em>validation.php. </em></strong>When a user adds a name to the application, the request contains the generated CSRF token. Then the server validates session-id which is in the request header using the one that is stored in the server-side, and also it compares the CSRF token in the body with the token in the request header.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1FHuAtshe82FHiKvWdsr3w.jpeg" /><figcaption>validation.php</figcaption></figure><p>If the CSRF tokens are a match, the application will add the name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*ezaZ1QfHO7X-tSU6f1b_Cw.jpeg" /><figcaption>Successfully added the name</figcaption></figure><p>Otherwise, it will show an error</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/404/1*yl6zRCBiVKPd-YrddGu2gg.jpeg" /><figcaption>When the CSRF tokens are not matching</figcaption></figure><p>I will be discussing the Double Submit cookie CSRF prevention method in the next blog post.</p><p>Thank you. See you soon</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1266428c87df" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>