Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d4b434

Browse files
committed
Add test to clear CSRF on stateless request
1 parent 1d10333 commit 3d4b434

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Symfony/Component/Security/Http/Tests/EventListener/CsrfTokenClearingLogoutListenerTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\RequestStack;
18+
use Symfony\Component\HttpFoundation\Session\Session;
1819
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
1920
use Symfony\Component\Security\Http\Event\LogoutEvent;
2021
use Symfony\Component\Security\Http\EventListener\CsrfTokenClearingLogoutListener;
2122

2223
class CsrfTokenClearingLogoutListenerTest extends TestCase
2324
{
24-
public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
25+
public function testSkipsClearingSessionTokenStorageOnRequestWithoutSession()
2526
{
2627
try {
2728
(new CsrfTokenClearingLogoutListener(
@@ -33,4 +34,25 @@ public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
3334

3435
$this->addToAssertionCount(1);
3536
}
37+
38+
public function testSkipsClearingSessionTokenStorageOnStatelessRequest()
39+
{
40+
$session = new Session();
41+
42+
// Create a stateless request with a previous session
43+
$request = new Request();
44+
$request->setSession($session);
45+
$request->cookies->set($session->getName(), 'previous_session');
46+
$request->attributes->set('_stateless', true);
47+
48+
try {
49+
(new CsrfTokenClearingLogoutListener(
50+
new SessionTokenStorage(new RequestStack())
51+
))->onLogout(new LogoutEvent($request, null));
52+
} catch (SessionNotFoundException) {
53+
$this->fail('clear() must not be called if the request is stateless');
54+
}
55+
56+
$this->addToAssertionCount(1);
57+
}
3658
}

0 commit comments

Comments
 (0)