-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Csrf] Moved CSRF component outside Security namespace #36423
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.1.0 | ||
----- | ||
|
||
* Moved the component from `Symfony\Component\Security\Csrf` to `Symfony\Component\Csrf` | ||
* `InvalidArgumentException` and `TokenNotFoundException` no longer implement `Symfony\Component\Security\Core\Exception\ExceptionInterface` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Csrf; | ||
|
||
/** | ||
* A CSRF token. | ||
* | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class CsrfToken | ||
{ | ||
private $id; | ||
private $value; | ||
|
||
public function __construct(string $id, ?string $value) | ||
{ | ||
$this->id = $id; | ||
$this->value = $value ?? ''; | ||
} | ||
|
||
/** | ||
* Returns the ID of the CSRF token. | ||
* | ||
* @return string The token ID | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* Returns the value of the CSRF token. | ||
* | ||
* @return string The token value | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
/** | ||
* Returns the value of the CSRF token. | ||
* | ||
* @return string The token value | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->value; | ||
} | ||
} | ||
class_alias(CsrfToken::class, \Symfony\Component\Security\Csrf\CsrfToken::class); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Csrf; | ||
|
||
use Symfony\Component\Csrf\Exception\InvalidArgumentException; | ||
use Symfony\Component\Csrf\TokenGenerator\TokenGeneratorInterface; | ||
use Symfony\Component\Csrf\TokenGenerator\UriSafeTokenGenerator; | ||
use Symfony\Component\Csrf\TokenStorage\NativeSessionTokenStorage; | ||
use Symfony\Component\Csrf\TokenStorage\TokenStorageInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* Default implementation of {@link CsrfTokenManagerInterface}. | ||
* | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class CsrfTokenManager implements CsrfTokenManagerInterface | ||
{ | ||
private $generator; | ||
private $storage; | ||
private $namespace; | ||
|
||
/** | ||
* @param string|RequestStack|callable|null $namespace | ||
* * null: generates a namespace using $_SERVER['HTTPS'] | ||
* * string: uses the given string | ||
* * RequestStack: generates a namespace using the current master request | ||
* * callable: uses the result of this callable (must return a string) | ||
*/ | ||
public function __construct(TokenGeneratorInterface $generator = null, TokenStorageInterface $storage = null, $namespace = null) | ||
{ | ||
$this->generator = $generator ?: new UriSafeTokenGenerator(); | ||
$this->storage = $storage ?: new NativeSessionTokenStorage(); | ||
|
||
$superGlobalNamespaceGenerator = function () { | ||
return !empty($_SERVER['HTTPS']) && 'off' !== strtolower($_SERVER['HTTPS']) ? 'https-' : ''; | ||
}; | ||
|
||
if (null === $namespace) { | ||
$this->namespace = $superGlobalNamespaceGenerator; | ||
} elseif ($namespace instanceof RequestStack) { | ||
$this->namespace = function () use ($namespace, $superGlobalNamespaceGenerator) { | ||
if ($request = $namespace->getMasterRequest()) { | ||
return $request->isSecure() ? 'https-' : ''; | ||
} | ||
|
||
return $superGlobalNamespaceGenerator(); | ||
}; | ||
} elseif (\is_callable($namespace) || \is_string($namespace)) { | ||
$this->namespace = $namespace; | ||
} else { | ||
throw new InvalidArgumentException(sprintf('$namespace must be a string, a callable returning a string, null or an instance of "RequestStack". "%s" given.', get_debug_type($namespace))); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getToken(string $tokenId) | ||
{ | ||
$namespacedId = $this->getNamespace().$tokenId; | ||
if ($this->storage->hasToken($namespacedId)) { | ||
$value = $this->storage->getToken($namespacedId); | ||
} else { | ||
$value = $this->generator->generateToken(); | ||
|
||
$this->storage->setToken($namespacedId, $value); | ||
} | ||
|
||
return new CsrfToken($tokenId, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function refreshToken(string $tokenId) | ||
{ | ||
$namespacedId = $this->getNamespace().$tokenId; | ||
$value = $this->generator->generateToken(); | ||
|
||
$this->storage->setToken($namespacedId, $value); | ||
|
||
return new CsrfToken($tokenId, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function removeToken(string $tokenId) | ||
{ | ||
return $this->storage->removeToken($this->getNamespace().$tokenId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isTokenValid(CsrfToken $token) | ||
{ | ||
$namespacedId = $this->getNamespace().$token->getId(); | ||
if (!$this->storage->hasToken($namespacedId)) { | ||
return false; | ||
} | ||
|
||
return hash_equals($this->storage->getToken($namespacedId), $token->getValue()); | ||
} | ||
|
||
private function getNamespace(): string | ||
{ | ||
return \is_callable($ns = $this->namespace) ? $ns() : $ns; | ||
} | ||
} | ||
class_alias(CsrfTokenManager::class, \Symfony\Component\Security\Csrf\CsrfTokenManager::class); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ALL similar changes are BC breaks
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure about this? There is a
class_alias
between the old and the new namespace.I just tested this by linking the new CSRF component, and the old CSRF component + twig bridge into the
vendor/
directory (+ autoload config). Then this code works as expected (without error, but with the deprecation):