Skip to content

Commit 5e29175

Browse files
committed
fix: resolve ZipArchive::open() return value not checked, BackupDestination::write() on exceptions, glob() returning false causes type errors
1 parent 70b71b4 commit 5e29175

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

src/BackupDestination/BackupDestination.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ public function write(string $file): void
6868

6969
$handle = fopen($file, 'r+');
7070

71-
$this->disk->getDriver()->writeStream(
72-
$destination,
73-
$handle,
74-
$this->getDiskOptions(),
75-
);
76-
77-
if (is_resource($handle)) {
78-
fclose($handle);
71+
try {
72+
$this->disk->getDriver()->writeStream(
73+
$destination,
74+
$handle,
75+
$this->getDiskOptions(),
76+
);
77+
} finally {
78+
if (is_resource($handle)) {
79+
fclose($handle);
80+
}
7981
}
8082
}
8183

src/Listeners/EncryptBackupArchive.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\Backup\Listeners;
44

55
use Spatie\Backup\Events\BackupZipWasCreated;
6+
use Spatie\Backup\Exceptions\BackupFailed;
67
use ZipArchive;
78

89
class EncryptBackupArchive
@@ -15,7 +16,11 @@ public function handle(BackupZipWasCreated $event): void
1516

1617
$zip = new ZipArchive;
1718

18-
$zip->open($event->pathToZip);
19+
$result = $zip->open($event->pathToZip);
20+
21+
if ($result !== true) {
22+
throw BackupFailed::from(new \Exception("Failed to open zip file for encryption at '{$event->pathToZip}'. ZipArchive error code: {$result}"));
23+
}
1924

2025
$this->encrypt($zip);
2126

src/Tasks/Backup/BackupJob.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,6 @@ protected function dumpDatabases(): array
289289

290290
$fileName = "{$dbType}-{$dbName}{$timeStamp}.{$this->getExtension($dbDumper)}";
291291

292-
// @todo is this still relevant or undocumented?
293-
if (config('backup.backup.gzip_database_dump')) {
294-
$dbDumper->useCompressor(new GzipCompressor);
295-
$fileName .= '.'.$dbDumper->getCompressorExtension();
296-
}
297-
298292
if ($compressor = $this->config->backup->databaseDumpCompressor) {
299293
$dbDumper->useCompressor(new $compressor);
300294
$fileName .= '.'.$dbDumper->getCompressorExtension();

src/Tasks/Backup/FileSelection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ protected function sanitize(string|array $paths): Collection
157157
protected function getMatchingPaths(string $path): array
158158
{
159159
if ($this->canUseGlobBrace($path)) {
160-
return glob(str_replace('*', '{.[!.],}*', $path), GLOB_BRACE);
160+
$result = @glob(str_replace('*', '{.[!.],}*', $path), GLOB_BRACE);
161+
162+
if ($result !== false) {
163+
return $result;
164+
}
161165
}
162166

163-
return glob($path);
167+
return glob($path) ?: [];
164168
}
165169

166170
protected function canUseGlobBrace(string $path): bool

src/Tasks/Backup/Zip.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Spatie\Backup\Config\Config;
7+
use Spatie\Backup\Exceptions\BackupFailed;
78
use Spatie\Backup\Helpers\Format;
89
use ZipArchive;
910

@@ -82,7 +83,11 @@ public function humanReadableSize(): string
8283

8384
public function open(): void
8485
{
85-
$this->zipFile->open($this->pathToZip, ZipArchive::CREATE);
86+
$result = $this->zipFile->open($this->pathToZip, ZipArchive::CREATE);
87+
88+
if ($result !== true) {
89+
throw BackupFailed::from(new \Exception("Failed to open zip file at '{$this->pathToZip}'. ZipArchive error code: {$result}"));
90+
}
8691
}
8792

8893
public function close(): void

0 commit comments

Comments
 (0)