Skip to content

Commit 0dfbb6a

Browse files
committed
fix: improve error handling by escaping output in exceptions
1 parent 10b28d0 commit 0dfbb6a

6 files changed

Lines changed: 15 additions & 13 deletions

File tree

src/Console/Command/Hyva/TokensCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
7676
return Command::SUCCESS;
7777
}
7878

79-
$options = array_map(fn($theme) => $theme->getCode(), $hyvaThemes);
80-
79+
$options = [];
80+
foreach ($hyvaThemes as $theme) {
81+
$options[] = $theme->getCode();
82+
}
83+
8184
$themeCodePrompt = new SelectPrompt(
8285
label: 'Select Hyvä theme to generate tokens for',
8386
options: $options,

src/Service/DependencyChecker.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Magento\Framework\Filesystem\Driver\File;
88
use Symfony\Component\Console\Style\SymfonyStyle;
99
use Magento\Framework\Shell;
10-
1110
class DependencyChecker
1211
{
1312
private const PACKAGE_JSON = 'package.json';

src/Service/HyvaTokens/ConfigReader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
public function getConfig(string $themePath): array
3131
{
3232
$configPath = $this->getConfigPath($themePath);
33-
33+
3434
// Default configuration
3535
$config = [
3636
'src' => self::DEFAULT_SOURCE,
@@ -41,16 +41,16 @@ public function getConfig(string $themePath): array
4141

4242
if ($this->fileDriver->isExists($configPath)) {
4343
$configContent = $this->fileDriver->fileGetContents($configPath);
44-
44+
4545
try {
4646
$jsonConfig = json_decode($configContent, true, 512, JSON_THROW_ON_ERROR);
4747
} catch (\JsonException $e) {
48-
throw new \Exception("Invalid JSON in configuration file: " . $e->getMessage());
48+
throw new \Exception("Invalid JSON in configuration file: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
4949
}
5050

5151
if (isset($jsonConfig['tokens'])) {
5252
$tokensConfig = $jsonConfig['tokens'];
53-
53+
5454
// Override with config file values
5555
if (isset($tokensConfig['src'])) {
5656
$config['src'] = $tokensConfig['src'];

src/Service/HyvaTokens/CssGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function write(string $content, string $outputPath): bool
5151
$pathParts = explode('/', $outputPath);
5252
array_pop($pathParts); // Remove filename
5353
$directory = implode('/', $pathParts);
54-
54+
5555
if (!$this->fileDriver->isDirectory($directory)) {
5656
$this->fileDriver->createDirectory($directory, 0750);
5757
}
@@ -60,7 +60,7 @@ public function write(string $content, string $outputPath): bool
6060
$this->fileDriver->filePutContents($outputPath, $content);
6161
return true;
6262
} catch (\Exception $e) {
63-
throw new \Exception("Failed to write CSS file: " . $e->getMessage());
63+
throw new \Exception("Failed to write CSS file: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
6464
}
6565
}
6666
}

src/Service/HyvaTokens/TokenParser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public function parse(?string $filePath, ?array $inlineValues, string $format):
3434

3535
// Otherwise, read from file
3636
if ($filePath === null || !$this->fileDriver->isFile($filePath)) {
37-
throw new \Exception("Token source file not found: " . ($filePath ?? 'null'));
37+
throw new \Exception("Token source file not found: " . htmlspecialchars($filePath ?? 'null', ENT_QUOTES, 'UTF-8'));
3838
}
3939

4040
$content = $this->fileDriver->fileGetContents($filePath);
41-
41+
4242
try {
4343
$tokens = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
4444
} catch (\JsonException $e) {
45-
throw new \Exception("Invalid JSON in token file: " . $e->getMessage());
45+
throw new \Exception("Invalid JSON in token file: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8'));
4646
}
4747

4848
return $this->normalizeTokens($tokens, $format);

src/Service/ThemeBuilder/BuilderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function addBuilder(BuilderInterface $builder): void
1616
public function create(string $type): BuilderInterface
1717
{
1818
if (!isset($this->builders[$type])) {
19-
throw new \InvalidArgumentException("Builder $type not found");
19+
throw new \InvalidArgumentException("Builder " . htmlspecialchars($type, ENT_QUOTES, 'UTF-8') . " not found");
2020
}
2121

2222
return $this->builders[$type];

0 commit comments

Comments
 (0)