From: Dan Brown Date: Thu, 7 Nov 2024 16:59:44 +0000 (+0000) Subject: Added path check/validation for provided restore file path X-Git-Url: http://source.bookstackapp.com/system-cli/commitdiff_plain/a96a9aa62fb0c274ea697284177a7484a3f968d8 Added path check/validation for provided restore file path Closes #17 --- diff --git a/src/Commands/RestoreCommand.php b/src/Commands/RestoreCommand.php index 2fa3c85..3fb9154 100644 --- a/src/Commands/RestoreCommand.php +++ b/src/Commands/RestoreCommand.php @@ -48,7 +48,13 @@ class RestoreCommand extends Command RequirementsValidator::validate(); (new ProgramRunner('mysql', '/usr/bin/mysql'))->ensureFound(); - $zipPath = realpath($input->getArgument('backup-zip')); + $providedZipPath = $input->getArgument('backup-zip'); + $zipPath = realpath($providedZipPath); + if (!$zipPath || !file_exists($zipPath)) { + $pathToDisplay = $zipPath ?: $providedZipPath; + throw new CommandError("Could not find ZIP file for restoration at provided path [{$pathToDisplay}]."); + } + $zip = new BackupZip($zipPath); $contents = $zip->getContentsOverview(); diff --git a/tests/Commands/RestoreCommandTest.php b/tests/Commands/RestoreCommandTest.php index 868b9ae..17b9dd5 100644 --- a/tests/Commands/RestoreCommandTest.php +++ b/tests/Commands/RestoreCommandTest.php @@ -167,6 +167,18 @@ class RestoreCommandTest extends TestCase exec('rm -rf /symlinks'); } + public function test_restore_with_invalid_zip_path_shows_warning() + { + chdir('/var/www/bookstack'); + + $result = $this->runCommand('restore', [ + 'backup-zip' => '/cats/dogs.zip', + '--app-directory' => '/var/www/bookstack', + ]); + + $result->assertStderrContains("Could not find ZIP file for restoration at provided path [/cats/dogs.zip]."); + } + protected function buildZip(callable $builder): string { $zipFile = tempnam(sys_get_temp_dir(), 'cli-test');