Skip to content

Commit b88dcf0

Browse files
committed
[BUG#AC-2441] - Setting an invalid Cookie domain crash the website
1 parent 0763e06 commit b88dcf0

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

dev/tests/integration/testsuite/Magento/Cookie/Model/Config/Backend/DomainTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
namespace Magento\Cookie\Model\Config\Backend;
77

88
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use PHPUnit\Framework\TestCase;
911

1012
/**
1113
* Test \Magento\Cookie\Model\Config\Backend\Domain
1214
*
1315
* @magentoAppArea adminhtml
1416
*/
15-
class DomainTest extends \PHPUnit\Framework\TestCase
17+
class DomainTest extends TestCase
1618
{
1719
/**
1820
* @param string $value
@@ -22,9 +24,9 @@ class DomainTest extends \PHPUnit\Framework\TestCase
2224
*/
2325
public function testBeforeSave($value, $exceptionMessage = null)
2426
{
25-
/** @var $domain \Magento\Cookie\Model\Config\Backend\Domain */
26-
$domain = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
27-
\Magento\Cookie\Model\Config\Backend\Domain::class
27+
/** @var $domain Domain */
28+
$domain = Bootstrap::getObjectManager()->create(
29+
Domain::class
2830
);
2931
$domain->setValue($value);
3032
$domain->setPath('path');
@@ -45,18 +47,19 @@ public function testBeforeSave($value, $exceptionMessage = null)
4547
/**
4648
* @return array
4749
*/
48-
public function beforeSaveDataProvider()
50+
public function beforeSaveDataProvider(): array
4951
{
5052
return [
51-
'not string' => [['array'], 'Invalid domain name: must be a string'],
52-
'invalid hostname' => [
53+
'notString' => [['array'], 'Invalid domain name: must be a string'],
54+
'invalidHostname' => [
5355
'http://',
5456
'Invalid domain name: The input does not match the expected structure for a DNS hostname; '
5557
. 'The input does not appear to be a valid URI hostname; '
5658
. 'The input does not appear to be a valid local network name',
5759
],
58-
'valid hostname' => ['hostname.com'],
59-
'empty string' => [''],
60+
'validHostname' => ['hostname.com'],
61+
'emptyString' => [''],
62+
'invalidCharacter' => ['hostname,com', 'Invalid domain name: invalid character in cookie domain'],
6063
];
6164
}
6265
}

lib/internal/Magento/Framework/Session/Config/Validator/CookieDomainValidator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,43 @@
66

77
namespace Magento\Framework\Session\Config\Validator;
88

9+
use Laminas\Validator\Hostname;
10+
use Magento\Framework\Validator\AbstractValidator;
11+
912
/**
1013
* Session cookie domain validator
1114
*/
12-
class CookieDomainValidator extends \Magento\Framework\Validator\AbstractValidator
15+
class CookieDomainValidator extends AbstractValidator
1316
{
1417
/**
1518
* @inheritDoc
1619
*/
1720
public function isValid($value)
1821
{
1922
$this->_clearMessages();
23+
2024
if (!is_string($value)) {
2125
$this->_addMessages(['must be a string']);
26+
2227
return false;
2328
}
2429

25-
$validator = new \Laminas\Validator\Hostname(\Laminas\Validator\Hostname::ALLOW_ALL);
30+
//Hostname validator allows [;,] and returns the validator as true but these are unacceptable cookie domain
31+
//characters hence need explicit validation for the same
32+
if (preg_match('/[;,]/', $value)) {
33+
$this->_addMessages(['invalid character in cookie domain']);
34+
35+
return false;
36+
}
37+
38+
$validator = new Hostname(Hostname::ALLOW_ALL);
2639

2740
if (!empty($value) && !$validator->isValid($value)) {
2841
$this->_addMessages($validator->getMessages());
42+
2943
return false;
3044
}
45+
3146
return true;
3247
}
3348
}

0 commit comments

Comments
 (0)