Skip to content

Add a param 'unescapeUser' to format dns like 'testdb%25dbuser:password@...' #460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Stan Putrya <root.vagner at gmail.com>
Stanley Gunawan <gunawan.stanley at gmail.com>
Xiaobing Jiang <s7v7nislands at gmail.com>
Xiuming Chen <cc at cxm.cc>

Dajun Qin <dajun.qin at gmail.com>
# Organizations

Barracuda Networks, Inc.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ Default: 0
I/O write timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*.


##### `unescapeUser`

```
Type: bool
Default: false
```

`unescapeUser=true` let the library unescape `user`, sometimes the dsn looks like "testdb%25dbuser..." .


##### System Variables

All other parameters are interpreted as system variables:
Expand Down
16 changes: 16 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Config struct {
MultiStatements bool // Allow multiple statements in one query
ParseTime bool // Parse time values to time.Time
Strict bool // Return warnings as errors
UnescapeUser bool // UnEscape username such as testdb%25testuesr:password.....
}

// FormatDSN formats the given Config into a DSN string which can be passed to
Expand Down Expand Up @@ -353,6 +354,21 @@ func parseDSNParams(cfg *Config, params string) (err error) {
switch value := param[1]; param[0] {

// Disable INFILE whitelist / enable all files
case "unescapeUser":
var isBool bool
cfg.UnescapeUser, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}

if cfg.UnescapeUser {
var err error
cfg.User, err = url.QueryUnescape(cfg.User)
if err != nil {
return errors.New("user unescape failed: " + err.Error())
}
}

case "allowAllFiles":
var isBool bool
cfg.AllowAllFiles, isBool = readBool(value)
Expand Down
12 changes: 12 additions & 0 deletions dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,15 @@ func BenchmarkParseDSN(b *testing.B) {
}
}
}

func TestUnescapeUser(t *testing.T) {
dsn := "testdb%25dbuser:password@tcp(localhost:5555)/?unescapeUser=true"
user := "testdb%dbuser"
cfg, err := ParseDSN(dsn)

if err != nil {
t.Error(err.Error())
} else if cfg.User != user {
t.Errorf("expected %v, got %v", user, dsn)
}
}