Skip to content

Commit 432552b

Browse files
ACPT-1718
Adding new CookieDisablerInterface so that Framework/App/PageCache/Kernel can work the same when not using PHP's built-in cookies.
1 parent 6e68d69 commit 432552b

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

app/code/Magento/Cookie/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\Framework\Stdlib\CookieManagerInterface" type="Magento\Framework\Stdlib\Cookie\PhpCookieManager" />
10+
<preference for="Magento\Framework\Stdlib\CookieDisablerInterface" type="Magento\Framework\Stdlib\Cookie\PhpCookieDisabler" />
1011
</config>
1112

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<preference for="Magento\Framework\Stdlib\Cookie\CookieScopeInterface" type="Magento\Framework\Stdlib\Cookie\CookieScope" />
9595
<preference for="Magento\Framework\Stdlib\Cookie\CookieReaderInterface" type="Magento\Framework\Stdlib\Cookie\PhpCookieReader" />
9696
<preference for="Magento\Framework\Stdlib\CookieManagerInterface" type="Magento\Framework\Stdlib\Cookie\PhpCookieManager" />
97+
<preference for="Magento\Framework\Stdlib\CookieDisablerInterface" type="Magento\Framework\Stdlib\Cookie\PhpCookieDisabler" />
9798
<preference for="Magento\Framework\TranslateInterface" type="Magento\Framework\Translate" />
9899
<preference for="Magento\Framework\Config\ScopeListInterface" type="interceptionConfigScope" />
99100
<preference for="Magento\Framework\View\Design\Theme\Label\ListInterface" type="Magento\Theme\Model\ResourceModel\Theme\Collection" />

dev/tests/api-functional/testsuite/Magento/GraphQl/GraphQl/GraphQlSessionTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function setUp(): void
4848
/**
4949
* Test for checking if graphQL query sets session cookies
5050
*
51+
* Note: The reason why the first response doesn't have cookies, but the subsequent responses do is
52+
* because Magento/Framework/App/PageCache/Kernel.php removes Set-Cookie headers when the response has a
53+
* public Cache-Control. This test asserts that behaviour.
54+
*
5155
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
5256
* @magentoConfigFixture graphql/session/disable 0
5357
*/
@@ -71,8 +75,7 @@ public function testCheckSessionCookieWithGetCategoryList(): void
7175
$result = $this->graphQlClient->getWithResponseHeaders($query, [], '', [], true);
7276
$this->assertEmpty($result['cookies']);
7377
// perform secondary request after cookies have been flushed
74-
$result = $this->graphQlClient->getWithResponseHeaders($query, [], '', []);
75-
78+
$result = $this->graphQlClient->getWithResponseHeaders($query, [], '', [], true);
7679
// may have other cookies than session
7780
$this->assertNotEmpty($result['cookies']);
7881
$this->assertAnyCookieMatchesRegex('/PHPSESSID=[a-z0-9]+;/', $result['cookies']);

lib/internal/Magento/Framework/App/PageCache/Kernel.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\State as AppState;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\Stdlib\CookieDisablerInterface;
1011

1112
/**
1213
* Builtin cache processor
@@ -68,6 +69,8 @@ class Kernel
6869
*/
6970
private $identifierForSave;
7071

72+
private readonly CookieDisablerInterface $cookieDisabler;
73+
7174
/**
7275
* @param Cache $cache
7376
* @param \Magento\Framework\App\PageCache\IdentifierInterface $identifier
@@ -79,6 +82,7 @@ class Kernel
7982
* @param AppState|null $state
8083
* @param \Magento\PageCache\Model\Cache\Type|null $fullPageCache
8184
* @param \Magento\Framework\App\PageCache\IdentifierInterface|null $identifierForSave
85+
* @param CookieDisablerInterface|null $cookieDisabler
8286
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8387
*/
8488
public function __construct(
@@ -91,7 +95,8 @@ public function __construct(
9195
\Magento\Framework\Serialize\SerializerInterface $serializer = null,
9296
AppState $state = null,
9397
\Magento\PageCache\Model\Cache\Type $fullPageCache = null,
94-
\Magento\Framework\App\PageCache\IdentifierInterface $identifierForSave = null
98+
\Magento\Framework\App\PageCache\IdentifierInterface $identifierForSave = null,
99+
?CookieDisablerInterface $cookieDisabler = null,
95100
) {
96101
$this->cache = $cache;
97102
$this->identifier = $identifier;
@@ -113,6 +118,7 @@ public function __construct(
113118
$this->identifierForSave = $identifierForSave ?? ObjectManager::getInstance()->get(
114119
\Magento\Framework\App\PageCache\IdentifierInterface::class
115120
);
121+
$this->cookieDisabler = $cookieDisabler ?? ObjectManager::getInstance()->get(CookieDisablerInterface::class);
116122
}
117123

118124
/**
@@ -163,9 +169,7 @@ public function process(\Magento\Framework\App\Response\Http $response)
163169
if ($this->state->getMode() != AppState::MODE_DEVELOPER) {
164170
$response->clearHeader('X-Magento-Tags');
165171
}
166-
if (!headers_sent()) {
167-
header_remove('Set-Cookie');
168-
}
172+
$this->cookieDisabler->setCookiesDisabled(true);
169173

170174
$this->fullPageCache->save(
171175
$this->serializer->serialize($this->getPreparedData($response)),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Stdlib\Cookie;
9+
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Stdlib\CookieDisablerInterface;
13+
use Magento\Framework\Stdlib\CookieManagerInterface;
14+
use Magento\Framework\Phrase;
15+
use Magento\Framework\HTTP\Header as HttpHeader;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Disables sending the cookies that are currently set.
20+
*/
21+
class PhpCookieDisabler implements CookieDisablerInterface
22+
{
23+
public function setCookiesDisabled(bool $disabled) : void
24+
{
25+
if ($disabled && !headers_sent()) {
26+
header_remove('Set-Cookie');
27+
}
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Stdlib;
8+
9+
/**
10+
* This interface is for when you need to disable all cookies from being sent in the HTTP response
11+
*/
12+
interface CookieDisablerInterface
13+
{
14+
public function setCookiesDisabled(bool $disabled) : void;
15+
}

0 commit comments

Comments
 (0)