Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Exceptions/CredentialExposure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Backup\Exceptions;

use Exception;

class CredentialExposure extends Exception
{
public static function from(Exception $exception): static
{
return new static($exception->getMessage(), $exception->getCode(), $exception);
}
}
10 changes: 10 additions & 0 deletions src/Tasks/Backup/BackupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Spatie\Backup\Events\DumpingDatabase;
use Spatie\Backup\Exceptions\BackupFailed;
use Spatie\Backup\Exceptions\InvalidBackupJob;
use Spatie\DbDumper\Compressors\GzipCompressor;
use Spatie\DbDumper\Databases\MongoDb;
use Spatie\DbDumper\Databases\Sqlite;
use Spatie\DbDumper\DbDumper;
Expand Down Expand Up @@ -155,6 +156,14 @@ public function run(): void
->force()
->create()
->empty();
$cleanupRegistered = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the added lines

$shutdownHandler = function () use (&$cleanupRegistered) {
if ($cleanupRegistered && $this->temporaryDirectory->exists()) {
$this->temporaryDirectory->delete();
}
};
register_shutdown_function($shutdownHandler);
$cleanupRegistered = true;

if ($this->signals) {
Signal::handle(SIGINT, function (Command $command) {
Expand Down Expand Up @@ -187,6 +196,7 @@ public function run(): void
}

$this->temporaryDirectory->delete();
$cleanupRegistered = false; // Prevent double cleanup

if ($this->signals) {
Signal::clearHandlers(SIGINT);
Expand Down
8 changes: 6 additions & 2 deletions src/Tasks/Backup/DbDumperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ public static function createFromConnection(string $dbConnectionName): DbDumper
}

if (isset($dbConfig['port'])) {
if (filter_var($dbConfig['port'], FILTER_VALIDATE_INT, [
$port = $dbConfig['port'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this. It'll fail elsewhere if the port is not correct.

if ($port === '' || $port === null) {
} elseif (filter_var($port, FILTER_VALIDATE_INT, [
'options' => [
'min_range' => 1,
'max_range' => 65535,
],
]) !== false) {
$dbDumper = $dbDumper->setPort((int) $dbConfig['port']);
$dbDumper = $dbDumper->setPort((int) $port);
} else {
consoleOutput()->warn("Invalid port value '{$port}' for database connection '{$dbConnectionName}'. Using default port.");
}
}

Expand Down
24 changes: 18 additions & 6 deletions src/Tasks/Backup/FileSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,20 @@ protected function includedDirectories(): array

protected function shouldExclude(string $path): bool
{
$path = realpath($path);
if (is_dir($path)) {
$path .= DIRECTORY_SEPARATOR;
$realPath = realpath($path);

if ($realPath === false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with excluded paths being non-existent. Remove all changes to this function.

consoleOutput()->warn("Cannot resolve path: {$path}. Skipping...");
return false;
}

if (is_dir($realPath)) {
$realPath .= DIRECTORY_SEPARATOR;
}

foreach ($this->excludeFilesAndDirectories as $excludedPath) {
if (Str::startsWith($path, $excludedPath.(is_dir($excludedPath) ? DIRECTORY_SEPARATOR : ''))) {
if ($path != $excludedPath && is_file($excludedPath)) {
if (Str::startsWith($realPath, $excludedPath.(is_dir($excludedPath) ? DIRECTORY_SEPARATOR : ''))) {
if ($realPath != $excludedPath && is_file($excludedPath)) {
continue;
}

Expand All @@ -137,7 +143,13 @@ protected function sanitize(string|array $paths): Collection
return collect($paths)
->reject(fn (string $path) => $path === '')
->flatMap(fn (string $path) => $this->getMatchingPaths($path))
->map(fn (string $path) => realpath($path))
->map(function (string $path) {
$realPath = realpath($path);
if ($realPath === false) {
consoleOutput()->warn("Cannot resolve path: {$path}. This path will be excluded from backup.");
}
return $realPath;
})
->reject(fn ($path) => $path === false);
}

Expand Down