Skip to content

Commit

Permalink
tested on PHP8.3.
Browse files Browse the repository at this point in the history
added Session\ConfigInterface plugin for PHPSESSID
removed PHPSESSID fron config.xml
  • Loading branch information
HirokazuNishi committed Apr 20, 2024
1 parent c3f0885 commit b88ebce
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 71 deletions.
2 changes: 2 additions & 0 deletions Model/Config/Source/SameSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class SameSite
{
/**
* Return options array
*
* @return array
*/
public function toOptionArray()
Expand Down
68 changes: 68 additions & 0 deletions Plugin/Session/ModifySameSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Veriteworks\CookieFix\Plugin\Session;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Session\Config;
use Magento\Framework\Session\Config\ConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\HTTP\Header;
use Veriteworks\CookieFix\Validator\SameSite;

class ModifySameSite
{
public const CONFIG_PATH = 'web/cookie/samesite';

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var SameSite
*/
private $validator;
/**
* @var Header
*/
private $header;

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

/**
* Modify samesite attribute
*
* @param ConfigInterface $subject
* @param string $cookieSameSite
* @return array
*/
public function beforeSetCookieSameSite(ConfigInterface $subject, string $cookieSameSite = 'Lax'): array
{
$agent = $this->header->getHttpUserAgent();
$sameSite = $this->validator->shouldSendSameSiteNone($agent);
if ($sameSite === false) {
$cookieSameSite = 'None';
} else {
$config = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
if ($config !== $cookieSameSite) {
$cookieSameSite = ucfirst($config);
}
}

return [$cookieSameSite];
}
}
24 changes: 17 additions & 7 deletions Plugin/SwitchSameSite.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Veriteworks\CookieFix\Plugin;


use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Header;
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
Expand All @@ -11,8 +10,8 @@

class SwitchSameSite
{
const CONFIG_PATH = 'web/cookie/samesite';
const CONFIG_AFFECTED_KEYS = 'web/cookie/affected_keys';
public const CONFIG_PATH = 'web/cookie/samesite';
public const CONFIG_AFFECTED_KEYS = 'web/cookie/affected_keys';
/**
* @var SameSite
*/
Expand All @@ -26,10 +25,14 @@ class SwitchSameSite
*/
private $scopeConfig;

/**
* @var array
*/
private $affectedKeys = [];

/**
* SwitchSameSite constructor.
*
* @param Header $header
* @param ScopeConfigInterface $scopeConfig
* @param SameSite $validator
Expand All @@ -45,6 +48,8 @@ public function __construct(
}

/**
* Modify same site cookie attribute
*
* @param PhpCookieManager $subject
* @param string $name
* @param string $value
Expand All @@ -68,8 +73,7 @@ public function beforeSetPublicCookie(
$config = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);

// Convert to lowercase since sometimes it comes as lower-cased string
if(strtolower($config) === 'none')
{
if (strtolower($config) === 'none') {
$metadata->setSecure(true);
}
$metadata->setSameSite($config);
Expand All @@ -79,11 +83,17 @@ public function beforeSetPublicCookie(
return [$name, $value, $metadata];
}

/**
* Check given key is in affected keys list
*
* @param string $name
* @return bool
*/
private function isAffectedKeys($name)
{
if (!count($this->affectedKeys)) {
$affectedKeys = (string)$this->scopeConfig->getValue(self::CONFIG_AFFECTED_KEYS, ScopeInterface::SCOPE_STORE);
$this->affectedKeys = explode(',', strtolower($affectedKeys));
$affectedKeys = $this->scopeConfig->getValue(self::CONFIG_AFFECTED_KEYS, ScopeInterface::SCOPE_STORE);
$this->affectedKeys = explode(',', strtolower((string)$affectedKeys));
}

return in_array(strtolower($name), $this->affectedKeys);
Expand Down
68 changes: 40 additions & 28 deletions Test/Unit/Validator/SameSiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
use PHPUnit\Framework\TestCase;
use Veriteworks\CookieFix\Validator\SameSite;

/**
* Class SameSiteTest
* @package Veriteworks\CookieFix\Test\Unit\Validator
*/
class SameSiteTest extends TestCase
{
/**
Expand All @@ -27,7 +23,7 @@ public function setUp() :void
* @param $useragent
* @param $expected
*
* @dataProvider iphoneAgentProvider
* @dataProvider iphoneAgentProvider
*/
public function testIphone($useragent, $expected)
{
Expand All @@ -39,7 +35,7 @@ public function testIphone($useragent, $expected)
* @param $useragent
* @param $expected
*
* @dataProvider ipadAgentProvider
* @dataProvider ipadAgentProvider
*/
public function testIpad($useragent, $expected)
{
Expand All @@ -51,7 +47,7 @@ public function testIpad($useragent, $expected)
* @param $useragent
* @param $expected
*
* @dataProvider chromeAgentProvider
* @dataProvider chromeAgentProvider
*/
public function testChrome($useragent, $expected)
{
Expand All @@ -63,7 +59,7 @@ public function testChrome($useragent, $expected)
* @param $useragent
* @param $expected
*
* @dataProvider chromiumAgentProvider
* @dataProvider chromiumAgentProvider
*/
public function testChromium($useragent, $expected)
{
Expand All @@ -75,7 +71,7 @@ public function testChromium($useragent, $expected)
* @param $useragent
* @param $expected
*
* @dataProvider safariAgentProvider
* @dataProvider safariAgentProvider
*/
public function testSafari($useragent, $expected)
{
Expand All @@ -87,7 +83,7 @@ public function testSafari($useragent, $expected)
* @param $useragent
* @param $expected
*
* @dataProvider ucbrowserAgentProvider
* @dataProvider ucbrowserAgentProvider
*/
public function testUcBrowser($useragent, $expected)
{
Expand All @@ -102,15 +98,18 @@ public function iphoneAgentProvider()
{
return [
[
'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_5 like Mac OS X) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0 Mobile/15D60 Safari/604.1',
'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_5 like Mac OS X)' .
' AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0 Mobile/15D60 Safari/604.1',
false
],
[
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_1 like Mac OS X)' .
' AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.2 Mobile/15E148 Safari/604.1',
false
],
[
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X)' .
' AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Mobile/15E148 Safari/604.1',
true
],
];
Expand All @@ -123,15 +122,18 @@ public function ipadAgentProvider()
{
return [
[
'Mozilla/5.0 (iPad; CPU OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPad; CPU OS 11_4_1 like Mac OS X)' .
' AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1',
false
],
[
'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X)' .
' AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Mobile/15E148 Safari/604.1',
false
],
[
'Mozilla/5.0 (iPad; CPU OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1',
'Mozilla/5.0 (iPad; CPU OS 13_3_1 like Mac OS X)' .
' AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1',
true
],
];
Expand All @@ -144,15 +146,18 @@ public function chromeAgentProvider()
{
return [
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)' .
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
true
],
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3)' .
' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
false
],
[
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.64',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36' .
' (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.64',
false
],
];
Expand All @@ -165,11 +170,13 @@ public function chromiumAgentProvider()
{
return [
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chromium/80.0.3729.157 Chrome/80.0.3729.157 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36' .
' (KHTML, like Gecko) Chromium/80.0.3729.157 Chrome/80.0.3729.157 Safari/537.36',
true
],
[
'Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko) Raspbian Chromium/74.0.3729.157 Chrome/74.0.3729.157 Safari/537.36',
'Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/537.36 (KHTML, like Gecko)' .
' Raspbian Chromium/74.0.3729.157 Chrome/74.0.3729.157 Safari/537.36',
true
]

Expand All @@ -183,11 +190,13 @@ public function safariAgentProvider()
{
return [
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15' .
' (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15',
false
],
[
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15' .
' (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15',
true
],
];
Expand All @@ -200,22 +209,25 @@ public function ucbrowserAgentProvider()
{
return [
[
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.7.0.953 Mobile Safari/537.36',
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36' .
'(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.7.0.953 Mobile Safari/537.36',
false
],
[
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.13.2.953 Mobile Safari/537.36',
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36' .
'(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.13.2.953 Mobile Safari/537.36',
true
],
[
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.13.1.953 Mobile Safari/537.36',
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36' .
'(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.13.1.953 Mobile Safari/537.36',
false
],
[
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.12.0.953 Mobile Safari/537.36',
'Mozilla/5.0(Linux;U;Android 5.1.1;zh-CN;OPPO A33 Build/LMY47V) AppleWebKit/537.36' .
'(KHTML,like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/12.12.0.953 Mobile Safari/537.36',
false
],
];
}

}
Loading

0 comments on commit b88ebce

Please sign in to comment.