Skip to content

Commit 24dbff0

Browse files
imhayatunnabifreekmurze
authored andcommitted
fix: resolve ZipArchive::open() return value not checked, BackupDestination::write() on exceptions, glob() returning false causes type errors
1 parent 1cc0499 commit 24dbff0

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Spatie\Backup\Events\DumpingDatabase;
1717
use Spatie\Backup\Exceptions\BackupFailed;
1818
use Spatie\Backup\Exceptions\InvalidBackupJob;
19-
use Spatie\DbDumper\Compressors\GzipCompressor;
2019
use Spatie\DbDumper\Databases\MongoDb;
2120
use Spatie\DbDumper\Databases\Sqlite;
2221
use Spatie\DbDumper\DbDumper;
@@ -278,12 +277,6 @@ protected function dumpDatabases(): array
278277

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

281-
// @todo is this still relevant or undocumented?
282-
if (config('backup.backup.gzip_database_dump')) {
283-
$dbDumper->useCompressor(new GzipCompressor);
284-
$fileName .= '.'.$dbDumper->getCompressorExtension();
285-
}
286-
287280
if ($compressor = $this->config->backup->databaseDumpCompressor) {
288281
$dbDumper->useCompressor(new $compressor);
289282
$fileName .= '.'.$dbDumper->getCompressorExtension();

src/Tasks/Backup/FileSelection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,14 @@ protected function sanitize(string|array $paths): Collection
145145
protected function getMatchingPaths(string $path): array
146146
{
147147
if ($this->canUseGlobBrace($path)) {
148-
return glob(str_replace('*', '{.[!.],}*', $path), GLOB_BRACE);
148+
$result = @glob(str_replace('*', '{.[!.],}*', $path), GLOB_BRACE);
149+
150+
if ($result !== false) {
151+
return $result;
152+
}
149153
}
150154

151-
return glob($path);
155+
return glob($path) ?: [];
152156
}
153157

154158
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)