Skip to content

Commit ded1e9c

Browse files
Copilotdermatz
andcommitted
Address code review feedback: improve error handling and security
Co-authored-by: dermatz <6103201+dermatz@users.noreply.github.com>
1 parent ad1c0ec commit ded1e9c

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/Console/Command/Hyva/TokensCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
8686

8787
try {
8888
$themeCode = $themeCodePrompt->prompt();
89-
\Laravel\Prompts\Prompt::terminal()->restoreTty();
9089
} catch (\Exception $e) {
9190
$this->io->error('Interactive mode failed: ' . $e->getMessage());
9291
return Command::FAILURE;
92+
} finally {
93+
\Laravel\Prompts\Prompt::terminal()->restoreTty();
9394
}
9495
}
9596

src/Service/HyvaTokens/ConfigReader.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __construct(
2525
*
2626
* @param string $themePath
2727
* @return array
28+
* @throws \Exception
2829
*/
2930
public function getConfig(string $themePath): array
3031
{
@@ -40,7 +41,12 @@ public function getConfig(string $themePath): array
4041

4142
if ($this->fileDriver->isExists($configPath)) {
4243
$configContent = $this->fileDriver->fileGetContents($configPath);
43-
$jsonConfig = json_decode($configContent, true);
44+
45+
try {
46+
$jsonConfig = json_decode($configContent, true, 512, JSON_THROW_ON_ERROR);
47+
} catch (\JsonException $e) {
48+
throw new \Exception("Invalid JSON in configuration file: " . $e->getMessage());
49+
}
4450

4551
if (isset($jsonConfig['tokens'])) {
4652
$tokensConfig = $jsonConfig['tokens'];

src/Service/HyvaTokens/CssGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function write(string $content, string $outputPath): bool
5050
// Ensure the directory exists
5151
$directory = dirname($outputPath);
5252
if (!$this->fileDriver->isDirectory($directory)) {
53-
$this->fileDriver->createDirectory($directory, 0755);
53+
$this->fileDriver->createDirectory($directory, 0750);
5454
}
5555

5656
try {

src/Service/HyvaTokens/TokenParser.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ public function parse(?string $filePath, ?array $inlineValues, string $format):
3333
}
3434

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

4040
$content = $this->fileDriver->fileGetContents($filePath);
41-
$tokens = json_decode($content, true);
42-
43-
if (json_last_error() !== JSON_ERROR_NONE) {
44-
throw new \Exception("Invalid JSON in token file: " . json_last_error_msg());
41+
42+
try {
43+
$tokens = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
44+
} catch (\JsonException $e) {
45+
throw new \Exception("Invalid JSON in token file: " . $e->getMessage());
4546
}
4647

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

0 commit comments

Comments
 (0)