Skip to content
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

Fixes for cookie signing #315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EventListener/SignedCookieListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function onKernelResponse(ResponseEvent $e): void
$response = $e->getResponse();

foreach ($response->headers->getCookies() as $cookie) {
if (true === $this->signedCookieNames || \in_array($cookie->getName(), $this->signedCookieNames, true)) {
if ($cookie->getValue() && (true === $this->signedCookieNames || \in_array($cookie->getName(), $this->signedCookieNames, true))) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if ($cookie->getValue() && (true === $this->signedCookieNames || \in_array($cookie->getName(), $this->signedCookieNames, true))) {
if (null !== $cookie->getValue() && (true === $this->signedCookieNames || \in_array($cookie->getName(), $this->signedCookieNames, true))) {

$response->headers->removeCookie($cookie->getName(), $cookie->getPath(), $cookie->getDomain());
$signedCookie = new Cookie(
$cookie->getName(),
Expand Down
4 changes: 2 additions & 2 deletions src/Signer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getSignedValue(string $value, ?string $signature = null): string
$signature = $this->generateSignature($value);
}

return $value.'.'.$signature;
return $value.','.$signature;
}

public function verifySignedValue(string $signedValue): bool
Expand Down Expand Up @@ -75,7 +75,7 @@ private function generateSignature(string $value): string
*/
private function splitSignatureFromSignedValue(string $signedValue): array
{
$pos = strrpos($signedValue, '.');
$pos = strrpos($signedValue, ',');
Copy link
Member

Choose a reason for hiding this comment

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

I'm not so sure about this.. On one hand it seems ok as a workaround, although I would like to still have BC here for . separated cookies so old cookies still work after deploying the new code (i.e. it should split using the last dot or comma found in the string if any).

On the other hand, it seems like the root cause is that you are signing session cookies, and

if ($this->signer->verifySignedValue($cookie)) {
$request->cookies->set($name, $this->signer->getVerifiedRawValue($cookie));
} else {
$request->cookies->remove($name);
}
does not update $_COOKIE directly so the NativeSessionHandler does not see the unsigned-cookie value, and it actually uses the signed value as session id which to me sounds still borked.

So IMO we should probably write to $_COOKIE to unsign the cookies as well as writing to the request.. Any opinion here @nicolas-grekas from the Symfony/session perspective?

Copy link
Member

Choose a reason for hiding this comment

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

Alternative fix would be to NOT sign the session cookie ever even if * is configured for signed cookies, because really that id should anyway be verified in the session storage to prevent session fixation, so I don't think signing this actually improves anything security wise.

if (false === $pos) {
return [$signedValue, null];
}
Expand Down
23 changes: 19 additions & 4 deletions tests/Listener/SignedCookieListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public function provideCookieReading(): array
[[], [], []],
[[], ['foo' => 'bar'], ['foo' => 'bar']],
[['foo'], ['foo' => 'bar'], []],
[['foo'], ['foo' => 'bar.ca3756f81d3728a023bdc8a622c0906f373b795e'], ['foo' => 'bar']],
[['*'], ['foo' => 'bar.ca3756f81d3728a023bdc8a622c0906f373b795e'], ['foo' => 'bar']],
[['foo'], ['foo' => 'bar,ca3756f81d3728a023bdc8a622c0906f373b795e'], ['foo' => 'bar']],
[['*'], ['foo' => 'bar,ca3756f81d3728a023bdc8a622c0906f373b795e'], ['foo' => 'bar']],
];
}

Expand Down Expand Up @@ -99,8 +99,8 @@ public function provideCookieWriting(): array
return [
[[], [], []],
[[], ['foo' => 'bar'], ['foo' => 'bar']],
[['foo'], ['foo' => 'bar'], ['foo' => 'bar.ca3756f81d3728a023bdc8a622c0906f373b795e']],
[['*'], ['foo' => 'bar'], ['foo' => 'bar.ca3756f81d3728a023bdc8a622c0906f373b795e']],
[['foo'], ['foo' => 'bar'], ['foo' => 'bar,ca3756f81d3728a023bdc8a622c0906f373b795e']],
[['*'], ['foo' => 'bar'], ['foo' => 'bar,ca3756f81d3728a023bdc8a622c0906f373b795e']],
];
}

Expand Down Expand Up @@ -129,4 +129,19 @@ public function testCookieWritingSkipsSubReqs(): void
$cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertSame('bar', $cookies['']['/']['foo']->getValue());
}

public function testCookieWritingHandlesEmptyValue(): void
{
$listener = new SignedCookieListener($this->signer, ['*']);
$request = Request::create('/');

$response = new Response();
$response->headers->setCookie(Cookie::create('foo'));

$event = $this->createResponseEventWithKernel($this->kernel, $request, true, $response);
$listener->onKernelResponse($event);

$cookies = $response->headers->getCookies(ResponseHeaderBag::COOKIES_ARRAY);
$this->assertNull($cookies['']['/']['foo']->getValue());
}
}