From 1fd363156a3040bb6c545d6c1a7fc826662439c3 Mon Sep 17 00:00:00 2001 From: Doug Sheffer Date: Wed, 29 Jul 2020 12:47:56 -0400 Subject: [PATCH 1/3] Add cookie_samesite Signed-off-by: Doug Sheffer --- docs/book/config.md | 1 + src/Config/ConfigInterface.php | 9 ++++++++ src/Config/StandardConfig.php | 34 ++++++++++++++++++++++++++++++ test/Config/SessionConfigTest.php | 24 +++++++++++++++++++++ test/Config/StandardConfigTest.php | 13 ++++++++++++ 5 files changed, 81 insertions(+) diff --git a/docs/book/config.md b/docs/book/config.md index e544578f..c32948d6 100644 --- a/docs/book/config.md +++ b/docs/book/config.md @@ -22,6 +22,7 @@ Option | Data Type | Description `cookie_httponly` | `boolean` | Marks the cookie as accessible only through the HTTP protocol. `cookie_lifetime` | `integer` | Specifies the lifetime of the cookie in seconds which is sent to the browser. `cookie_path` | `string` | Specifies path to set in the session cookie. +`cookie_samesite` | `string` | Specifies whether cookies should be sent along with cross-site requests. `cookie_secure` | `boolean` | Specifies whether cookies should only be sent over secure connections. `entropy_length` | `integer` | Specifies the number of bytes which will be read from the file specified in entropy_file. Removed in PHP 7.1.0. `entropy_file` | `string` | Defines a path to an external resource (file) which will be used as an additional entropy. Removed in PHP 7.1.0. diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php index c4b3c643..bab34889 100644 --- a/src/Config/ConfigInterface.php +++ b/src/Config/ConfigInterface.php @@ -83,6 +83,15 @@ public function setCookieDomain($cookieDomain); /** @return string */ public function getCookieDomain(); + /** + * @param bool $cookieSameSite + * @return void + */ + public function setCookieSameSite($cookieSameSite); + + /** @return bool */ + public function getCookieSameSite(); + /** * @param bool $cookieSecure * @return void diff --git a/src/Config/StandardConfig.php b/src/Config/StandardConfig.php index 947f3c81..ef551db0 100644 --- a/src/Config/StandardConfig.php +++ b/src/Config/StandardConfig.php @@ -70,6 +70,13 @@ class StandardConfig implements ConfigInterface */ protected $cookieDomain; + /** + * session.cookie_samesite + * + * @var string + */ + protected $cookieSameSite; + /** * session.cookie_secure * @@ -511,6 +518,32 @@ public function getCookieDomain() return $this->cookieDomain; } + /** + * Set session.cookie_samesite + * + * @param string $cookieSameSite + * @return StandardConfig + */ + public function setCookieSameSite($cookieSameSite) + { + $this->cookieSameSite = (string) $cookieSameSite; + $this->setStorageOption('cookie_samesite', $this->cookieSameSite); + return $this; + } + + /** + * Get session.cookie_samesite + * + * @return string + */ + public function getCookieSameSite() + { + if (null === $this->cookieSameSite) { + $this->cookieSameSite = $this->getStorageOption('cookie_samesite'); + } + return $this->cookieSameSite; + } + /** * Set session.cookie_secure * @@ -912,6 +945,7 @@ public function toArray() 'cookie_httponly' => $this->getCookieHttpOnly(), 'cookie_lifetime' => $this->getCookieLifetime(), 'cookie_path' => $this->getCookiePath(), + 'cookie_samesite' => $this->getCookieSameSite(), 'cookie_secure' => $this->getCookieSecure(), 'name' => $this->getName(), 'remember_me_seconds' => $this->getRememberMeSeconds(), diff --git a/test/Config/SessionConfigTest.php b/test/Config/SessionConfigTest.php index bf1f697a..0a9d9d7e 100644 --- a/test/Config/SessionConfigTest.php +++ b/test/Config/SessionConfigTest.php @@ -395,6 +395,25 @@ public function testSettingInvalidCookieDomainRaisesException2(): void $this->config->setCookieDomain('D:\\WINDOWS\\System32\\drivers\\etc\\hosts'); } + // session.cookie_samesite + + public function testCookieSameSiteDefaultsToIniSettings() + { + $this->assertSame(ini_get('session.cookie_samesite'), $this->config->getCookieSameSite()); + } + + public function testCookieSameSiteIsMutable() + { + $this->config->setCookieSameSite('Strict'); + $this->assertEquals('Strict', $this->config->getCookieSameSite()); + } + + public function testCookieSameSiteAltersIniSetting() + { + $this->config->setCookieSameSite('Strict'); + $this->assertEquals('Strict', ini_get('session.cookie_samesite')); + } + // session.cookie_secure public function testCookieSecureDefaultsToIniSettings(): void @@ -901,6 +920,11 @@ public function optionsProvider(): array 'getCookieDomain', 'getlaminas.org', ], + [ + 'cookie_samesite', + 'getCookieSameSite', + 'Lax', + ], [ 'cookie_secure', 'getCookieSecure', diff --git a/test/Config/StandardConfigTest.php b/test/Config/StandardConfigTest.php index a7ffb4cb..401e5704 100644 --- a/test/Config/StandardConfigTest.php +++ b/test/Config/StandardConfigTest.php @@ -225,6 +225,14 @@ public function testSettingInvalidCookieDomainRaisesException2(): void $this->config->setCookieDomain('D:\\WINDOWS\\System32\\drivers\\etc\\hosts'); } + // session.cookie_samesite + + public function testCookieSameSiteIsMutable() + { + $this->config->setCookieSameSite('Strict'); + $this->assertEquals('Strict', $this->config->getCookieSameSite()); + } + // session.cookie_secure public function testCookieSecureIsMutable(): void @@ -524,6 +532,11 @@ public function optionsProvider(): array 'getCookieDomain', 'getlaminas.org', ], + [ + 'cookie_samesite', + 'getCookieSameSite', + 'Lax', + ], [ 'cookie_secure', 'getCookieSecure', From fa47772416ae2e13c869572fde22de37b8dc4749 Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Wed, 31 Mar 2021 08:41:40 +0200 Subject: [PATCH 2/3] Add SameSiteCookieCapableInterface Signed-off-by: Josef Moravec --- src/Config/ConfigInterface.php | 9 -- src/Config/SameSiteCookieCapableInterface.php | 15 ++ src/Config/StandardConfig.php | 2 +- src/Service/SessionConfigFactory.php | 12 ++ test/Service/SessionConfigFactoryTest.php | 18 +++ test/TestAsset/TestConfig.php | 134 ++++++++++++++++++ 6 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 src/Config/SameSiteCookieCapableInterface.php create mode 100644 test/TestAsset/TestConfig.php diff --git a/src/Config/ConfigInterface.php b/src/Config/ConfigInterface.php index bab34889..c4b3c643 100644 --- a/src/Config/ConfigInterface.php +++ b/src/Config/ConfigInterface.php @@ -83,15 +83,6 @@ public function setCookieDomain($cookieDomain); /** @return string */ public function getCookieDomain(); - /** - * @param bool $cookieSameSite - * @return void - */ - public function setCookieSameSite($cookieSameSite); - - /** @return bool */ - public function getCookieSameSite(); - /** * @param bool $cookieSecure * @return void diff --git a/src/Config/SameSiteCookieCapableInterface.php b/src/Config/SameSiteCookieCapableInterface.php new file mode 100644 index 00000000..60e8c794 --- /dev/null +++ b/src/Config/SameSiteCookieCapableInterface.php @@ -0,0 +1,15 @@ +setOptions($config); return $sessionConfig; diff --git a/test/Service/SessionConfigFactoryTest.php b/test/Service/SessionConfigFactoryTest.php index 4d15953c..533e6b20 100644 --- a/test/Service/SessionConfigFactoryTest.php +++ b/test/Service/SessionConfigFactoryTest.php @@ -4,10 +4,12 @@ use Laminas\ServiceManager\Config; use Laminas\ServiceManager\ServiceManager; +use Laminas\ServiceManager\Exception\ServiceNotCreatedException; use Laminas\Session\Config\ConfigInterface; use Laminas\Session\Config\SessionConfig; use Laminas\Session\Config\StandardConfig; use Laminas\Session\Service\SessionConfigFactory; +use LaminasTest\Session\TestAsset\TestConfig; use PHPUnit\Framework\TestCase; /** @@ -73,4 +75,20 @@ public function testServiceReceivesConfiguration(): void $config = $this->services->get(ConfigInterface::class); self::assertEquals('laminas', $config->getName()); } + + public function testServiceNotCreatedWhenInvalidSamesiteConfig() + { + $this->services->setService( + 'config', + [ + 'session_config' => [ + 'config_class' => TestConfig::class, + 'cookie_samesite' => 'Lax', + ], + ] + ); + $this->expectException(ServiceNotCreatedException::class); + $this->expectExceptionMessage('Invalid configuration class "LaminasTest\Session\TestAsset\TestConfig". When configuration option "cookie_samesite" is used, the configuration class must implement Laminas\Session\Config\SameSiteCookieCapableInterface'); + $this->services->get(ConfigInterface::class); + } } diff --git a/test/TestAsset/TestConfig.php b/test/TestAsset/TestConfig.php new file mode 100644 index 00000000..55b73835 --- /dev/null +++ b/test/TestAsset/TestConfig.php @@ -0,0 +1,134 @@ + Date: Wed, 30 Jun 2021 10:27:36 -0500 Subject: [PATCH 3/3] qa: remove BC breaks and ensure conforms to CS guidelines - Removes docblock headers from new files - Ensures all types are documented in new interface and test assets - Do not have SameSiteCookieCapableInterface extend ConfigInterface, and StandardConfig implement SameSiteCookieCapableInterface; instead, have StandarConfig implement both ConfigInterface and SameSiteCookieCapableInterface. Signed-off-by: Matthew Weier O'Phinney --- docs/book/config.md | 2 +- src/Config/SameSiteCookieCapableInterface.php | 16 ++-- src/Config/StandardConfig.php | 2 +- src/Service/SessionConfigFactory.php | 3 +- test/Service/SessionConfigFactoryTest.php | 6 +- test/TestAsset/TestConfig.php | 96 +++++++++++++------ 6 files changed, 80 insertions(+), 45 deletions(-) diff --git a/docs/book/config.md b/docs/book/config.md index c32948d6..b6568bc7 100644 --- a/docs/book/config.md +++ b/docs/book/config.md @@ -22,7 +22,7 @@ Option | Data Type | Description `cookie_httponly` | `boolean` | Marks the cookie as accessible only through the HTTP protocol. `cookie_lifetime` | `integer` | Specifies the lifetime of the cookie in seconds which is sent to the browser. `cookie_path` | `string` | Specifies path to set in the session cookie. -`cookie_samesite` | `string` | Specifies whether cookies should be sent along with cross-site requests. +`cookie_samesite` | `string` | Specifies whether cookies should be sent along with cross-site requests. (Since 2.11.0) `cookie_secure` | `boolean` | Specifies whether cookies should only be sent over secure connections. `entropy_length` | `integer` | Specifies the number of bytes which will be read from the file specified in entropy_file. Removed in PHP 7.1.0. `entropy_file` | `string` | Defines a path to an external resource (file) which will be used as an additional entropy. Removed in PHP 7.1.0. diff --git a/src/Config/SameSiteCookieCapableInterface.php b/src/Config/SameSiteCookieCapableInterface.php index 60e8c794..2f5af695 100644 --- a/src/Config/SameSiteCookieCapableInterface.php +++ b/src/Config/SameSiteCookieCapableInterface.php @@ -1,15 +1,15 @@ [ - 'config_class' => TestConfig::class, + 'config_class' => TestConfig::class, 'cookie_samesite' => 'Lax', ], ] ); $this->expectException(ServiceNotCreatedException::class); - $this->expectExceptionMessage('Invalid configuration class "LaminasTest\Session\TestAsset\TestConfig". When configuration option "cookie_samesite" is used, the configuration class must implement Laminas\Session\Config\SameSiteCookieCapableInterface'); + $this->expectExceptionMessage('"cookie_samesite"'); $this->services->get(ConfigInterface::class); } } diff --git a/test/TestAsset/TestConfig.php b/test/TestAsset/TestConfig.php index 55b73835..82de07cc 100644 --- a/test/TestAsset/TestConfig.php +++ b/test/TestAsset/TestConfig.php @@ -1,134 +1,168 @@