Skip to content

Commit 7341225

Browse files
committed
Merge branch 'sw-26849/adjust-csrf-token' into '5.7'
SW-26849 - adjust csrf token See merge request shopware/5/product/shopware!939
2 parents 62e4ea2 + 2071340 commit 7341225

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

engine/Shopware/Components/CSRFTokenValidator.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,21 @@ public function checkFrontendTokenValidation(Enlight_Event_EventArgs $args)
170170

171171
public function clearExistingCookie(): void
172172
{
173+
$front = $this->container->get('front');
174+
173175
$shop = $this->contextService->getShopContext()->getShop();
174176
$name = $this->getCsrfName();
175177

176-
$front = $this->container->get('front');
177178
$response = $front->Response();
178-
$response->headers->clearCookie(
179+
$response->headers->setCookie(new Cookie(
179180
$name,
181+
Random::getAlphanumericString(30),
182+
0,
180183
sprintf('%s/', $shop->getPath() ?: ''),
181184
'',
182185
$shop->getSecure(),
183186
false
184-
);
187+
));
185188
}
186189

187190
/**

tests/Functional/Core/AdminTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
use Enlight_Controller_Front;
3434
use Enlight_Controller_Request_Request;
3535
use Enlight_Controller_Request_RequestHttp;
36+
use Enlight_Controller_Request_RequestTestCase;
37+
use Enlight_Controller_Response_ResponseTestCase;
3638
use Generator;
3739
use PHPUnit\Framework\TestCase;
3840
use sAdmin;
@@ -52,6 +54,8 @@
5254
use Shopware_Components_Config;
5355
use Shopware_Components_Snippet_Manager;
5456
use ShopwarePlugin\PaymentMethods\Components\BasePaymentMethod;
57+
use Symfony\Component\HttpFoundation\Cookie;
58+
use Symfony\Component\HttpFoundation\Response;
5559

5660
class AdminTest extends TestCase
5761
{
@@ -80,7 +84,8 @@ public function setUp(): void
8084
parent::setUp();
8185

8286
$this->getContainer()->get(ModelManager::class)->clear();
83-
$this->getContainer()->get('front')->setRequest(new Enlight_Controller_Request_RequestHttp());
87+
$this->getContainer()->get('front')->setRequest(new Enlight_Controller_Request_RequestTestCase());
88+
$this->getContainer()->get('front')->setResponse(new Enlight_Controller_Response_ResponseTestCase());
8489

8590
$this->module = $this->getContainer()->get('modules')->Admin();
8691
$this->config = $this->getContainer()->get('config');
@@ -2365,6 +2370,46 @@ public function testsGetPremiumShippingcostsWithCountryTaxRule(): void
23652370
static::assertSame($expectedTaxValue, $result['tax']);
23662371
}
23672372

2373+
public function testCsrfTokenAreUpdatedLogout(): void
2374+
{
2375+
static::assertCount(0, $this->getResponse()->headers->getCookies());
2376+
2377+
$customer = $this->createDummyCustomer();
2378+
2379+
// Test successful login
2380+
$this->getRequest()->setPost([
2381+
'email' => $customer->getEmail(),
2382+
'password' => 'fooobar',
2383+
]);
2384+
$this->module->sLogin();
2385+
2386+
$csrfCookies = array_filter($this->getResponse()->headers->getCookies(), function ($cookie) {
2387+
if ($cookie->getName() === '__csrf_token-1') {
2388+
return $cookie;
2389+
}
2390+
});
2391+
$cookie = array_pop($csrfCookies);
2392+
static::assertInstanceOf(Cookie::class, $cookie);
2393+
$token = $cookie->getValue();
2394+
static::assertIsString($token);
2395+
2396+
$this->getContainer()->get('front')->setResponse(new Enlight_Controller_Response_ResponseTestCase());
2397+
2398+
$this->module->logout();
2399+
2400+
$csrfCookies = array_filter($this->getResponse()->headers->getCookies(), function ($cookie) {
2401+
if ($cookie->getName() === '__csrf_token-1') {
2402+
return $cookie;
2403+
}
2404+
});
2405+
$cookie = array_pop($csrfCookies);
2406+
static::assertInstanceOf(Cookie::class, $cookie);
2407+
$newToken = $cookie->getValue();
2408+
static::assertIsString($newToken);
2409+
2410+
static::assertNotEquals($token, $newToken);
2411+
}
2412+
23682413
/**
23692414
* @param array<string, array<string, mixed>> $userData
23702415
*
@@ -2544,8 +2589,16 @@ private function deleteDummyCustomer(Customer $customer): void
25442589
private function getRequest(): Enlight_Controller_Request_Request
25452590
{
25462591
$request = $this->front->Request();
2547-
static::assertNotNull($request);
2592+
static::assertInstanceOf(Enlight_Controller_Request_Request::class, $request);
25482593

25492594
return $request;
25502595
}
2596+
2597+
private function getResponse(): Response
2598+
{
2599+
$response = $this->front->Response();
2600+
static::assertInstanceOf(Response::class, $response);
2601+
2602+
return $response;
2603+
}
25512604
}

0 commit comments

Comments
 (0)