Skip to content

Commit 6a44779

Browse files
fix(postgres): prevent path traversal attacks in database names
Add validateDatabase() function to prevent URL-encoded forward slashes and path traversal patterns in database names. This blocks the reported attack vector %2f..%2f..%2f and other similar injection attempts. Security improvements: - Block URL-encoded forward slashes (%2f, %2F) - Block literal path traversal patterns (../, ..\) - Block other problematic URL-encoded characters - Maintain postgresql:// URL format as required Co-authored-by: Anguel <modelorona@users.noreply.github.com>
1 parent ef6e8bb commit 6a44779

File tree

1 file changed

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

1 file changed

+29
-0
lines changed

core/src/plugins/postgres/db.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ func validateHostname(hostname string) error {
4141
return nil
4242
}
4343

44+
// validateDatabase ensures the database name doesn't contain URL-encoded characters
45+
// or patterns that could lead to path traversal attacks
46+
func validateDatabase(database string) error {
47+
// Check for URL-encoded forward slashes that could enable path traversal
48+
if strings.Contains(database, "%2f") || strings.Contains(database, "%2F") {
49+
return fmt.Errorf("invalid database name: contains URL-encoded forward slash")
50+
}
51+
52+
// Check for literal path traversal patterns
53+
if strings.Contains(database, "../") || strings.Contains(database, "..\\") {
54+
return fmt.Errorf("invalid database name: contains path traversal pattern")
55+
}
56+
57+
// Check for other URL-encoded characters that could be problematic
58+
problematicEncoded := []string{"%00", "%20", "%22", "%27", "%3B", "%3C", "%3E"}
59+
for _, encoded := range problematicEncoded {
60+
if strings.Contains(strings.ToLower(database), encoded) {
61+
return fmt.Errorf("invalid database name: contains URL-encoded character '%s'", encoded)
62+
}
63+
}
64+
65+
return nil
66+
}
67+
4468
func (p *PostgresPlugin) DB(config *engine.PluginConfig) (*gorm.DB, error) {
4569
connectionInput, err := p.ParseConnectionConfig(config)
4670
if err != nil {
@@ -52,6 +76,11 @@ func (p *PostgresPlugin) DB(config *engine.PluginConfig) (*gorm.DB, error) {
5276
return nil, err
5377
}
5478

79+
// Validate database name to prevent path traversal attacks
80+
if err := validateDatabase(connectionInput.Database); err != nil {
81+
return nil, err
82+
}
83+
5584
// Construct PostgreSQL URL securely using url.URL struct
5685
u := &url.URL{
5786
Scheme: "postgresql",

0 commit comments

Comments
 (0)