Skip to content

Commit 56a7e9f

Browse files
committed
MAGETWO-95621: Path Validation in Templates
1 parent 2d9c1e6 commit 56a7e9f

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

lib/internal/Magento/Framework/View/Element/Template/File/Validator.php

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

88
use \Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Component\ComponentRegistrar;
10+
use \Magento\Framework\Filesystem\Driver\File as FileDriver;
1011

1112
/**
12-
* Class Validator
13-
* @package Magento\Framework\View\Element\Template\File
13+
* Class Validator.
1414
*/
1515
class Validator
1616
{
@@ -68,26 +68,34 @@ class Validator
6868
*/
6969
protected $_compiledDir;
7070

71+
/**
72+
* @var FileDriver
73+
*/
74+
private $fileDriver;
75+
7176
/**
7277
* Class constructor
7378
*
7479
* @param \Magento\Framework\Filesystem $filesystem
7580
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface
7681
* @param ComponentRegistrar $componentRegistrar
7782
* @param string|null $scope
83+
* @param FileDriver|null $fileDriver
7884
*/
7985
public function __construct(
8086
\Magento\Framework\Filesystem $filesystem,
8187
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface,
8288
ComponentRegistrar $componentRegistrar,
83-
$scope = null
89+
$scope = null,
90+
FileDriver $fileDriver = null
8491
) {
8592
$this->_filesystem = $filesystem;
8693
$this->_isAllowSymlinks = $scopeConfigInterface->getValue(self::XML_PATH_TEMPLATE_ALLOW_SYMLINK, $scope);
8794
$this->_themesDir = $componentRegistrar->getPaths(ComponentRegistrar::THEME);
8895
$this->moduleDirs = $componentRegistrar->getPaths(ComponentRegistrar::MODULE);
8996
$this->_compiledDir = $this->_filesystem->getDirectoryRead(DirectoryList::TEMPLATE_MINIFICATION_DIR)
9097
->getAbsolutePath();
98+
$this->fileDriver = $fileDriver ?: \Magento\Framework\App\ObjectManager::getInstance()->get(FileDriver::class);
9199
}
92100

93101
/**
@@ -128,7 +136,7 @@ protected function isPathInDirectories($path, $directories)
128136
$directories = (array)$directories;
129137
}
130138
foreach ($directories as $directory) {
131-
if (0 === strpos($path, $directory)) {
139+
if (0 === strpos($this->fileDriver->getRealPath($path), $directory)) {
132140
return true;
133141
}
134142
}

lib/internal/Magento/Framework/View/Test/Unit/Element/Template/File/ValidatorTest.php

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
2020
*
2121
* @var \Magento\Framework\View\Element\Template\File\Validator
2222
*/
23-
private $_validator;
23+
private $validator;
2424

2525
/**
2626
* Mock for view file system
2727
*
2828
* @var \Magento\Framework\FileSystem|\PHPUnit_Framework_MockObject_MockObject
2929
*/
30-
private $_fileSystemMock;
30+
private $fileSystemMock;
3131

3232
/**
3333
* Mock for scope config
3434
*
3535
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
3636
*/
37-
private $_scopeConfigMock;
37+
private $scopeConfigMock;
3838

3939
/**
4040
* Mock for root directory reader
@@ -62,12 +62,35 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
6262
*/
6363
protected function setUp()
6464
{
65-
$this->_fileSystemMock = $this->getMock('\Magento\Framework\Filesystem', [], [], '', false);
66-
$this->_scopeConfigMock = $this->getMock('\Magento\Framework\App\Config\ScopeConfigInterface');
67-
$this->rootDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
68-
$this->compiledDirectoryMock = $this->getMock('\Magento\Framework\Filesystem\Directory\ReadInterface');
65+
$this->fileSystemMock = $this->getMock(
66+
\Magento\Framework\Filesystem::class,
67+
[],
68+
[],
69+
'',
70+
false
71+
);
72+
$this->scopeConfigMock = $this->getMock(
73+
\Magento\Framework\App\Config\ScopeConfigInterface::class,
74+
[],
75+
[],
76+
'',
77+
false
78+
);
79+
$this->rootDirectoryMock = $this->getMock(
80+
\Magento\Framework\Filesystem\Directory\ReadInterface::class,
81+
[],
82+
[],
83+
'',
84+
false);
85+
$this->compiledDirectoryMock = $this->getMock(
86+
\Magento\Framework\Filesystem\Directory\ReadInterface::class,
87+
[],
88+
[],
89+
'',
90+
false
91+
);
6992

70-
$this->_fileSystemMock->expects($this->any())
93+
$this->fileSystemMock->expects($this->any())
7194
->method('getDirectoryRead')
7295
->will($this->returnValueMap(
7396
[
@@ -80,7 +103,13 @@ protected function setUp()
80103
->method('getAbsolutePath')
81104
->will($this->returnValue('/magento/var/compiled'));
82105

83-
$this->componentRegistrar = $this->getMock('Magento\Framework\Component\ComponentRegistrar', [], [], '', false);
106+
$this->componentRegistrar = $this->getMock(
107+
\Magento\Framework\Component\ComponentRegistrar::class,
108+
[],
109+
[],
110+
'',
111+
false
112+
);
84113
$this->componentRegistrar->expects($this->any())
85114
->method('getPaths')
86115
->will(
@@ -91,10 +120,18 @@ protected function setUp()
91120
]
92121
)
93122
);
94-
$this->_validator = new \Magento\Framework\View\Element\Template\File\Validator(
95-
$this->_fileSystemMock,
96-
$this->_scopeConfigMock,
97-
$this->componentRegistrar
123+
124+
$fileDriverMock = $this->getMock(\Magento\Framework\Filesystem\Driver\File::class);
125+
$fileDriverMock->expects($this->any())
126+
->method('getRealPath')
127+
->willReturnArgument(0);
128+
129+
$this->validator = new \Magento\Framework\View\Element\Template\File\Validator(
130+
$this->fileSystemMock,
131+
$this->scopeConfigMock,
132+
$this->componentRegistrar,
133+
null,
134+
$fileDriverMock
98135
);
99136
}
100137

@@ -103,23 +140,22 @@ protected function setUp()
103140
*
104141
* @param string $file
105142
* @param bool $expectedResult
106-
*
107-
* @dataProvider testIsValidDataProvider
108-
*
109143
* @return void
144+
*
145+
* @dataProvider isValidDataProvider
110146
*/
111147
public function testIsValid($file, $expectedResult)
112148
{
113149
$this->rootDirectoryMock->expects($this->any())->method('isFile')->will($this->returnValue(true));
114-
$this->assertEquals($expectedResult, $this->_validator->isValid($file));
150+
$this->assertEquals($expectedResult, $this->validator->isValid($file));
115151
}
116152

117153
/**
118154
* Data provider for testIsValid
119155
*
120-
* @return []
156+
* @return array
121157
*/
122-
public function testIsValidDataProvider()
158+
public function isValidDataProvider()
123159
{
124160
return [
125161
'empty' => ['', false],

0 commit comments

Comments
 (0)