Skip to content

Commit 9d99362

Browse files
authored
LYNX-339: private_content_version cookie returned in GQL queries
1 parent df72199 commit 9d99362

File tree

2 files changed

+119
-37
lines changed

2 files changed

+119
-37
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\GraphQl\PageCache;
18+
19+
use Magento\TestFramework\TestCase\GraphQlAbstract;
20+
use Magento\TestFramework\Fixture\Config;
21+
use Magento\Framework\App\PageCache\Version;
22+
23+
/**
24+
* Test absence/presence of private_content_version cookie in GraphQl POST HTTP responses
25+
*/
26+
class DisableSessionTest extends GraphQlAbstract
27+
{
28+
#[
29+
Config('graphql/session/disable', '1')
30+
]
31+
public function testPrivateSessionContentCookieNotPresentWhenSessionDisabled()
32+
{
33+
$result = $this->graphQlMutationWithResponseHeaders($this->getMutation());
34+
$this->assertArrayHasKey('headers', $result);
35+
if (!empty($result['headers']['Set-Cookie'])) {
36+
$this->assertStringNotContainsString(
37+
Version::COOKIE_NAME,
38+
$result['headers']['Set-Cookie'],
39+
Version::COOKIE_NAME . ' should not be present in Set-Cookie header'
40+
);
41+
}
42+
}
43+
44+
#[
45+
Config('graphql/session/disable', '0')
46+
]
47+
public function testPrivateSessionContentCookiePresentWhenSessionEnabled()
48+
{
49+
$result = $this->graphQlMutationWithResponseHeaders($this->getMutation());
50+
$this->assertArrayHasKey('headers', $result);
51+
$this->assertArrayHasKey('Set-Cookie', $result['headers'], 'Set-Cookie HTTP response header should be present');
52+
$this->assertStringContainsString(
53+
Version::COOKIE_NAME,
54+
$result['headers']['Set-Cookie'],
55+
Version::COOKIE_NAME . ' should be set by the server'
56+
);
57+
}
58+
59+
/**
60+
* Provides dummy mutation to test GraphQl HTTP POST response
61+
*
62+
* @return string
63+
*/
64+
private function getMutation(): string
65+
{
66+
return <<<GRAPHQL
67+
mutation {
68+
createEmptyCart
69+
}
70+
GRAPHQL;
71+
}
72+
}

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

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Framework\App\PageCache;
78

9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Magento\Framework\Stdlib\CookieManagerInterface;
11+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
12+
use Magento\Framework\App\Request\Http;
13+
814
/**
915
* PageCache Version
1016
*
@@ -15,53 +21,38 @@ class Version
1521
/**
1622
* Name of cookie that holds private content version
1723
*/
18-
const COOKIE_NAME = 'private_content_version';
24+
public const COOKIE_NAME = 'private_content_version';
1925

2026
/**
2127
* Ten years cookie period
2228
*/
23-
const COOKIE_PERIOD = 315360000;
24-
25-
/**
26-
* Cookie Manager
27-
*
28-
* @var \Magento\Framework\Stdlib\CookieManagerInterface
29-
*/
30-
protected $cookieManager;
31-
32-
/**
33-
* Request
34-
*
35-
* @var \Magento\Framework\App\Request\Http
36-
*/
37-
protected $request;
29+
public const COOKIE_PERIOD = 315360000;
3830

3931
/**
40-
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
32+
* Config setting for disabling session for GraphQl
4133
*/
42-
protected $cookieMetadataFactory;
34+
private const XML_PATH_GRAPHQL_DISABLE_SESSION = 'graphql/session/disable';
4335

4436
/**
45-
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
46-
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
47-
* @param \Magento\Framework\App\Request\Http $request
37+
* @param CookieManagerInterface $cookieManager
38+
* @param CookieMetadataFactory $cookieMetadataFactory
39+
* @param Http $request
40+
* @param ScopeConfigInterface $scopeConfig
4841
*/
4942
public function __construct(
50-
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
51-
\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
52-
\Magento\Framework\App\Request\Http $request
43+
private readonly CookieManagerInterface $cookieManager,
44+
private readonly CookieMetadataFactory $cookieMetadataFactory,
45+
private readonly Http $request,
46+
private readonly ScopeConfigInterface $scopeConfig
5347
) {
54-
$this->cookieManager = $cookieManager;
55-
$this->request = $request;
56-
$this->cookieMetadataFactory = $cookieMetadataFactory;
5748
}
5849

5950
/**
6051
* Generate unique version identifier
6152
*
6253
* @return string
6354
*/
64-
protected function generateValue()
55+
protected function generateValue(): string
6556
{
6657
//phpcs:ignore
6758
return md5(rand() . time());
@@ -75,16 +66,35 @@ protected function generateValue()
7566
*
7667
* @return void
7768
*/
78-
public function process()
69+
public function process(): void
7970
{
80-
if ($this->request->isPost()) {
81-
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
82-
->setDuration(self::COOKIE_PERIOD)
83-
->setPath('/')
84-
->setSecure($this->request->isSecure())
85-
->setHttpOnly(false)
86-
->setSameSite('Lax');
87-
$this->cookieManager->setPublicCookie(self::COOKIE_NAME, $this->generateValue(), $publicCookieMetadata);
71+
if (!$this->request->isPost()) {
72+
return;
8873
}
74+
75+
if ($this->request->getOriginalPathInfo() === '/graphql' && $this->isSessionDisabled() === true) {
76+
return;
77+
}
78+
79+
$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
80+
->setDuration(self::COOKIE_PERIOD)
81+
->setPath('/')
82+
->setSecure($this->request->isSecure())
83+
->setHttpOnly(false)
84+
->setSameSite('Lax');
85+
$this->cookieManager->setPublicCookie(self::COOKIE_NAME, $this->generateValue(), $publicCookieMetadata);
86+
}
87+
88+
/**
89+
* Returns configuration setting for disable session for GraphQl
90+
*
91+
* @return bool
92+
*/
93+
private function isSessionDisabled(): bool
94+
{
95+
return (bool)$this->scopeConfig->getValue(
96+
self::XML_PATH_GRAPHQL_DISABLE_SESSION,
97+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
98+
);
8999
}
90100
}

0 commit comments

Comments
 (0)