Skip to content

QueryUnescape DSN ConnectionAttribute value #1470

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

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3367,9 +3367,12 @@ func TestConnectionAttributes(t *testing.T) {

attr1 := "attr1"
value1 := "value1"
attr2 := "foo"
value2 := "boo"
dsn += fmt.Sprintf("&connectionAttributes=%s:%s,%s:%s", attr1, value1, attr2, value2)
attr2 := "fo/o"
value2 := "bo/o"
dsn += fmt.Sprintf(
"&connectionAttributes=%s:%s,%s:%s",
attr1, value1, url.QueryEscape(attr2), url.QueryEscape(value2),
)

var db *sql.DB
if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation {
Expand All @@ -3395,6 +3398,17 @@ func TestConnectionAttributes(t *testing.T) {
}
rows.Close()

rows = dbt.mustQuery(queryString, attr1)
if rows.Next() {
rows.Scan(&attrValue)
if attrValue != value1 {
dbt.Errorf("expected %q, got %q", value1, attrValue)
}
} else {
dbt.Errorf("no data")
}
rows.Close()

rows = dbt.mustQuery(queryString, attr2)
if rows.Next() {
rows.Scan(&attrValue)
Expand Down
6 changes: 5 additions & 1 deletion dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,11 @@ func parseDSNParams(cfg *Config, params string) (err error) {

// Connection attributes
case "connectionAttributes":
cfg.ConnectionAttributes = value
connectionAttributes, err := url.QueryUnescape(value)
if err != nil {
return fmt.Errorf("invalid connectionAttributes value: %v", err)
}
cfg.ConnectionAttributes = connectionAttributes

default:
// lazy init
Expand Down