Skip to content

Commit b5e36dd

Browse files
Copilotdermatz
andcommitted
#feature-request - Improve security and performance in CleanCommand
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
1 parent ff38879 commit b5e36dd

1 file changed

Lines changed: 21 additions & 23 deletions

File tree

src/Console/Command/Static/CleanCommand.php

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ private function processCleanThemes(array $themeCodes): int
102102

103103
$this->io->title(sprintf('Cleaning static files for %d theme(s)', $totalThemes));
104104

105+
// Get themes list once for validation
106+
$installedThemes = $this->themeList->getAllThemes();
107+
$installedThemeCodes = array_map(fn($theme) => $theme->getCode(), $installedThemes);
108+
105109
foreach ($themeCodes as $themeCode) {
106110
$this->io->section(sprintf('Cleaning theme: %s', $themeCode));
107111

108-
if (!$this->validateTheme($themeCode)) {
112+
if (!in_array($themeCode, $installedThemeCodes, true)) {
109113
$failureList[] = $themeCode;
110114
$this->io->error("Theme $themeCode is not installed.");
111115
continue;
@@ -126,25 +130,6 @@ private function processCleanThemes(array $themeCodes): int
126130
return Cli::RETURN_SUCCESS;
127131
}
128132

129-
/**
130-
* Validate if theme exists
131-
*
132-
* @param string $themeCode
133-
* @return bool
134-
*/
135-
private function validateTheme(string $themeCode): bool
136-
{
137-
$themes = $this->themeList->getAllThemes();
138-
139-
foreach ($themes as $theme) {
140-
if ($theme->getCode() === $themeCode) {
141-
return true;
142-
}
143-
}
144-
145-
return false;
146-
}
147-
148133
/**
149134
* Clean theme files from var/view_preprocessed and pub/static
150135
*
@@ -278,21 +263,34 @@ private function removeDirectory(string $dir): bool
278263
return false;
279264
}
280265

281-
$files = array_diff(scandir($dir), ['.', '..']);
266+
// Check if directory is writable
267+
if (!is_writable($dir)) {
268+
throw new \RuntimeException(sprintf('Directory is not writable: %s', $dir));
269+
}
270+
271+
$files = scandir($dir);
272+
if ($files === false) {
273+
throw new \RuntimeException(sprintf('Failed to scan directory: %s', $dir));
274+
}
275+
276+
$files = array_diff($files, ['.', '..']);
282277
foreach ($files as $file) {
283278
$path = $dir . DIRECTORY_SEPARATOR . $file;
284279
if (is_dir($path)) {
285280
if (!$this->removeDirectory($path)) {
286281
throw new \RuntimeException(sprintf('Failed to remove directory: %s', $path));
287282
}
288283
} else {
289-
if (!@unlink($path)) {
284+
if (!is_writable($path)) {
285+
throw new \RuntimeException(sprintf('File is not writable: %s', $path));
286+
}
287+
if (!unlink($path)) {
290288
throw new \RuntimeException(sprintf('Failed to remove file: %s', $path));
291289
}
292290
}
293291
}
294292

295-
if (!@rmdir($dir)) {
293+
if (!rmdir($dir)) {
296294
throw new \RuntimeException(sprintf('Failed to remove directory: %s', $dir));
297295
}
298296

0 commit comments

Comments
 (0)