-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Make statefull pages NOT turn responses private when the token is not used #28089
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
51 changes: 51 additions & 0 deletions
51
...ymfony/Component/Security/Core/Authentication/Token/Storage/UsageTrackingTokenStorage.php
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,51 @@ | ||
<?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\Security\Core\Authentication\Token\Storage; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
/** | ||
* A token storage that optionally calls a closure when the token is accessed. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class UsageTrackingTokenStorage implements UsageTrackingTokenStorageInterface | ||
{ | ||
private $storage; | ||
private $usageTracker; | ||
|
||
public function __construct(TokenStorageInterface $storage = null) | ||
{ | ||
$this->storage = $storage ?? new TokenStorage(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getToken(bool $trackUsage = true): ?TokenInterface | ||
{ | ||
if ($trackUsage && $usageTracker = $this->usageTracker) { | ||
$usageTracker(); | ||
} | ||
|
||
return $this->storage->getToken(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setToken(TokenInterface $token = null, \Closure $usageTracker = null): void | ||
{ | ||
$this->usageTracker = $usageTracker; | ||
$this->storage->setToken($token); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...mponent/Security/Core/Authentication/Token/Storage/UsageTrackingTokenStorageInterface.php
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,32 @@ | ||
<?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\Security\Core\Authentication\Token\Storage; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
/** | ||
* A token storage that optionally calls a closure when the token is accessed. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface UsageTrackingTokenStorageInterface extends TokenStorageInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getToken(bool $trackUsage = true): ?TokenInterface; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setToken(TokenInterface $token = null, \Closure $usageTracker = null): void; | ||
} |
43 changes: 43 additions & 0 deletions
43
...ponent/Security/Core/Tests/Authentication/Token/Storage/UsageTrackingTokenStorageTest.php
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,43 @@ | ||
<?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\Security\Core\Tests\Authentication\Token\Storage; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage; | ||
|
||
class UsageTrackingTokenStorageTest extends TestCase | ||
{ | ||
public function testGetSetToken() | ||
{ | ||
$counter = 0; | ||
$usageTracker = function () use (&$counter) { ++$counter; }; | ||
|
||
$tokenStorage = new UsageTrackingTokenStorage(); | ||
$this->assertNull($tokenStorage->getToken()); | ||
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); | ||
|
||
$tokenStorage->setToken($token); | ||
$this->assertSame($token, $tokenStorage->getToken()); | ||
|
||
$tokenStorage->setToken($token, $usageTracker); | ||
$this->assertSame($token, $tokenStorage->getToken(false)); | ||
$this->assertSame(0, $counter); | ||
$this->assertSame($token, $tokenStorage->getToken()); | ||
$this->assertSame(1, $counter); | ||
|
||
$tokenStorage->setToken(null, $usageTracker); | ||
$this->assertNull($tokenStorage->getToken(false)); | ||
$this->assertSame(1, $counter); | ||
$this->assertNull($tokenStorage->getToken()); | ||
$this->assertSame(2, $counter); | ||
} | ||
} |
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
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.
I have the feeling this is a step backwards in terms of code quality 😢
When I see this and then look at the interface I'm confused:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authentication/Token/Storage/TokenStorageInterface.php#L28
So here we kind of code against the implementation
UsageTrackingTokenStorage
and not against the abstraction/interface anymore.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.
Also if someone decorated the original
security.token_storage
service this will not work properly, right? (as the argument will be missing in anyparent::getToken()
call because its simply not part of the contract).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.
fixed by adding a new
UsageTrackingTokenStorageInterface
The behavior will be the same as currently. That's the typical downside with service decoration: you're required to adapt to changes done at the decorated level. It's not very different from being required to write new code to leverage any new features. There is no way around.
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.
the issue here is that your new interface adds a new argument to the same method, making it really weird for decorators
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.
I don't understand what/why this is weird. This works fine.
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.
@stof I think that's OK. We don't want ppl to have to use two different methods for fetching the token. And this is in a separate interface (
UsageTrackingTokenStorageInterface
) so that's documented. We don't want ppl to decorate blindly btw.