diff --git a/README.md b/README.md index dca7210..ce88450 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,13 @@ IE Mobile, UC Browser for Android. Further details can be found here: -* [https://web.dev/samesite-cookies-explained](SameSite cookies explained) +* [SameSite cookies explained](https://web.dev/samesite-cookies-explained) * [CSRF is (really) dead](https://scotthelme.co.uk/csrf-is-really-dead/) * [PHP setcookie “SameSite=Strict”?](https://stackoverflow.com/questions/39750906/php-setcookie-samesite-strict) * [How to Set a cookie attribute Samesite value in PHP ?](https://www.tutorialshore.com/how-to-set-a-cookie-attribute-samesite-value-in-php/) * [Can I use SameSite?](https://caniuse.com/#feat=same-site-cookie-attribute) -## Usage +## Slim 4 integration Slim 4 uses a LIFO (last in, first out) middleware stack, so we have to add the middleware in reverse order: @@ -45,7 +45,8 @@ so we have to add the middleware in reverse order: ```php add(new SameSiteCookieMiddlware(true)); +$app->add(new SameSiteCookieMiddleware($configuration)); // Start the native PHP session handler and fetch the session attributes -$app->add(new SameSiteSessionMiddleware('Lax', true, true)); +$app->add(new SameSiteSessionMiddleware($configuration)); // ... diff --git a/composer.json b/composer.json index f6a8a79..3312c1c 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,11 @@ "psr/http-server-middleware": "^1.0" }, "require-dev": { + "nyholm/psr7": "^1.1", "overtrue/phplint": "^1.1", - "phpunit/phpunit": "^7", "phpstan/phpstan-shim": "^0.11", + "phpunit/phpunit": "^7", + "relay/relay": "^2.0", "squizlabs/php_codesniffer": "^3.4" }, "scripts": { diff --git a/phpunit.xml b/phpunit.xml index 1babff2..e3d5d55 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,5 +1,5 @@ -sameSite = $sameSite; - $this->httpOnly = $httpOnly; - $this->secure = $secure; + $this->sameSite = $configuration->sameSite; + $this->httpOnly = $configuration->httpOnly; + $this->secure = $configuration->secure; } /** @@ -57,20 +55,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $sessionName = $request->getAttribute('session_name'); $params = $request->getAttribute('session_cookie_params'); - if (version_compare(PHP_VERSION, '7.3.0') >= 0) { - // Remove invalid key - unset($params['lifetime']); - - $params['samesite'] = $this->sameSite; - $params['httponly'] = $this->httpOnly; - $params['secure'] = $this->secure; - - setcookie($sessionName, $sessionId, $params); - - return $response; - } - - // For older PHP versions $cookieValues = [ sprintf('%s=%s;', $sessionName, $sessionId), sprintf('path=%s;', $params['path']), @@ -88,8 +72,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $cookieValues[] = sprintf('SameSite=%s;', $this->sameSite); } - //$response = $response->withHeader('Set-Cookie', implode(' ', $cookieValues)); - header('Set-Cookie: ' . implode(' ', $cookieValues)); + $response = $response->withHeader('Set-Cookie', implode(' ', $cookieValues)); return $response; } diff --git a/src/SameSiteSessionMiddleware.php b/src/SameSiteSessionMiddleware.php index 6b82b53..f6340ce 100644 --- a/src/SameSiteSessionMiddleware.php +++ b/src/SameSiteSessionMiddleware.php @@ -20,11 +20,11 @@ final class SameSiteSessionMiddleware implements MiddlewareInterface /** * The constructor. * - * @param bool $startSession The the session + * @param SameSiteCookieConfiguration $configuration The configuration */ - public function __construct(bool $startSession = true) + public function __construct(SameSiteCookieConfiguration $configuration) { - $this->startSession = $startSession; + $this->startSession = $configuration->startSession; } /** diff --git a/tests/MiddlewareTestTrait.php b/tests/MiddlewareTestTrait.php new file mode 100644 index 0000000..eb5d0fd --- /dev/null +++ b/tests/MiddlewareTestTrait.php @@ -0,0 +1,41 @@ +createRequest(); + $relay = new Relay($queue); + + return $relay->handle($request); + } + + /** + * Factory. + * + * @return ServerRequestInterface + */ + protected function createRequest(): ServerRequestInterface + { + return (new Psr17Factory())->createServerRequest('GET', '/'); + } +} diff --git a/tests/ResponseFactoryMiddleware.php b/tests/ResponseFactoryMiddleware.php new file mode 100644 index 0000000..5663f3b --- /dev/null +++ b/tests/ResponseFactoryMiddleware.php @@ -0,0 +1,28 @@ +createResponse(); + } +} diff --git a/tests/SameSiteCookieMiddlewareTest.php b/tests/SameSiteCookieMiddlewareTest.php index a511a73..cff3650 100644 --- a/tests/SameSiteCookieMiddlewareTest.php +++ b/tests/SameSiteCookieMiddlewareTest.php @@ -3,19 +3,35 @@ namespace Selective\SameSiteCookie\Test; use PHPUnit\Framework\TestCase; +use Selective\SameSiteCookie\SameSiteCookieConfiguration; +use Selective\SameSiteCookie\SameSiteCookieMiddleware; +use Selective\SameSiteCookie\SameSiteSessionMiddleware; /** * Test. */ class SameSiteCookieMiddlewareTest extends TestCase { + use MiddlewareTestTrait; + /** * Test. * * @return void */ - public function testTrue(): void + public function testDefaultConfiguration(): void { - static::assertTrue(true); + $configuration = new SameSiteCookieConfiguration(); + + session_id('v3absd19o9pi6cjvhb5pkmsfo9'); + + $response = $this->runQueue([ + new SameSiteSessionMiddleware($configuration), + new SameSiteCookieMiddleware($configuration), + ]); + + $cookie = $response->getHeaderLine('Set-Cookie'); + static::assertSame('PHPSESSID=v3absd19o9pi6cjvhb5pkmsfo9; path=/; Secure; HttpOnly; SameSite=Lax;', $cookie); + static::assertSame('', (string)$response->getBody()); } } diff --git a/tests/boostrap.php b/tests/boostrap.php new file mode 100644 index 0000000..b8efaca --- /dev/null +++ b/tests/boostrap.php @@ -0,0 +1,5 @@ +