]> BookStack Code Mirror - system-cli/commitdiff
Added path check/validation for provided restore file path
authorDan Brown <redacted>
Thu, 7 Nov 2024 16:59:44 +0000 (16:59 +0000)
committerDan Brown <redacted>
Thu, 7 Nov 2024 16:59:44 +0000 (16:59 +0000)
Closes #17

src/Commands/RestoreCommand.php
tests/Commands/RestoreCommandTest.php

index 2fa3c853c071931691c303b19ad53485f7f936d7..3fb91540dc8328696ba269741b6714579a5d3deb 100644 (file)
@@ -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();
 
index 868b9aefd826ce6350f2a3acb314117e9339967a..17b9dd50a27dbce34eaefecad875c96456c2ea92 100644 (file)
@@ -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');