Skip to content

Commit da9a7a4

Browse files
committed
Merge remote-tracking branch 'origin/MC-40901' into 2.4.3-develop-pr124
2 parents 2b7fe41 + 67047d3 commit da9a7a4

File tree

2 files changed

+143
-2
lines changed

2 files changed

+143
-2
lines changed

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

Lines changed: 34 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
/**
@@ -149,6 +167,16 @@ public function launch()
149167
throw $e;
150168
}
151169

170+
if (!($this->isThemeAllowed($params['area'] . DIRECTORY_SEPARATOR . $params['theme'])
171+
&& $this->localeValidator->isValid($params['locale']))
172+
) {
173+
if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
174+
$this->response->setHttpResponseCode(404);
175+
return $this->response;
176+
}
177+
throw new \InvalidArgumentException('Requested path ' . $path . ' is wrong.');
178+
}
179+
152180
$this->state->setAreaCode($params['area']);
153181
$this->objectManager->configure($this->configLoader->load($params['area']));
154182
$file = $params['file'];
@@ -247,4 +275,9 @@ private function getLogger()
247275

248276
return $this->logger;
249277
}
278+
279+
private function isThemeAllowed(string $theme): bool
280+
{
281+
return in_array($theme, array_keys($this->themePackageList->getThemes()));
282+
}
250283
}

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

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
use Magento\Framework\Filesystem\Driver\File;
2020
use Magento\Framework\Module\ModuleList;
2121
use Magento\Framework\ObjectManagerInterface;
22+
use Magento\Framework\Validator\Locale;
2223
use Magento\Framework\View\Asset\LocalInterface;
2324
use Magento\Framework\View\Asset\Repository;
25+
use Magento\Framework\View\Design\Theme\ThemePackageList;
2426
use PHPUnit\Framework\MockObject\MockObject;
2527
use PHPUnit\Framework\TestCase;
2628
use Psr\Log\LoggerInterface;
@@ -85,6 +87,16 @@ class StaticResourceTest extends TestCase
8587
*/
8688
private $driverMock;
8789

90+
/**
91+
* @var ThemePackageList|MockObject
92+
*/
93+
private $themePackageListMock;
94+
95+
/**
96+
* @var Locale|MockObject
97+
*/
98+
private $localeValidatorMock;
99+
88100
/**
89101
* @var StaticResource
90102
*/
@@ -106,6 +118,8 @@ protected function setUp(): void
106118
$this->configLoaderMock = $this->createMock(ConfigLoader::class);
107119
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
108120
$this->driverMock = $this->createMock(File::class);
121+
$this->themePackageListMock = $this->createMock(ThemePackageList::class);
122+
$this->localeValidatorMock = $this->createMock(Locale::class);
109123
$this->object = new StaticResource(
110124
$this->stateMock,
111125
$this->responseMock,
@@ -116,7 +130,9 @@ protected function setUp(): void
116130
$this->objectManagerMock,
117131
$this->configLoaderMock,
118132
$this->deploymentConfigMock,
119-
$this->driverMock
133+
$this->driverMock,
134+
$this->themePackageListMock,
135+
$this->localeValidatorMock
120136
);
121137
}
122138

@@ -210,6 +226,16 @@ public function testLaunch(
210226
$this->driverMock->expects($this->once())
211227
->method('getRealPathSafety')
212228
->willReturnArgument(0);
229+
$this->themePackageListMock->expects($this->atLeastOnce())->method('getThemes')->willReturn(
230+
[
231+
'area/Magento/theme' => [
232+
'area' => 'area',
233+
'vendor' => 'Magento',
234+
'name' => 'theme',
235+
],
236+
],
237+
);
238+
$this->localeValidatorMock->expects($this->once())->method('isValid')->willReturn(true);
213239
$this->object->launch();
214240
}
215241

@@ -353,4 +379,86 @@ public function testLaunchPathAbove()
353379

354380
$this->object->launch();
355381
}
382+
383+
/**
384+
* @param array $themes
385+
* @dataProvider themesDataProvider
386+
*/
387+
public function testLaunchWithInvalidTheme(array $themes): void
388+
{
389+
$this->expectException('InvalidArgumentException');
390+
$path = 'frontend/Test/luma/en_US/calendar.css';
391+
392+
$this->stateMock->expects($this->once())
393+
->method('getMode')
394+
->willReturn(State::MODE_DEVELOPER);
395+
$this->requestMock->expects($this->once())
396+
->method('get')
397+
->with('resource')
398+
->willReturn($path);
399+
$this->driverMock->expects($this->once())
400+
->method('getRealPathSafety')
401+
->with($path)
402+
->willReturn($path);
403+
$this->themePackageListMock->expects($this->once())->method('getThemes')->willReturn($themes);
404+
$this->localeValidatorMock->expects($this->never())->method('isValid');
405+
$this->expectExceptionMessage('Requested path ' . $path . ' is wrong.');
406+
407+
$this->object->launch();
408+
}
409+
410+
/**
411+
* @param array $themes
412+
* @dataProvider themesDataProvider
413+
*/
414+
public function testLaunchWithInvalidLocale(array $themes): void
415+
{
416+
$this->expectException('InvalidArgumentException');
417+
$path = 'frontend/Magento/luma/test/calendar.css';
418+
419+
$this->stateMock->expects($this->once())
420+
->method('getMode')
421+
->willReturn(State::MODE_DEVELOPER);
422+
$this->requestMock->expects($this->once())
423+
->method('get')
424+
->with('resource')
425+
->willReturn($path);
426+
$this->driverMock->expects($this->once())
427+
->method('getRealPathSafety')
428+
->with($path)
429+
->willReturn($path);
430+
$this->themePackageListMock->expects($this->once())->method('getThemes')->willReturn($themes);
431+
$this->localeValidatorMock->expects($this->once())->method('isValid')->willReturn(false);
432+
$this->expectExceptionMessage('Requested path ' . $path . ' is wrong.');
433+
434+
$this->object->launch();
435+
}
436+
437+
/**
438+
* @return array
439+
*/
440+
public function themesDataProvider(): array
441+
{
442+
return [
443+
[
444+
[
445+
'adminhtml/Magento/backend' => [
446+
'area' => 'adminhtml',
447+
'vendor' => 'Magento',
448+
'name' => 'backend',
449+
],
450+
'frontend/Magento/blank' => [
451+
'area' => 'frontend',
452+
'vendor' => 'Magento',
453+
'name' => 'blank',
454+
],
455+
'frontend/Magento/luma' => [
456+
'area' => 'frontend',
457+
'vendor' => 'Magento',
458+
'name' => 'luma',
459+
],
460+
],
461+
],
462+
];
463+
}
356464
}

0 commit comments

Comments
 (0)