How To

How to Bypass CSRF Validation in Magento 2?

Bypassing CSRF (Cross-Site Request Forgery) validation in Magento 2 is generally not recommended, as it can expose your store to significant security risks. CSRF protection is implemented to prevent unauthorized actions on behalf of authenticated users. However, there may be specific scenarios where you need to bypass CSRF validation, such as when integrating third-party services or handling custom API requests. This blog will guide you through the steps to bypass CSRF validation in Magento 2, but be cautious and understand the security implications.

Understanding CSRF in Magento 2

CSRF is a security mechanism that ensures requests made to your Magento 2 store are legitimate and originate from the authenticated user. Magento 2 uses a CSRF token to validate POST, PUT, and DELETE requests to prevent malicious attacks.

When to Bypass CSRF Validation?

Bypassing CSRF validation should only be considered when:

  • You are integrating a trusted third-party service that cannot provide the CSRF token.
  • You are handling specific requests where CSRF validation is unnecessary.
  • You are working in a controlled development environment and need to bypass validation for testing purposes.

Steps to Bypass CSRF Validation in Magento 2:

Step 1: Create a di.xml file in the below-given path

app\code\Vendor\Extension\etc\di.xml

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\Request\CsrfValidator">
        <plugin name="csrf_validator_skip" type="Vendor\Extension\Plugin\Csrfvalidatorskip" />
    </type>
</config>

Step 2: Create a Csrfvalidatorskip.php file in the below path.

app\code\Vendor\Extension\Plugin\Csrfvalidatorskip.php

Now add the following code

<?php
namespace Vendor\Extension\Plugin;

class Csrfvalidatorskip
{
    /**
     * @param \Magento\Framework\App\Request\CsrfValidator $subject
     * @param \Closure $proceed
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Framework\App\ActionInterface $action
     */    public function aroundValidate(
        $subject,
        \Closure $proceed,
        $request,
        $action
    ) {
        // replace your route name with yourroutename
        if ($request->getModuleName() == 'yourroutename') {  
            return; // Skip CSRF check
        }
        $proceed($request, $action); // Proceed Magento 2 core functionalities
    }
}

Conclusion:

Bypassing CSRF validation in Magento 2 can be necessary in specific cases, but it comes with significant security risks. Always weigh the need to bypass against the potential vulnerabilities it may introduce. Implement the bypass in a controlled manner, and take additional security measures to protect your Magento 2 store. If possible, seek alternative solutions that do not require bypassing CSRF validation.

By following this guide, you can safely bypass CSRF validation when necessary, ensuring that your Magento 2 store remains secure and functional.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

12 hours ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

3 days ago

Hyvä Theme FAQs

Hyvä is gradually gaining popularity in this current market, and even 3.5% of Magento websites…

4 days ago

What is Curbside Pickup?

In today’s fast-paced society, where convenience and safety are paramount, curbside pickup has emerged as…

4 days ago

What is a Planogram?

Have you ever observed how complementary and similar items are often displayed together in brick-and-mortar…

4 days ago

Hyvä Checkout – A Real Game Changer

You may be familiar with Hyvä, the frontend theme for Magento 2, which has been…

4 days ago