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

Escape SQL username and password parameters before substituting them into a URL #7089

Merged
merged 1 commit into from
Jul 8, 2019
Merged
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
7 changes: 5 additions & 2 deletions sdk/database/helper/connutil/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -54,9 +55,11 @@ func (c *SQLConnectionProducer) Init(ctx context.Context, conf map[string]interf
return nil, fmt.Errorf("connection_url cannot be empty")
}

// QueryHelper doesn't do any SQL escaping, but if it starts to do so
// then maybe we won't be able to use it to do URL substitution any more.
c.ConnectionURL = dbutil.QueryHelper(c.ConnectionURL, map[string]string{
"username": c.Username,
"password": c.Password,
"username": url.PathEscape(c.Username),
"password": url.PathEscape(c.Password),
})

if c.MaxOpenConnections == 0 {
Expand Down
56 changes: 56 additions & 0 deletions sdk/database/helper/connutil/sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package connutil

import (
"context"
"net/url"
"testing"
)

func TestSQLPasswordChars(t *testing.T) {
testCases := []struct {
Username string
Password string
}{
{"postgres", "password{0}"},
{"postgres", "pass:word"},
{"postgres", "pass/word"},
{"postgres", "p@ssword"},
{"postgres", "pass\"word\""},
// Much to my surprise, CREATE USER "{{password}}" PASSWORD 'foo' worked.
Copy link
Contributor

Choose a reason for hiding this comment

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

PG treats anything quoted as a literal and literals can be used for roles and database names.

{"{{password}}", "foo"},
{"user", "{{username}}"},
}
for _, tc := range testCases {
t.Logf("username %q password %q", tc.Username, tc.Password)

sql := &SQLConnectionProducer{}
ctx := context.Background()
conf := map[string]interface{}{
"connection_url": "postgres://{{username}}:{{password}}@localhost:5432/mydb",
"username": tc.Username,
"password": tc.Password,
}
_, err := sql.Init(ctx, conf, false)
if err != nil {
t.Errorf("Init error on %q %q: %+v", tc.Username, tc.Password, err)
} else {
// This jumps down a few layers...
// Connection() uses sql.Open uses lib/pq uses net/url.Parse
u, err := url.Parse(sql.ConnectionURL)
if err != nil {
t.Errorf("URL parse error on %q %q: %+v", tc.Username, tc.Password, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change these to t.Fatalf() to fail fast?

} else {
username := u.User.Username()
password, pPresent := u.User.Password()
if username != tc.Username {
t.Errorf("Parsed username %q != original username %q", username, tc.Username)
}
if !pPresent {
t.Errorf("Password %q not present", tc.Password)
} else if password != tc.Password {
t.Errorf("Parsed password %q != original password %q", password, tc.Password)
}
}
}
}
}