{"id":907,"date":"2012-09-14T20:09:36","date_gmt":"2012-09-14T20:09:36","guid":{"rendered":"http:\/\/www.websitedesignwebsitedevelopment.com\/?p=907"},"modified":"2014-03-19T09:13:47","modified_gmt":"2014-03-19T09:13:47","slug":"cross-site-request-forgery-csrf-ajax-fix","status":"publish","type":"post","link":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/","title":{"rendered":"Cross-site request forgery (CSRF) &#8211; Ajax Fix"},"content":{"rendered":"<h2>Cross-site request forgery (CSRF) &#8211; Ajax Fix<\/h2>\n<p>CSRF <a title=\"Security Class\" href=\"http:\/\/codeigniter.com\/user_guide\/libraries\/security.html\" rel=\"nofollow\" target=\"_blank\">Security Class<\/a> is great practice which is not really in practice commonly. The reason is workload of the scope of the project. Developers mostly don&#8217;t care about the form class of the CI and 20% or less people use form helper to generate 100% forms in their projects. I am one of those developers who developed numerous web based projects of medium to large nature but still i never fully used form helper for form related activities. And CSRF comes with form_open and form_close as CI says &#8220;If you use the form helper the <var>form_open()<\/var> function will automatically insert a hidden csrf field in your forms.&#8221; When i checked this class and realized that it is a useful thing so i reviewed my views and forms related pages. It was a huge work that i have to make changes in almost 82 pages in whole project. In which a couple of pages are not views but helper functions etc. The common step ahead is to leave as it is and plan it for the next time? Yes, its true.<\/p>\n<p>But i figured out another possibility which i want to share for those projects which are lacking this security and can have this one easily. Here are few recommendations before implementation of CSRF:<\/p>\n<ol>\n<li>Use Template Parser<\/li>\n<li>Keep the CSRF config values dynamic so it will be hard to guess in form as hidden field<\/li>\n<\/ol>\n<p>Now if you are using template parser then modify the parser library from system library folder to make a variable available in all the views with simple curly brackets e.g. {csrf_token}<\/p>\n<p>If you are not using template parser then create form_hidden with the name and value given in the security class inside system folder.<\/p>\n<p>Suppose that you made this token available in views either by template parser or by some helper function or form_hidden function. Now you have to find all the files which are having the tag &#8221;<\/p>\n<p>&#8221; ending. You have to replace all<\/p>\n<p>with {csrf_token}<\/p>\n<p>or some helper function with form ending tag. By this you will have all the forms pages updated.<\/p>\n<p>Thats it, now your forms are security but your ajax calls won&#8217;t be accepted if you are not sending that csrf_token parameter in post or get type of ajax. For this you have two options, either you send this parameter for all the ajax requests or make your security class to ignore it by sensing the XMLHttpRequest. And <a href=\"http:\/\/www.websitedesignwebsitedevelopment.com\/website-development\/security-related\/cross-domain-form-posted-php-security-fix\/894\" target=\"_blank\">use this code<\/a> for ajax portion. By this you will prevent spamming activities through REFFERER HOST AND THE HTTP HOST matching. Now you can turn ON the option from config file. I will propose these changes to ellislab soon because this thing will encourage those developers who couldn&#8217;t implement this fix yet because of a lot of work to be redo.<\/p>\n<p>Thanks,<br \/>\nFahad<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cross-site request forgery (CSRF) &#8211; Ajax Fix CSRF Security Class is great practice which is not really in practice commonly. The reason is workload of the scope of the project. Developers mostly don&#8217;t care about the form class of the CI and 20% or less people use form helper to generate 100% forms in their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-907","post","type-post","status-publish","format-standard","hentry","category-codeigniter"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Cross-site request forgery (CSRF) - Ajax Fix<\/title>\n<meta name=\"description\" content=\"Cross-site request forgery (CSRF) - Ajax Fix\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cross-site request forgery (CSRF) - Ajax Fix\" \/>\n<meta property=\"og:description\" content=\"Cross-site request forgery (CSRF) - Ajax Fix\" \/>\n<meta property=\"og:url\" content=\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\" \/>\n<meta property=\"og:site_name\" content=\"AndroidBubble Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/pages\/Website-Design-Website-Development\/172363336135453\" \/>\n<meta property=\"article:published_time\" content=\"2012-09-14T20:09:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-03-19T09:13:47+00:00\" \/>\n<meta name=\"author\" content=\"Fahad\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Fahad\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\"},\"author\":{\"name\":\"Fahad\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"headline\":\"Cross-site request forgery (CSRF) &#8211; Ajax Fix\",\"datePublished\":\"2012-09-14T20:09:36+00:00\",\"dateModified\":\"2014-03-19T09:13:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\"},\"wordCount\":492,\"articleSection\":[\"CodeIgniter\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\",\"url\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\",\"name\":\"Cross-site request forgery (CSRF) - Ajax Fix\",\"isPartOf\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#website\"},\"datePublished\":\"2012-09-14T20:09:36+00:00\",\"dateModified\":\"2014-03-19T09:13:47+00:00\",\"author\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\"},\"description\":\"Cross-site request forgery (CSRF) - Ajax Fix\",\"breadcrumb\":{\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/androidbubble.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Website Development\",\"item\":\"https:\/\/androidbubble.com\/blog\/category\/website-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP Frameworks\",\"item\":\"https:\/\/androidbubble.com\/blog\/category\/website-development\/php-frameworks\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"CodeIgniter\",\"item\":\"https:\/\/androidbubble.com\/blog\/category\/website-development\/php-frameworks\/codeigniter\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Cross-site request forgery (CSRF) &#8211; Ajax Fix\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#website\",\"url\":\"https:\/\/androidbubble.com\/blog\/\",\"name\":\"AndroidBubble Blog\",\"description\":\"Articles &amp; Posts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/androidbubble.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7\",\"name\":\"Fahad\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g\",\"caption\":\"Fahad\"},\"sameAs\":[\"http:\/\/www.androidbubbles.com\/\",\"https:\/\/x.com\/phpFlex\"],\"url\":\"https:\/\/androidbubble.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cross-site request forgery (CSRF) - Ajax Fix","description":"Cross-site request forgery (CSRF) - Ajax Fix","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/","og_locale":"en_US","og_type":"article","og_title":"Cross-site request forgery (CSRF) - Ajax Fix","og_description":"Cross-site request forgery (CSRF) - Ajax Fix","og_url":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/","og_site_name":"AndroidBubble Blog","article_publisher":"https:\/\/www.facebook.com\/pages\/Website-Design-Website-Development\/172363336135453","article_published_time":"2012-09-14T20:09:36+00:00","article_modified_time":"2014-03-19T09:13:47+00:00","author":"Fahad","twitter_misc":{"Written by":"Fahad","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#article","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/"},"author":{"name":"Fahad","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"headline":"Cross-site request forgery (CSRF) &#8211; Ajax Fix","datePublished":"2012-09-14T20:09:36+00:00","dateModified":"2014-03-19T09:13:47+00:00","mainEntityOfPage":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/"},"wordCount":492,"articleSection":["CodeIgniter"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/","url":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/","name":"Cross-site request forgery (CSRF) - Ajax Fix","isPartOf":{"@id":"https:\/\/androidbubble.com\/blog\/#website"},"datePublished":"2012-09-14T20:09:36+00:00","dateModified":"2014-03-19T09:13:47+00:00","author":{"@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7"},"description":"Cross-site request forgery (CSRF) - Ajax Fix","breadcrumb":{"@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/androidbubble.com\/blog\/website-development\/php-frameworks\/codeigniter\/cross-site-request-forgery-csrf-ajax-fix\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/androidbubble.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Website Development","item":"https:\/\/androidbubble.com\/blog\/category\/website-development\/"},{"@type":"ListItem","position":3,"name":"PHP Frameworks","item":"https:\/\/androidbubble.com\/blog\/category\/website-development\/php-frameworks\/"},{"@type":"ListItem","position":4,"name":"CodeIgniter","item":"https:\/\/androidbubble.com\/blog\/category\/website-development\/php-frameworks\/codeigniter\/"},{"@type":"ListItem","position":5,"name":"Cross-site request forgery (CSRF) &#8211; Ajax Fix"}]},{"@type":"WebSite","@id":"https:\/\/androidbubble.com\/blog\/#website","url":"https:\/\/androidbubble.com\/blog\/","name":"AndroidBubble Blog","description":"Articles &amp; Posts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/androidbubble.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/0a833b633f744c3a5e0de7c9a94e71f7","name":"Fahad","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/androidbubble.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/afab5bcad71955d142a69df1758e6710aed4ab9cc2b5da28c54a7c3f39801b73?s=96&d=mm&r=g","caption":"Fahad"},"sameAs":["http:\/\/www.androidbubbles.com\/","https:\/\/x.com\/phpFlex"],"url":"https:\/\/androidbubble.com\/blog\/author\/admin\/"}]}},"views":913,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/comments?post=907"}],"version-history":[{"count":2,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/907\/revisions"}],"predecessor-version":[{"id":909,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/posts\/907\/revisions\/909"}],"wp:attachment":[{"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/media?parent=907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/categories?post=907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/androidbubble.com\/blog\/wp-json\/wp\/v2\/tags?post=907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}