Skip to content

Commit 8d4f861

Browse files
committed
Merge branch 'MC-40902' of github.com:magento-tsg/magento2ce into cia-2.3.7-3112021
2 parents 2028328 + 04e2194 commit 8d4f861

File tree

2 files changed

+150
-2
lines changed

2 files changed

+150
-2
lines changed

lib/internal/Magento/Framework/App/StaticResource.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\Config\ConfigOptionsListConstants;
12+
use Magento\Framework\Validator\Locale;
13+
use Magento\Framework\View\Design\Theme\ThemePackageList;
1214
use Psr\Log\LoggerInterface;
1315
use Magento\Framework\Debug;
1416
use Magento\Framework\Filesystem\Driver\File;
@@ -80,6 +82,16 @@ class StaticResource implements \Magento\Framework\AppInterface
8082
*/
8183
private $driver;
8284

85+
/**
86+
* @var ThemePackageList
87+
*/
88+
private $themePackageList;
89+
90+
/**
91+
* @var Locale
92+
*/
93+
private $localeValidator;
94+
8395
/**
8496
* @param State $state
8597
* @param Response\FileInterface $response
@@ -91,6 +103,8 @@ class StaticResource implements \Magento\Framework\AppInterface
91103
* @param ConfigLoaderInterface $configLoader
92104
* @param DeploymentConfig|null $deploymentConfig
93105
* @param File|null $driver
106+
* @param ThemePackageList|null $themePackageList
107+
* @param Locale|null $localeValidator
94108
*
95109
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
96110
*/
@@ -104,7 +118,9 @@ public function __construct(
104118
\Magento\Framework\ObjectManagerInterface $objectManager,
105119
ConfigLoaderInterface $configLoader,
106120
DeploymentConfig $deploymentConfig = null,
107-
File $driver = null
121+
File $driver = null,
122+
ThemePackageList $themePackageList = null,
123+
Locale $localeValidator = null
108124
) {
109125
$this->state = $state;
110126
$this->response = $response;
@@ -116,6 +132,8 @@ public function __construct(
116132
$this->configLoader = $configLoader;
117133
$this->deploymentConfig = $deploymentConfig ?: ObjectManager::getInstance()->get(DeploymentConfig::class);
118134
$this->driver = $driver ?: ObjectManager::getInstance()->get(File::class);
135+
$this->themePackageList = $themePackageList ?? ObjectManager::getInstance()->get(ThemePackageList::class);
136+
$this->localeValidator = $localeValidator ?? ObjectManager::getInstance()->get(Locale::class);
119137
}
120138

121139
/**
@@ -138,6 +156,16 @@ public function launch()
138156
} else {
139157
$path = $this->request->get('resource');
140158
$params = $this->parsePath($path);
159+
if (!($this->isThemeAllowed($params['area'] . DIRECTORY_SEPARATOR . $params['theme'])
160+
&& $this->localeValidator->isValid($params['locale']))
161+
) {
162+
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
163+
$this->response->setHttpResponseCode(404);
164+
return $this->response;
165+
}
166+
throw new \InvalidArgumentException('Requested path ' . $path . ' is wrong.');
167+
}
168+
141169
$this->state->setAreaCode($params['area']);
142170
$this->objectManager->configure($this->configLoader->load($params['area']));
143171
$file = $params['file'];
@@ -236,4 +264,15 @@ private function getLogger()
236264

237265
return $this->logger;
238266
}
267+
268+
/**
269+
* Check that theme is available.
270+
*
271+
* @param string $theme
272+
* @return bool
273+
*/
274+
private function isThemeAllowed(string $theme): bool
275+
{
276+
return in_array($theme, array_keys($this->themePackageList->getThemes()));
277+
}
239278
}

lib/internal/Magento/Framework/App/Test/Unit/StaticResourceTest.php

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Psr\Log\LoggerInterface;
2222
use PHPUnit\Framework\MockObject\MockObject as MockObject;
2323
use Magento\Framework\Filesystem\Driver\File;
24+
use Magento\Framework\View\Design\Theme\ThemePackageList;
25+
use Magento\Framework\Validator\Locale;
2426

2527
/**
2628
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -77,6 +79,16 @@ class StaticResourceTest extends \PHPUnit\Framework\TestCase
7779
*/
7880
private $deploymentConfigMock;
7981

