Skip to content

Commit

Permalink
replace rewrite into plugin.
Browse files Browse the repository at this point in the history
added browser version check on plugin (missed).
  • Loading branch information
HirokazuNishi committed Sep 16, 2020
1 parent dbac2e1 commit 9c84bc6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 46 deletions.
64 changes: 64 additions & 0 deletions Plugin/Session/AddSameSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Veriteworks\CookieFix\Plugin\Session;

use Magento\Framework\HTTP\Header;
use Magento\Framework\Session\Config;
use Veriteworks\CookieFix\Validator\SameSite;

class AddSameSite
{
/**
* @var SameSite
*/
private $validator;
/**
* @var Header
*/
private $header;

/**
* AddSameSite constructor.
* @param SameSite $validator
* @param Header $header
*/
public function __construct(
SameSite $validator,
Header $header
) {
$this->validator = $validator;
$this->header = $header;
}

/**
* @param \Magento\Framework\Session\Config $subject
* @param $result
* @param string $cookiePath
* @param string|null $default
*/
public function afterSetCookiePath(
Config $subject,
$result,
$cookiePath,
$default = null
) {
$version = PHP_VERSION_ID;
$agent = $this->header->getHttpUserAgent();
$sameSite = $this->validator->shouldSendSameSiteNone($agent);

if (!$sameSite) {
return $result;
}

if ($version >= 70300) {
$subject->setOption('session.cookie_samesite', 'None');
} else {
$path = $subject->getCookiePath();
if (!preg_match('/SameSite/', $path)) {
$path .= '; SameSite=None';
$subject->setOption('session.cookie_path', $path);
}
}

return $result;
}
}
4 changes: 2 additions & 2 deletions Plugin/View/Element/Js/AdjustPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class AdjustPath
{

/**
* @param \Magento\Framework\View\Element\Js\Cookie $subject
* @param Cookie $subject
* @param $result
*/
public function afterGetPath(\Magento\Framework\View\Element\Js\Cookie $subject, $result)
public function afterGetPath(Cookie $subject, $result)
{
if (preg_match('/SameSite/', $result)) {
$str = explode(';', $result);
Expand Down
39 changes: 0 additions & 39 deletions Session/Config.php

This file was deleted.

6 changes: 3 additions & 3 deletions Stdlib/Cookie/CookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function setCookie($name, $value, array $metadataArray)
self::KEY_HTTP_ONLY => $this->extractValue(CookieMetadata::KEY_HTTP_ONLY, $metadataArray, false)
];

if (array_key_exists(ExtendPulicCookieMetadata::KEY_SAMESITE, $metadataArray)) {
if (array_key_exists(ExtendPulicCookieMetadata::KEY_SAMESITE, $metadataArray)) {
$options = array_merge($options, [self::KEY_SAME_SITE => $metadataArray[ExtendPulicCookieMetadata::KEY_SAMESITE]]);
} elseif ($sameSite) {
$options = array_merge($options, [self::KEY_SAME_SITE => 'None']);
Expand All @@ -183,10 +183,10 @@ protected function setCookie($name, $value, array $metadataArray)
$options
);
} else {
$path = $this->extractValue(CookieMetadata::KEY_PATH, $metadataArray, '');
$path = $this->extractValue(CookieMetadata::KEY_PATH, $metadataArray, '');
if (array_key_exists(ExtendPulicCookieMetadata::KEY_SAMESITE, $metadataArray)) {
$path .= '; SameSite=' . $metadataArray[ExtendPulicCookieMetadata::KEY_SAMESITE];
} elseif ($sameSite) {
} elseif ($sameSite && !preg_match('/SameSite/', $path)) {
$path .= '; SameSite=None';
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"AFL-3.0"
],
"description":"Magento2 extension for Cookie SameSite attribute.",
"version":"2.3.1",
"version":"2.4.0",
"authors":[
{
"name":"Hirokazu Nishi",
Expand Down
1 change: 0 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<preference for="Magento\Framework\Stdlib\CookieManagerInterface" type="Veriteworks\CookieFix\Stdlib\Cookie\CookieManager"/>
<preference for="Magento\Framework\Session\Config\ConfigInterface" type="Veriteworks\CookieFix\Session\Config"/>
<preference for="Magento\Framework\Stdlib\Cookie\PublicCookieMetadata" type="Veriteworks\CookieFix\Rewrite\Stdlib\Cookie\PublicCookieMetadata"/>
</config>
5 changes: 5 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
type="Veriteworks\CookieFix\Plugin\View\Element\Js\AdjustPath"
sortOrder="10"/>
</type>
<type name="Magento\Framework\Session\Config">
<plugin name="add_samesite"
type="Veriteworks\CookieFix\Plugin\Session\AddSameSite"
sortOrder="10"/>
</type>
</config>

0 comments on commit 9c84bc6

Please sign in to comment.