8000 [Security][CSRF] Double Submit Cookies CSRF prevention strategy by backbone87 · Pull Request #18333 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security][CSRF] Double Submit Cookies CSRF prevention strategy #18333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Prev Previous commit
code style
  • Loading branch information
backbone87 committed Sep 28, 2016
commit 99dde3b93f8c8106ef09558217c84381f9eb0ae6
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct($cookies, $secure, $secret, $ttl = null)
$this->cookies = self::parseCookieHeader($cookies);
$this->secure = (bool) $secure;
< 10000 span class='blob-code-inner blob-code-marker ' data-code-marker=" "> $this->secret = (string) $secret;
$this->ttl = $ttl === null ? 60 * 60 : (int) $ttl;
$this->ttl = null === $ttl ? 60 * 60 : (int) $ttl;

if ('' === $this->secret) {
throw new InvalidArgumentException('Secret must be a non-empty string');
Expand Down Expand Up @@ -151,7 +151,7 @@ public function createCookies()
}
}

if ($token !== '') {
if ('' !== $token) {
$cookies[] = $this->createCookie($tokenId, $token);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CookieTokenStorageFactory implements TokenStorageFactoryInterface
public function __construct($secret, $ttl = null)
{
$this->secret = (string) $secret;
$this->ttl = $ttl === null ? 60 * 60 : (int) $ttl;
$this->ttl = null === $ttl ? 60 * 60 : (int) $ttl;

if ('' === $this->secret) {
throw new InvalidArgumentException('Secret must be a non-empty string');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
class CookieTokenStorageListener implements EventSubscriberInterface
{
/**
* @var string
*/
const DEFAULT_TOKEN_STORAGE_KEY = '_csrf_token_storage';

/**
* @var string
*/
Expand All @@ -36,9 +41,7 @@ class CookieTokenStorageListener implements EventSubscriberInterface
*/
public function __construct($tokenStorageKey = null)
{
// TODO should this class get its own DEFAULT_TOKEN_STORAGE_KEY constant?
// if no, where should the sole constant be put?
$this->tokenStorageKey = $tokenStorageKey === null ? RequestStackTokenStorage::DEFAULT_TOKEN_STORAGE_KEY : $tokenStorageKey;
$this->tokenStorageKey = null === $tokenStorageKey ? self::DEFAULT_TOKEN_STORAGE_KEY : $tokenStorageKey;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ class RequestStackTokenStorage extends AbstractTokenStorageProxy
* @param TokenStorageFactoryInterface $factory
* @param string|null $tokenStorageKey
*/
public function __construct(
RequestStack $requestStack,
TokenStorageFactoryInterface $factory,
$tokenStorageKey = null
) {
public function __construct(RequestStack $requestStack, TokenStorageFactoryInterface $factory, $tokenStorageKey = null)
{
$this->requestStack = $requestStack;
$this->factory = $factory;
$this->tokenStorageKey = $tokenStorageKey === null ? self::DEFAULT_TOKEN_STORAGE_KEY : $tokenStorageKey;
Expand All @@ -64,7 +61,6 @@ public function __construct(
*/
public function getProxiedTokenStorage()
{
// TODO use master or current request?
$request = $this->requestStack->getMasterRequest();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

master?


if (!$request) {
Expand All @@ -77,11 +73,8 @@ public function getProxiedTokenStorage()
return $storage;
}

if ($storage !== null) {
throw new UnexpectedValueException(sprintf(
'Expected null or an instance of "Symfony\\Component\\Security\\Csrf\\TokenStorage\\TokenStorageInterface", got "%s"',
is_object($storage) ? get_class($storage) : gettype($storage)
));
if (null !== $storage) {
throw new UnexpectedValueException(sprintf('Expected null or an implementation of "%s", got "%s"', TokenStorageInterface::class, is_object($storage) ? get_class($storage) : gettype($storage)));
}

$storage = $this->factory->createTokenStorage($request);
Expand Down
0