Skip to content
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

Allow Connecting to Databases with Slashes in the Name via Url Escaping #1406

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Xuehong Chan <chanxuehong at gmail.com>
Zhenye Xie <xiezhenye at gmail.com>
Zhixin Wen <john.wenzhixin at gmail.com>
Ziheng Lyu <zihenglv at gmail.com>
Brian Hendriks <brian at dolthub.com>

# Organizations

Expand All @@ -123,3 +124,4 @@ Percona LLC
Pivotal Inc.
Stripe Inc.
Zendesk Inc.
Dolthub Inc.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ This has the same effect as an empty DSN string:

```

If your database name includes a slash, use the [URL encoding](https://en.wikipedia.org/wiki/Percent-encoding) `%2F`:
```
/dbname%2Fwithslash
```

Alternatively, [Config.FormatDSN](https://godoc.org/github.com/go-sql-driver/mysql#Config.FormatDSN) can be used to create a DSN string by filling a struct.

#### Password
Expand Down
9 changes: 7 additions & 2 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (cfg *Config) FormatDSN() string {

// /dbname
buf.WriteByte('/')
buf.WriteString(cfg.DBName)
dbNameEncoded := url.QueryEscape(cfg.DBName)
Copy link
Member

Choose a reason for hiding this comment

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

This should be PathEscape rather than QueryEscape.
QueryEscape will escape many characters that we don't want to escape.

buf.WriteString(dbNameEncoded)

// [?param1=value1&...&paramN=valueN]
hasParam := false
Expand Down Expand Up @@ -358,7 +359,11 @@ func ParseDSN(dsn string) (cfg *Config, err error) {
break
}
}
cfg.DBName = dsn[i+1 : j]

dbName := dsn[i+1 : j]
if cfg.DBName, err = url.QueryUnescape(dbName); err != nil {
return nil, fmt.Errorf("invalid dbname '%s': %w", dbName, err)
}

break
}
Expand Down
66 changes: 37 additions & 29 deletions dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var testDSNs = []struct {
}, {
"/dbname",
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, CheckConnLiveness: true},
}, {
"/dbname%2Fwithslash",
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname/withslash", Collation: "utf8mb4_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, CheckConnLiveness: true},
}, {
"@/",
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8mb4_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, CheckConnLiveness: true},
Expand All @@ -76,17 +79,20 @@ var testDSNs = []struct {

func TestDSNParser(t *testing.T) {
for i, tst := range testDSNs {
cfg, err := ParseDSN(tst.in)
if err != nil {
t.Error(err.Error())
}
t.Run(tst.in, func(t *testing.T) {
cfg, err := ParseDSN(tst.in)
if err != nil {
t.Error(err.Error())
return
}

// pointer not static
cfg.TLS = nil
// pointer not static
cfg.TLS = nil

if !reflect.DeepEqual(cfg, tst.out) {
t.Errorf("%d. ParseDSN(%q) mismatch:\ngot %+v\nwant %+v", i, tst.in, cfg, tst.out)
}
if !reflect.DeepEqual(cfg, tst.out) {
t.Errorf("%d. ParseDSN(%q) mismatch:\ngot %+v\nwant %+v", i, tst.in, cfg, tst.out)
}
})
}
}

Expand All @@ -113,27 +119,29 @@ func TestDSNParserInvalid(t *testing.T) {

func TestDSNReformat(t *testing.T) {
for i, tst := range testDSNs {
dsn1 := tst.in
cfg1, err := ParseDSN(dsn1)
if err != nil {
t.Error(err.Error())
continue
}
cfg1.TLS = nil // pointer not static
res1 := fmt.Sprintf("%+v", cfg1)

dsn2 := cfg1.FormatDSN()
cfg2, err := ParseDSN(dsn2)
if err != nil {
t.Error(err.Error())
continue
}
cfg2.TLS = nil // pointer not static
res2 := fmt.Sprintf("%+v", cfg2)
t.Run(tst.in, func(t *testing.T) {
dsn1 := tst.in
cfg1, err := ParseDSN(dsn1)
if err != nil {
t.Error(err.Error())
return
}
cfg1.TLS = nil // pointer not static
res1 := fmt.Sprintf("%+v", cfg1)

if res1 != res2 {
t.Errorf("%d. %q does not match %q", i, res2, res1)
}
dsn2 := cfg1.FormatDSN()
cfg2, err := ParseDSN(dsn2)
if err != nil {
t.Error(err.Error())
return
}
cfg2.TLS = nil // pointer not static
res2 := fmt.Sprintf("%+v", cfg2)

if res1 != res2 {
t.Errorf("%d. %q does not match %q", i, res2, res1)
}
})
}
}

Expand Down