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

*: [security] neutralizes externally-controlled format DSN strings #37489

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 br/pkg/lightning/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (param *MySQLConnectParam) ToDSN() string {
hostPort := net.JoinHostPort(param.Host, strconv.Itoa(param.Port))
dsn := fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&sql_mode='%s'&maxAllowedPacket=%d&tls=%s",
param.User, param.Password, hostPort,
param.SQLMode, param.MaxAllowedPacket, param.TLS)
url.QueryEscape(param.SQLMode), param.MaxAllowedPacket, url.QueryEscape(param.TLS))

for k, v := range param.Vars {
dsn += fmt.Sprintf("&%s='%s'", k, url.QueryEscape(v))
Expand Down
7 changes: 6 additions & 1 deletion cmd/explaintest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -688,7 +689,11 @@ func main() {

mdb, err = openDBWithRetry(
"mysql",
"root@tcp(localhost:"+fmt.Sprint(port)+")/"+dbName+"?allowAllFiles=true",
fmt.Sprintf(
"root@tcp(localhost:%d)/%s?allowAllFiles=true",
port,
url.QueryEscape(dbName),
),
)
if err != nil {
log.Fatal("open DB failed", zap.Error(err))
Expand Down
5 changes: 4 additions & 1 deletion cmd/importer/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"math"
"math/rand"
"net"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -318,7 +320,8 @@ func execSQL(db *sql.DB, sql string) error {
}

func createDB(cfg DBConfig) (*sql.DB, error) {
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Name)
hostPort := net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port))
dbDSN := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", cfg.User, cfg.Password, hostPort, url.QueryEscape(cfg.Name))
db, err := sql.Open("mysql", dbDSN)
if err != nil {
return nil, errors.Trace(err)
Expand Down
3 changes: 2 additions & 1 deletion dumpling/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"net"
"net/url"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -208,7 +209,7 @@ func (conf *Config) GetDSN(db string) string {
// https://github.com/go-sql-driver/mysql#maxallowedpacket
hostPort := net.JoinHostPort(conf.Host, strconv.Itoa(conf.Port))
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?collation=utf8mb4_general_ci&readTimeout=%s&writeTimeout=30s&interpolateParams=true&maxAllowedPacket=0",
conf.User, conf.Password, hostPort, db, conf.ReadTimeout)
conf.User, conf.Password, hostPort, url.QueryEscape(db), conf.ReadTimeout)
Copy link
Contributor

Choose a reason for hiding this comment

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

seems dbName does not support url.QueryEscape. So in order to connect to databases like test!a I prefer we don't escape it

Copy link
Author

Choose a reason for hiding this comment

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

Hi @lance6716 - It must be escaped due to a security vulnerability matter, see the release note of this PR summary. Or do you have a better way to validate the database name?

Copy link
Contributor

@lance6716 lance6716 Sep 9, 2022

Choose a reason for hiding this comment

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

I guess the vulnerability lies in parsing DSN? If so, maybe we can

/cc @hawkingrei @gozssky

Copy link
Contributor

Choose a reason for hiding this comment

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

if len(conf.Security.CAPath) > 0 {
dsn += "&tls=dumpling-tls-target"
}
Expand Down
5 changes: 4 additions & 1 deletion dumpling/tests/s3/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"context"
"database/sql"
"fmt"
"net"
"net/url"
"os"
"strconv"

_ "github.com/go-sql-driver/mysql"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -48,7 +51,7 @@ func main() {
return errors.Trace(err)
}

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4", "root", "", "127.0.0.1", port, database)
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", "root", "", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)), url.QueryEscape(database))
db, err := sql.Open("mysql", dsn)
if err != nil {
return errors.Trace(err)
Expand Down
9 changes: 6 additions & 3 deletions util/dbutil/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"strconv"
Expand Down Expand Up @@ -107,12 +108,14 @@ func GetDBConfigFromEnv(schema string) DBConfig {

// OpenDB opens a mysql connection FD
func OpenDB(cfg DBConfig, vars map[string]string) (*sql.DB, error) {
var dbDSN string
var dbDSN, hostPort string

hostPort = net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port))
if len(cfg.Snapshot) != 0 {
log.Info("create connection with snapshot", zap.String("snapshot", cfg.Snapshot))
dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&tidb_snapshot=%s", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Snapshot)
dbDSN = fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&tidb_snapshot=%s", cfg.User, cfg.Password, hostPort, url.QueryEscape(cfg.Snapshot))
} else {
dbDSN = fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4", cfg.User, cfg.Password, cfg.Host, cfg.Port)
dbDSN = fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4", cfg.User, cfg.Password, hostPort)
}

for key, val := range vars {
Expand Down
5 changes: 4 additions & 1 deletion util/importer/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"database/sql"
"fmt"
"math"
"net"
"net/url"
"strconv"

_ "github.com/go-sql-driver/mysql" // for mysql driver
Expand Down Expand Up @@ -228,7 +230,8 @@ func execSQL(db *sql.DB, sql string) error {
}

func createDB(cfg dbutil.DBConfig) (*sql.DB, error) {
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Schema)
hostPort := net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port))
dbDSN := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", cfg.User, cfg.Password, hostPort, url.QueryEscape(cfg.Schema))
db, err := sql.Open("mysql", dbDSN)
if err != nil {
return nil, errors.Trace(err)
Expand Down