Skip to content

Commit

Permalink
feat(postgres): Add URL escaping to database connection string (#380)
Browse files Browse the repository at this point in the history
* feat(postgres): Add URL escaping to database connection string

Add url.QueryEscape() to properly handle special characters in PostgreSQL
connection parameters. This prevents potential connection issues and improves
security when credentials contain special characters.

Changes:
- Escape username with url.QueryEscape()
- Escape password with url.QueryEscape()
- Escape hostname with url.QueryEscape()
- Escape database name with url.QueryEscape()

Example special characters handled:
- @ in usernames (e.g., [email protected])
- Special chars in passwords (e.g., *, (, ), @)
- Special chars in database names

This change ensures the DSN string is properly formatted regardless of the
characters present in the connection parameters.

Related to PostgreSQL connection string format:
postgres://username:password@hostname:port/database

* Update src/lib/config/config.go

Remove url.QueryEscape for Host

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* Update config.go to match spacing format

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
  • Loading branch information
alexbricepalo and ellipsis-dev[bot] authored Nov 28, 2024
1 parent e8cac50 commit bff3c40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ test_data
# Go workspace file
.idea
.vscode

# VSCode local history
.history
17 changes: 10 additions & 7 deletions src/lib/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "fmt"
import (
"fmt"
"net/url"
)

// this is a pointer so that if someone attempts to use it before loading it will
// panic and force them to load it first.
Expand Down Expand Up @@ -63,12 +66,12 @@ type postgresConfigCommon struct {
func (c postgresConfigCommon) DSN() string {
return fmt.Sprintf(
"postgres://%s:%s@%s:%d/%s?sslmode=disable",
c.User,
c.Password,
c.Host,
c.Port,
c.Database,
)
url.QueryEscape(c.User),
url.QueryEscape(c.Password),
c.Host,
c.Port,
url.QueryEscape(c.Database),
)
}

type carbonConfig struct {
Expand Down

0 comments on commit bff3c40

Please sign in to comment.