82+
/**
83+
* @var ThemePackageList|MockObject
84+
*/
85+
private $themePackageListMock;
86+
87+
/**
88+
* @var Locale|MockObject
89+
*/
90+
private $localeValidatorMock;
91+
8092
/**
8193
* @var StaticResource
8294
*/
@@ -100,6 +112,8 @@ protected function setUp(): void
100112
$this->configLoaderMock = $this->createMock(ConfigLoader::class);
101113
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
102114
$this->driverMock = $this->createMock(File::class);
115+
$this->themePackageListMock = $this->createMock(ThemePackageList::class);
116+
$this->localeValidatorMock = $this->createMock(Locale::class);
103117
$this->object = new StaticResource(
104118
$this->stateMock,
105119
$this->responseMock,
@@ -110,7 +124,9 @@ protected function setUp(): void
110124
$this->objectManagerMock,
111125
$this->configLoaderMock,
112126
$this->deploymentConfigMock,
113-
$this->driverMock
127+
$this->driverMock,
128+
$this->themePackageListMock,
129+
$this->localeValidatorMock
114130
);
115131
}
116132

@@ -201,6 +217,17 @@ public function testLaunch(
201217
$this->driverMock->expects($this->once())
202218
->method('getRealPathSafety')
203219
->willReturnArgument(0);
220+
$this->themePackageListMock->expects($this->atLeastOnce())->method('getThemes')->willReturn(
221+
[
222+
'area/Magento/theme' => [
223+
'area' => 'area',
224+
'vendor' => 'Magento',
225+
'name' => 'theme',
226+
],
227+
]
228+
);
229+
$this->localeValidatorMock->expects($this->once())->method('isValid')->willReturn(true);
230+
204231
$this->object->launch();
205232
}
206233

@@ -322,4 +349,86 @@ public function testLaunchPathAbove()
322349

323350
$this->object->launch();
324351
}
352+
353+
/**
354+
* @param array $themes
355+
* @dataProvider themesDataProvider
356+
*/
357+
public function testLaunchWithInvalidTheme(array $themes): void
358+
{
359+
$this->expectException('InvalidArgumentException');
360+
$path = 'frontend/Test/luma/en_US/calendar.css';
361+
362+
$this->stateMock->expects($this->once())
363+
->method('getMode')
364+
->willReturn(State::MODE_DEVELOPER);
365+
$this->requestMock->expects($this->once())
366+
->method('get')
367+
->with('resource')
368+
->willReturn($path);
369+
$this->driverMock->expects($this->once())
370+
->method('getRealPathSafety')
371+
->with($path)
372+
->willReturn($path);
373+
$this->themePackageListMock->expects($this->once())->method('getThemes')->willReturn($themes);
374+
$this->localeValidatorMock->expects($this->never())->method('isValid');
375+
$this->expectExceptionMessage('Requested path ' . $path . ' is wrong.');
376+
377+
$this->object->launch();
378+
}
379+
380+
/**
381+
* @param array $themes
382+
* @dataProvider themesDataProvider
383+
*/
384+
public function testLaunchWithInvalidLocale(array $themes): void
385+
{
386+
$this->expectException('InvalidArgumentException');
387+
$path = 'frontend/Magento/luma/test/calendar.css';
388+
389+
$this->stateMock->expects($this->once())
390+
->method('getMode')
391+
->willReturn(State::MODE_DEVELOPER);
392+
$this->requestMock->expects($this->once())
393+
->method('get')
394+
->with('resource')
395+
->willReturn($path);
396+
$this->driverMock->expects($this->once())
397+
->method('getRealPathSafety')
398+
->with($path)
399+
->willReturn($path);
400+
$this->themePackageListMock->expects($this->once())->method('getThemes')->willReturn($themes);
401+
$this->localeValidatorMock->expects($this->once())->method('isValid')->willReturn(false);
402+
$this->expectExceptionMessage('Requested path ' . $path . ' is wrong.');
403+
404+
$this->object->launch();
405+
}
406+
407+
/**
408+
* @return array
409+
*/
410+
public function themesDataProvider(): array
411+
{
412+
return [
413+
[
414+
[
415+
'adminhtml/Magento/backend' => [
416+
'area' => 'adminhtml',
417+
'vendor' => 'Magento',
418+
'name' => 'backend',
419+
],
420+
'frontend/Magento/blank' => [
421+
'area' => 'frontend',
422+
'vendor' => 'Magento',
423+
'name' => 'blank',
424+
],
425+
'frontend/Magento/luma' => [
426+
'area' => 'frontend',
427+
'vendor' => 'Magento',
428+
'name' => 'luma',
429+
],
430+
],
431+
],
432+
];
433+
}
325434
}

0 commit comments

Comments
 (0)