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

fix: backend article preview 'shop' cookie #2737

Open
wants to merge 1 commit into
base: 5.7
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
7 changes: 5 additions & 2 deletions engine/Shopware/Plugins/Default/Core/Router/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Shopware\Models\Shop\Shop;
use Shopware\Models\Shop\Template;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;

class Shopware_Plugins_Core_Router_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
Expand All @@ -53,14 +54,15 @@ public function install()
public function onRouteStartup(Enlight_Controller_EventArgs $args)
{
$request = $args->getRequest();
$response = $args->getResponse();

if (str_starts_with($request->getPathInfo(), '/backend')
|| str_starts_with($request->getPathInfo(), '/api/')
) {
return;
}

$shop = $this->getShopByRequest($request);
$shop = $this->getShopByRequest($request, $response);

if (!$shop->getHost()) {
$shop->setHost($request->getHttpHost());
Expand Down Expand Up @@ -306,7 +308,7 @@ protected function upgradeShop($request, $response)
*
* @return Shop
*/
protected function getShopByRequest(Request $request)
protected function getShopByRequest(Request $request, Response $response)
{
$repository = $this->get(ModelManager::class)->getRepository(Shop::class);

Expand All @@ -317,6 +319,7 @@ protected function getShopByRequest(Request $request)

if ($shop === null && $request->getCookie('shop') !== null) {
$shop = $repository->getActiveById($request->getCookie('shop'));
$response->headers->clearCookie('shop');
Copy link
Contributor

Choose a reason for hiding this comment

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

clearing the cookie here, would also affect the next if condition, if I see that correctly.

Before your change, the if condition could also be met, if this condition was met before.

With your change this is no longer possible.

A test of the \Shopware_Plugins_Core_Router_Bootstrap::onRouteStartup method in \Shopware\Tests\Functional\Plugins\Core\Router\BootstrapTest would be really nice, to ensure, that we do not break stuff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi, the main problem is, I don't understand the reason of this cookie.

I tested this change on my server and I still get redirect after the first attempt, trying to visit the start page. Only on the second attempt is the Cookie gone and I can visit the primary shop again.

But I don't know, if there is a case that the cookie is needed to redirect to the right shop.

Copy link
Contributor

Choose a reason for hiding this comment

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

do we need the cookie at all then? 🤔

unfortunately I cannot reproduce this locally. you wrote this only occurs via HTTPS? 🤔 If I am using only HTTP, the cookie is not set at all 🤔

}

if ($shop && $request->getCookie('shop') !== null && $request->getPost('__shop') === null) {
Expand Down
29 changes: 29 additions & 0 deletions tests/Functional/Plugins/Core/Router/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Enlight_Controller_Response_ResponseTestCase;
use PHPUnit\Framework\TestCase;
use Shopware\Tests\Functional\Traits\ContainerTrait;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;

class BootstrapTest extends TestCase
Expand All @@ -52,4 +53,32 @@ public function testOnRouteShutdown(): void
static::assertSame(Response::HTTP_MOVED_PERMANENTLY, $response->getStatusCode());
static::assertSame('shopware.php/www.test.de', $response->getHeader('location'));
}

public function testOnRouteStartupClearShopCookie(): void
{
$pluginBootstrap = $this->getContainer()->get('plugins')->Core()->Router();

$request = new Enlight_Controller_Request_RequestTestCase();
$request->setRequestUri('shopware.php/www.test.de');
$request->setCookie('shop', 2);
$response = new Enlight_Controller_Response_ResponseTestCase();
$args = new Enlight_Controller_EventArgs([
'request' => $request,
'response' => $response,
]);
$pluginBootstrap->onRouteStartup($args);

static::assertSame(Response::HTTP_OK, $response->getStatusCode());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mitelg, here 2 options for testing this, I'm not happy with both solutions

// Option A
static::assertIsString($response->headers->get('Set-Cookie'));
static::assertStringContainsString('shop=deleted', $response->headers->get('Set-Cookie'));

// Option B
$cookie = $response->headers->getCookies();
static::assertCount(1, $cookie);
static::assertInstanceOf(Cookie::class, $cookie[0]);
static::assertSame('shop', $cookie[0]->getName());
static::assertTrue($cookie[0]->isCleared());
}
}
Loading