Skip to content

Instantly share code, notes, and snippets.

@magecommerce
Forked from ananth-iyer/CsrfValidatorSkip.php
Created October 14, 2021 08:57
Show Gist options
  • Select an option

  • Save magecommerce/3bdc6eac6e77a028c55d067c96489591 to your computer and use it in GitHub Desktop.

Select an option

Save magecommerce/3bdc6eac6e77a028c55d067c96489591 to your computer and use it in GitHub Desktop.

Revisions

  1. @ananth-iyer ananth-iyer revised this gist Jun 18, 2020. 2 changed files with 7 additions and 2 deletions.
    7 changes: 6 additions & 1 deletion CsrfValidatorSkip.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    <?php
    namespace Module\Vendor\Plugin;
    namespace Vendor\Module\Plugin;

    class CsrfValidatorSkip
    {
    @@ -15,9 +15,14 @@ public function aroundValidate(
    $request,
    $action
    ) {
    /* Magento 2.1.x, 2.2.x */
    if ($request->getModuleName() == 'Your_Module_frontName_Here') {
    return; // Skip CSRF check
    }
    /* Magento 2.3.x */
    if (strpos($request->getOriginalPathInfo(), 'Add_Controller_frontName') !== false) {
    return; // Skip CSRF check
    }
    $proceed($request, $action); // Proceed Magento 2 core functionalities
    }
    }
    2 changes: 1 addition & 1 deletion di.xml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    <?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="Module\Vendor\Plugin\CsrfValidatorSkip" />
    <plugin name="csrf_validator_skip" type="Vendor\Module\Plugin\CsrfValidatorSkip" />
    </type>
    </config>
  2. @ananth-iyer ananth-iyer created this gist Mar 27, 2019.
    23 changes: 23 additions & 0 deletions CsrfValidatorSkip.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php
    namespace Module\Vendor\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
    ) {
    if ($request->getModuleName() == 'Your_Module_frontName_Here') {
    return; // Skip CSRF check
    }
    $proceed($request, $action); // Proceed Magento 2 core functionalities
    }
    }
    6 changes: 6 additions & 0 deletions di.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?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="Module\Vendor\Plugin\CsrfValidatorSkip" />
    </type>
    </config>