-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
[RTM] Store the request token as cookie instead of the session #1065
[RTM] Store the request token as cookie instead of the session #1065
Conversation
a0b0742
to
93b65a0
Compare
src/Csrf/MemoryTokenStorage.php
Outdated
| * | ||
| * @return array | ||
| */ | ||
| public function getSaveTokens() |
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 like this method name, shouldn't it be getActiveTokens if the internal variable is called $activeTokens? Also, I would prefer to name that usedTokens 😁
The method can be simplified like this:
return array_intersect_key($this->tokens, $this->activeTokens);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.
Done in c3f6afb
| * @param int $cookieLifetime | ||
| * @param string $cookiePrefix | ||
| */ | ||
| public function __construct(MemoryTokenStorage $tokenStorage, int $cookieLifetime = 86400, string $cookiePrefix = 'csrf_') |
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.
Does this have to be a MemoryTokenStorage? Shouldn't we expect the interface?
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.
We call the initialize() method on it, so we cannot use the interface.
| $tokens = []; | ||
|
|
||
| foreach ($cookies as $key => $value) { | ||
| if (strncmp($key, $this->cookiePrefix, strlen($this->cookiePrefix)) === 0) { |
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.
strpos($this->cookiePrefix, $key) === 0 ?
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.
strncmp() should be way faster IMO.
src/Resources/config/listener.yml
Outdated
| - "@contao.csrf.token_storage" | ||
| tags: | ||
| - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 20 } | ||
| - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: 0 } |
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.
you don't need to set a priority if it's 0.
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.
Done in d874349
| $isSecure = $event->getRequest()->isSecure(); | ||
|
|
||
| foreach ($this->tokenStorage->getUsedTokens() as $key => $value) { | ||
| $event->getResponse()->headers->setCookie( |
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 think you should use deleteCookie if the value is null. This way Symfony handles the unset case.
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.
deleteCookie needs the same arguments as setCookie, except value and lifetime. Using deleteCookie would make a block of duplicate code.
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.
but it would move the delete logic to the header bag instead of our class, right?
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.
If you refer to $value === null ? 1 : $cookieLifetime as the delete logic, then yes.
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.
Looks good to me, can you add an issue to the header-replay-bundle so I can work on what's needed to make sure that cookie is ignored from executing a preflight request?
ed0a7e5
to
3dadd44
Compare
0e0a15f
to
b142160
Compare
b142160
to
e27ac94
Compare
Use a double submit cookie instead of storing the request token in the session of the user.