Skip to content
Open
Changes from 1 commit
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
61 changes: 61 additions & 0 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,70 @@ func LoadDbConfig() {
}
}

// Check if the database exists
func checkDatabaseExists() (bool, error) {
psqlCmd := exec.Command(
"psql",
"-U", dbConfig.PsqlConf.User,
"-h", dbConfig.PsqlConf.Host,
"-p", dbConfig.PsqlConf.Port,
"-d", "postgres",
"-tAc",
fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s';", dbConfig.PsqlConf.Dbname),

Copilot AI Dec 8, 2025

Copy link

Choose a reason for hiding this comment

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

SQL injection vulnerability: the database name is directly interpolated into the SQL query without proper escaping. A malicious database name in the config could be used to execute arbitrary SQL commands. Use parameterized queries or properly escape the database name using PostgreSQL's quote_ident function.

Suggested change
fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s';", dbConfig.PsqlConf.Dbname),
fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = quote_ident('%s');", dbConfig.PsqlConf.Dbname),

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Leaving this for now as the databsae name is provided by admin only

)
psqlCmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", dbConfig.PsqlConf.Password))

output, err := psqlCmd.CombinedOutput()
if err != nil {
return false, fmt.Errorf("failed to check if database exists: %v, output: %s", err, string(output))
}
// output will be "1" if db exists
exists := false
if len(output) > 0 && string(output[:1]) == "1" {
exists = true
}
return exists, nil
}

// Create the database if it does not exist
func createDatabase() error {
createCmd := exec.Command(
"psql",
"-U", dbConfig.PsqlConf.User,
"-h", dbConfig.PsqlConf.Host,
"-p", dbConfig.PsqlConf.Port,
"-d", "postgres",
"-c", fmt.Sprintf("CREATE DATABASE %s;", dbConfig.PsqlConf.Dbname),
Comment thread
sukhman-sukh marked this conversation as resolved.
Outdated
)
createCmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", dbConfig.PsqlConf.Password))

output, err := createCmd.CombinedOutput()
if err != nil {
log.Errorf("Create DB error: %s\n", string(output))
return err
}
return nil
}

// Connect psql database
func ConnectDatabase() error {
LoadDbConfig()

// Check if the DB exists, if not, create it.
exists, err := checkDatabaseExists()
if err != nil {
log.Errorf("Failed checking if db exists: %v", err)
Comment thread
sukhman-sukh marked this conversation as resolved.
Outdated
return err
}
if !exists {
log.Infof("Database '%s' does not exist. Creating...", dbConfig.PsqlConf.Dbname)
if err := createDatabase(); err != nil {
log.Fatalf("Failed to create database: %v", err)
Comment thread
sukhman-sukh marked this conversation as resolved.
Outdated
return err
Comment thread
sukhman-sukh marked this conversation as resolved.
Outdated
}
log.Infof("Database '%s' created successfully.", dbConfig.PsqlConf.Dbname)
}

dsn := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%s sslmode=%s", dbConfig.PsqlConf.User, dbConfig.PsqlConf.Password, dbConfig.PsqlConf.Dbname, dbConfig.PsqlConf.Host, dbConfig.PsqlConf.Port, dbConfig.PsqlConf.SslMode)
Db, dberr = gorm.Open(postgres.Open(dsn), &gorm.Config{})
if dberr != nil {
Expand Down