Skip to content

Commit ef6e8bb

Browse files
fix(postgres): prevent hostname injection attacks with validation
Addresses PostgreSQL hostname injection vulnerability where malicious hostnames containing URL-reserved characters like '@' could inject connection parameters. Added validateHostname() function to reject hostnames with URL-reserved characters before URL construction. Attack vector blocked: evil.com:5432@attacker.com Co-authored-by: Anguel <modelorona@users.noreply.github.com>
1 parent 0db339f commit ef6e8bb

File tree

1 file changed

+20
-0
lines changed
  • core/src/plugins/postgres

1 file changed

+20
-0
lines changed

core/src/plugins/postgres/db.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,41 @@
1717
package postgres
1818

1919
import (
20+
"fmt"
2021
"net"
2122
"net/url"
2223
"strconv"
24+
"strings"
2325

2426
"github.com/clidey/whodb/core/src/engine"
2527
"gorm.io/driver/postgres"
2628
"gorm.io/gorm"
2729
)
2830

31+
// validateHostname ensures the hostname doesn't contain URL-reserved characters
32+
// that could lead to injection attacks
33+
func validateHostname(hostname string) error {
34+
// Check for URL-reserved characters that could enable injection
35+
invalidChars := []string{"@", "?", "#", "/", "\\"}
36+
for _, char := range invalidChars {
37+
if strings.Contains(hostname, char) {
38+
return fmt.Errorf("invalid hostname: contains URL-reserved character '%s'", char)
39+
}
40+
}
41+
return nil
42+
}
43+
2944
func (p *PostgresPlugin) DB(config *engine.PluginConfig) (*gorm.DB, error) {
3045
connectionInput, err := p.ParseConnectionConfig(config)
3146
if err != nil {
3247
return nil, err
3348
}
3449

50+
// Validate hostname to prevent injection attacks
51+
if err := validateHostname(connectionInput.Hostname); err != nil {
52+
return nil, err
53+
}
54+
3555
// Construct PostgreSQL URL securely using url.URL struct
3656
u := &url.URL{
3757
Scheme: "postgresql",

0 commit comments

Comments
 (0)