From b2e2ccbf16565d9706a2ffe77aafb21fb545a8d5 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Tue, 14 Nov 2023 19:17:17 +0800 Subject: [PATCH] QueryUnescape DSN ConnectionAttribute value (#1470) --- AUTHORS | 2 ++ driver_test.go | 18 +++++++++++++++--- dsn.go | 6 +++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index dec27daca..c84293100 100644 --- a/AUTHORS +++ b/AUTHORS @@ -109,6 +109,7 @@ Xiangyu Hu Xiaobing Jiang Xiuming Chen Xuehong Chan +Zhang Xiang Zhenye Xie Zhixin Wen Ziheng Lyu @@ -127,6 +128,7 @@ InfoSum Ltd. Keybase Inc. Multiplay Ltd. Percona LLC +PingCAP Inc. Pivotal Inc. Stripe Inc. Zendesk Inc. diff --git a/driver_test.go b/driver_test.go index f256011a7..8c02f6d1c 100644 --- a/driver_test.go +++ b/driver_test.go @@ -3379,9 +3379,10 @@ 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 += "&connectionAttributes=" + url.QueryEscape(fmt.Sprintf("%s:%s,%s:%s", attr1, value1, attr2, value2)) + var db *sql.DB if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation { @@ -3407,6 +3408,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) diff --git a/dsn.go b/dsn.go index 50c7ec413..ef0608636 100644 --- a/dsn.go +++ b/dsn.go @@ -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