Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (backups): Escape password over connection check to allow whitespaces

Rostislav Dugin52c0f536084e
diff --git a/backend/internal/features/databases/databases/postgresql/model.go b/backend/internal/features/databases/databases/postgresql/model.go
index 9eb92368..dc51a5f4 100644
--- a/backend/internal/features/databases/databases/postgresql/model.go
+++ b/backend/internal/features/databases/databases/postgresql/model.go
@@ -695,16 +695,22 @@ func buildConnectionStringForDB(p *PostgresqlDatabase, dbName string, password s
}
return fmt.Sprintf(
- "host=%s port=%d user=%s password=%s dbname=%s sslmode=%s default_query_exec_mode=simple_protocol standard_conforming_strings=on client_encoding=UTF8",
+ "host=%s port=%d user=%s password='%s' dbname=%s sslmode=%s default_query_exec_mode=simple_protocol standard_conforming_strings=on client_encoding=UTF8",
p.Host,
p.Port,
p.Username,
- password,
+ escapeConnectionStringValue(password),
dbName,
sslMode,
)
}
+func escapeConnectionStringValue(value string) string {
+ value = strings.ReplaceAll(value, `\`, `\\`)
+ value = strings.ReplaceAll(value, `'`, `\'`)
+ return value
+}
+
func decryptPasswordIfNeeded(
password string,
encryptor encryption.FieldEncryptor,
diff --git a/backend/internal/features/databases/databases/postgresql/model_test.go b/backend/internal/features/databases/databases/postgresql/model_test.go
new file mode 100644
index 00000000..71b25ef7
--- /dev/null
+++ b/backend/internal/features/databases/databases/postgresql/model_test.go
@@ -0,0 +1,94 @@
+package postgresql
+
+import (
+ "fmt"
+ "log/slog"
+ "os"
+ "strconv"
+ "testing"
+
+ "github.com/google/uuid"
+ "github.com/jmoiron/sqlx"
+ _ "github.com/lib/pq"
+ "github.com/stretchr/testify/assert"
+
+ "databasus-backend/internal/config"
+ "databasus-backend/internal/util/tools"
+)
+
+func Test_TestConnection_PasswordContainingSpaces_TestedSuccessfully(t *testing.T) {
+ env := config.GetEnv()
+ container := connectToTestPostgresContainer(t, env.TestPostgres16Port)
+ defer container.DB.Close()
+
+ passwordWithSpaces := "test password with spaces"
+ usernameWithSpaces := fmt.Sprintf("testuser_spaces_%s", uuid.New().String()[:8])
+
+ _, err := container.DB.Exec(fmt.Sprintf(
+ `CREATE USER "%s" WITH PASSWORD '%s' LOGIN`,
+ usernameWithSpaces,
+ passwordWithSpaces,
+ ))
+ assert.NoError(t, err)
+
+ _, err = container.DB.Exec(fmt.Sprintf(
+ `GRANT CONNECT ON DATABASE "%s" TO "%s"`,
+ container.Database,
+ usernameWithSpaces,
+ ))
+ assert.NoError(t, err)
+
+ defer func() {
+ _, _ = container.DB.Exec(fmt.Sprintf(`DROP USER IF EXISTS "%s"`, usernameWithSpaces))
+ }()
+
+ pgModel := &PostgresqlDatabase{
+ Version: tools.GetPostgresqlVersionEnum("16"),
+ Host: container.Host,
+ Port: container.Port,
+ Username: usernameWithSpaces,
+ Password: passwordWithSpaces,
+ Database: &container.Database,
+ IsHttps: false,
+ CpuCount: 1,
+ }
+
+ logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
+
+ err = pgModel.TestConnection(logger, nil, uuid.New())
+ assert.NoError(t, err)
+}
+
+type testPostgresContainer struct {
+ Host string
+ Port int
+ Username string
+ Password string
+ Database string
+ DB *sqlx.DB
+}
+
+func connectToTestPostgresContainer(t *testing.T, port string) *testPostgresContainer {
+ dbName := "testdb"
+ password := "testpassword"
+ username := "testuser"
+ host := "localhost"
+
+ portInt, err := strconv.Atoi(port)
+ assert.NoError(t, err)
+
+ dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
+ host, portInt, username, password, dbName)
+
+ db, err := sqlx.Connect("postgres", dsn)
+ assert.NoError(t, err)
+
+ return &testPostgresContainer{
+ Host: host,
+ Port: portInt,
+ Username: username,
+ Password: password,
+ Database: dbName,
+ DB: db,
+ }
+}
diff --git a/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go b/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
index 94f1e8a9..817c25b1 100644
--- a/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
+++ b/backend/internal/features/restores/usecases/postgresql/restore_backup_uc.go
@@ -131,7 +131,6 @@ func (uc *RestorePostgresqlBackupUsecase) restoreViaStdin(
"--if-exists",
"--no-owner",
"--no-acl",
- "--no-comments",
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Minute)
diff --git a/frontend/src/features/restores/ui/RestoresComponent.tsx b/frontend/src/features/restores/ui/RestoresComponent.tsx
index ced37ce6..11c361e5 100644
--- a/frontend/src/features/restores/ui/RestoresComponent.tsx
+++ b/frontend/src/features/restores/ui/RestoresComponent.tsx
@@ -163,7 +163,7 @@ export const RestoresComponent = ({ database, backup }: Props) => {
loading={isRestoreInProgress}
onClick={() => setIsShowRestore(true)}
>
- Restore from backup
+ Select database to restore to
</Button>
{restores.length === 0 && (