Skip to content

[Security] Use the session only if it is started when using SameOriginCsrfTokenManager #59146

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

Merged
merged 1 commit into from
Jan 2, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,17 @@ public function clearCookies(Request $request, Response $response): void

public function persistStrategy(Request $request): void
{
if ($request->hasSession(true) && $request->attributes->has($this->cookieName)) {
$request->getSession()->set($this->cookieName, $request->attributes->get($this->cookieName));
if (!$request->attributes->has($this->cookieName)
|| !$request->hasSession(true)
|| !($session = $request->getSession())->isStarted()
) {
return;
}

$usageIndexValue = $session instanceof Session ? $usageIndexReference = &$session->getUsageIndex() : 0;
$usageIndexReference = \PHP_INT_MIN;
$session->set($this->cookieName, $request->attributes->get($this->cookieName));
$usageIndexReference = $usageIndexValue;
}

public function onKernelResponse(ResponseEvent $event): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ public function testClearCookies()
$this->assertTrue($response->headers->has('Set-Cookie'));
}

public function testPersistStrategyWithSession()
public function testPersistStrategyWithStartedSession()
{
$session = $this->createMock(Session::class);
$session->method('isStarted')->willReturn(true);

$request = new Request();
$request->setSession($session);
$request->attributes->set('csrf-token', 2 << 8);
Expand All @@ -219,6 +221,19 @@ public function testPersistStrategyWithSession()
$this->csrfTokenManager->persistStrategy($request);
}

public function testPersistStrategyWithSessionNotStarted()
{
$session = $this->createMock(Session::class);

$request = new Request();
$request->setSession($session);
$request->attributes->set('csrf-token', 2 << 8);

$session->expects($this->never())->method('set');

$this->csrfTokenManager->persistStrategy($request);
}

public function testOnKernelResponse()
{
$request = new Request([], [], ['csrf-token' => 2], ['csrf-token_test' => 'csrf-token']);
Expand Down
Loading