Skip to content

Commit 0b0e3cd

Browse files
committed
MC-40902: Improve static content generation.
1 parent c1025c0 commit 0b0e3cd

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-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
/**
@@ -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,9 @@ private function getLogger()
236264

237265
return $this->logger;
238266
}
267+
268+
private function isThemeAllowed(string $theme): bool
269+
{
270+
return in_array($theme, array_keys($this->themePackageList->getThemes()));
271+
}
239272
}

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()
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()
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

@@ -320,4 +347,86 @@ public function testLaunchPathAbove()
320347

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

0 commit comments

Comments
 (0)