Skip to content

Commit d07085c

Browse files
Merge pull request #108 from kapawit/fix/pgpass-special-characters
FIX (postgresql): Escape special characters in .pgpass file for authentication
2 parents 6cfc0ca + c89c1f9 commit d07085c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

backend/internal/features/backups/backups/usecases/postgresql/create_backup_uc.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,18 @@ func (uc *CreatePostgresqlBackupUsecase) createTempPgpassFile(
719719
return "", nil
720720
}
721721

722+
// Escape special characters in password as per PostgreSQL .pgpass format
723+
// Per official PostgreSQL documentation: only backslash and colon need escaping
724+
escapedPassword := strings.NewReplacer(
725+
"\\", "\\\\",
726+
":", "\\:",
727+
).Replace(password)
728+
722729
pgpassContent := fmt.Sprintf("%s:%d:*:%s:%s",
723730
pgConfig.Host,
724731
pgConfig.Port,
725732
pgConfig.Username,
726-
password,
733+
escapedPassword,
727734
)
728735

729736
tempDir, err := os.MkdirTemp("", "pgpass")

backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,18 @@ func (uc *RestorePostgresqlBackupUsecase) createTempPgpassFile(
564564
return "", nil
565565
}
566566

567+
// Escape special characters in password as per PostgreSQL .pgpass format
568+
// Per official PostgreSQL documentation: only backslash and colon need escaping
569+
escapedPassword := strings.NewReplacer(
570+
"\\", "\\\\",
571+
":", "\\:",
572+
).Replace(password)
573+
567574
pgpassContent := fmt.Sprintf("%s:%d:*:%s:%s",
568575
pgConfig.Host,
569576
pgConfig.Port,
570577
pgConfig.Username,
571-
password,
578+
escapedPassword,
572579
)
573580

574581
tempDir, err := os.MkdirTemp("", "pgpass")

0 commit comments

Comments
 (0)