Skip to content

Commit

Permalink
bug #36175 [Security/Http] Remember me: allow to set the samesite coo…
Browse files Browse the repository at this point in the history
…kie flag (dunglas)

This PR was merged into the 3.4 branch.

Discussion
----------

[Security/Http] Remember me: allow to set the samesite cookie flag

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Similar to #35605, since Chrome 80 is going to require the `samesite` attribute.

This is a cherry-pick of #27976

Commits
-------

f0ceb73 [Security] Remember me: allow to set the samesite cookie flag
  • Loading branch information
nicolas-grekas committed Mar 23, 2020
2 parents b4ec8b9 + f0ceb73 commit 438d9e5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
Expand Up @@ -25,6 +25,7 @@ class RememberMeFactory implements SecurityFactoryInterface
'domain' => null,
'secure' => false,
'httponly' => true,
'samesite' => null,
'always_remember_me' => false,
'remember_me_parameter' => '_remember_me',
];
Expand Down
Expand Up @@ -38,6 +38,7 @@ abstract class AbstractRememberMeServices implements RememberMeServicesInterface
protected $options = [
'secure' => false,
'httponly' => true,
'samesite' => null,
];
private $providerKey;
private $secret;
Expand Down Expand Up @@ -281,7 +282,7 @@ protected function cancelCookie(Request $request)
$this->logger->debug('Clearing remember-me cookie.', ['name' => $this->options['name']]);
}

$request->attributes->set(self::COOKIE_ATTR_NAME, new Cookie($this->options['name'], null, 1, $this->options['path'], $this->options['domain'], $this->options['secure'], $this->options['httponly']));
$request->attributes->set(self::COOKIE_ATTR_NAME, new Cookie($this->options['name'], null, 1, $this->options['path'], $this->options['domain'], $this->options['secure'], $this->options['httponly'], false, $this->options['samesite']));
}

/**
Expand Down
Expand Up @@ -84,7 +84,9 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
$this->options['path'],
$this->options['domain'],
$this->options['secure'],
$this->options['httponly']
$this->options['httponly'],
false,
$this->options['samesite']
)
);

Expand Down Expand Up @@ -117,7 +119,9 @@ protected function onLoginSuccess(Request $request, Response $response, TokenInt
$this->options['path'],
$this->options['domain'],
$this->options['secure'],
$this->options['httponly']
$this->options['httponly'],
false,
$this->options['samesite']
)
);
}
Expand Down
Expand Up @@ -81,7 +81,9 @@ protected function onLoginSuccess(Request $request, Response $response, TokenInt
$this->options['path'],
$this->options['domain'],
$this->options['secure'],
$this->options['httponly']
$this->options['httponly'],
false,
$this->options['samesite']
)
);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Http\Tests\RememberMe;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
Expand Down Expand Up @@ -268,7 +269,7 @@ public function testLoginFail()

public function testLoginSuccessSetsCookieWhenLoggedInWithNonRememberMeTokenInterfaceImplementation()
{
$service = $this->getService(null, ['name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true]);
$service = $this->getService(null, ['name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'samesite' => Cookie::SAMESITE_STRICT, 'lifetime' => 3600, 'always_remember_me' => true]);
$request = new Request();
$response = new Response();

Expand Down Expand Up @@ -305,6 +306,7 @@ public function testLoginSuccessSetsCookieWhenLoggedInWithNonRememberMeTokenInte
$this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
$this->assertEquals('myfoodomain.foo', $cookie->getDomain());
$this->assertEquals('/foo/path', $cookie->getPath());
$this->assertSame(Cookie::SAMESITE_STRICT, $cookie->getSameSite());
}

protected function encodeCookie(array $parts)
Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Http\Tests\RememberMe;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
Expand Down Expand Up @@ -205,7 +206,7 @@ public function testLoginSuccessIgnoresTokensWhichDoNotContainAnUserInterfaceImp

public function testLoginSuccess()
{
$service = $this->getService(null, ['name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'lifetime' => 3600, 'always_remember_me' => true]);
$service = $this->getService(null, ['name' => 'foo', 'domain' => 'myfoodomain.foo', 'path' => '/foo/path', 'secure' => true, 'httponly' => true, 'samesite' => Cookie::SAMESITE_STRICT, 'lifetime' => 3600, 'always_remember_me' => true]);
$request = new Request();
$response = new Response();

Expand Down Expand Up @@ -240,6 +241,7 @@ public function testLoginSuccess()
$this->assertTrue($cookie->getExpiresTime() > time() + 3590 && $cookie->getExpiresTime() < time() + 3610);
$this->assertEquals('myfoodomain.foo', $cookie->getDomain());
$this->assertEquals('/foo/path', $cookie->getPath());
$this->assertSame(Cookie::SAMESITE_STRICT, $cookie->getSameSite());
}

protected function getCookie($class, $username, $expires, $password)
Expand Down

0 comments on commit 438d9e5

Please sign in to comment.