Skip to content

Commit 7cffddb

Browse files
committed
Update version to 1.0.3 and enhance BackupService and ChunkedBackupService with improved password handling through escaping mechanisms for secure configuration file generation.
1 parent c0cfc38 commit 7cffddb

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "A Laravel package for backing up databases and files to external sources with notifications",
44
"type": "library",
55
"license": "MIT",
6-
"version": "1.0.2",
6+
"version": "1.0.3",
77
"authors": [{
88
"name": "Nathan Langer",
99
"email": "nathanlanger@googlemail.com"

src/Services/BackupService.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,18 @@ protected function startMysqlDumpProcess(array $config, string $dumpPath)
484484
protected function startPostgresDumpProcess(array $config, string $dumpPath)
485485
{
486486
$pgpassFile = tempnam(sys_get_temp_dir(), 'pgpass_');
487+
$escape = function ($value) {
488+
$value = (string) $value;
489+
// Escape backslashes and colons for .pgpass format
490+
return str_replace(['\\', ':'], ['\\\\', '\\:'], $value);
491+
};
487492
$pgpassContent = sprintf(
488493
"%s:%s:%s:%s:%s\n",
489-
$config['host'],
490-
$config['port'] ?? 5432,
491-
$config['database'],
492-
$config['username'],
493-
$config['password']
494+
$escape($config['host']),
495+
$escape($config['port'] ?? 5432),
496+
$escape($config['database']),
497+
$escape($config['username']),
498+
$escape($config['password'])
494499
);
495500

496501
file_put_contents($pgpassFile, $pgpassContent);
@@ -695,13 +700,18 @@ protected function createPostgresDump(array $config, string $dumpPath): void
695700
{
696701
// Create temporary .pgpass file for secure password handling
697702
$pgpassFile = tempnam(sys_get_temp_dir(), 'pgpass_');
703+
$escape = function ($value) {
704+
$value = (string) $value;
705+
// Escape backslashes and colons for .pgpass format
706+
return str_replace(['\\', ':'], ['\\\\', '\\:'], $value);
707+
};
698708
$pgpassContent = sprintf(
699709
"%s:%s:%s:%s:%s\n",
700-
$config['host'],
701-
$config['port'] ?? 5432,
702-
$config['database'],
703-
$config['username'],
704-
$config['password']
710+
$escape($config['host']),
711+
$escape($config['port'] ?? 5432),
712+
$escape($config['database']),
713+
$escape($config['username']),
714+
$escape($config['password'])
705715
);
706716

707717
file_put_contents($pgpassFile, $pgpassContent);

src/Services/ChunkedBackupService.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,25 @@ protected function streamMysqlToChunks(array $config, string $connection, string
204204
{
205205
// Create a temporary config file for secure password handling
206206
$configFile = tempnam(sys_get_temp_dir(), 'mysql_config_');
207+
$escape = function ($value) {
208+
$value = (string) $value;
209+
$value = str_replace(["\\", "\n", "\r", '"'], ["\\\\", "\\n", "\\r", '\\"'], $value);
210+
return '"' . $value . '"';
211+
};
207212
$configContent = sprintf(
208-
"[mysqldump]\nuser=%s\npassword=%s\nhost=%s\nport=%s\n",
209-
$config['username'],
210-
$config['password'],
211-
$config['host'],
212-
$config['port'] ?? 3306
213+
"[mysqldump]\nuser=%s\npassword=%s\n",
214+
$escape($config['username'] ?? ''),
215+
$escape($config['password'] ?? '')
213216
);
217+
if (!empty($config['unix_socket'])) {
218+
$configContent .= 'socket=' . $escape($config['unix_socket']) . "\n";
219+
} else {
220+
$configContent .= sprintf(
221+
"host=%s\nport=%s\n",
222+
$escape($config['host'] ?? 'localhost'),
223+
$escape($config['port'] ?? 3306)
224+
);
225+
}
214226
file_put_contents($configFile, $configContent);
215227
chmod($configFile, 0600);
216228

0 commit comments

Comments
 (0)