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

database/sql: Open silently connects to default database when url is quoted #42380

Closed
vHanda opened this issue Nov 4, 2020 · 2 comments
Closed

Comments

@vHanda
Copy link

vHanda commented Nov 4, 2020

$ go version
go version go1.15.3 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/vishesh/Library/Caches/go-build"
GOENV="/Users/vishesh/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/vishesh/go/pkg/mod"
GONOPROXY="*.emobg.tech,*.ubeeqo.com"
GONOSUMDB="*.emobg.tech,*.ubeeqo.com"
GOOS="darwin"
GOPATH="/Users/vishesh/go"
GOPRIVATE="*.emobg.tech,*.ubeeqo.com"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.3/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.3/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/7s/rqcr3mc96lv7j4s__zcjrcfm0000gn/T/go-build312611641=/tmp/go-build -gno-record-gcc-switches -fno-common"

Tested on linux as well.

What did you do?

When using sql.Open if you accidentally pass it a string which has quotes, it doesn't fail and silently tries to connect to the localhost with the default settings instead.

It should fail and an error should be shown.

package main

import (
	"database/sql"
	"log"

	_ "github.com/lib/pq"
)

func main() {
	dbUrl := "\"postgres://username:password@postgres:5432/postgres?sslmode=disable\""
	db, err := sql.Open("postgres", dbUrl)
	if err != nil {
		log.Panic("sqlx connect failed", err)
		return
	}

	err = db.Ping()
	if err != nil {
		log.Panic("Ping failed: ", err)
	}
}

What did you expect to see?

A clear error instead of it silently deciding to connect to a different database.

What did you see instead?

2020/11/04 20:21:55 Ping failed: dial tcp [::1]:5432: connect: connection refused
panic: Ping failed: dial tcp [::1]:5432: connect: connection refused

goroutine 1 [running]:
log.Panic(0xc0000f7f58, 0x2, 0x2)
	/usr/local/Cellar/go/1.15.3/libexec/src/log/log.go:351 +0xae
main.main()
	/Users/vishesh/src/gitjournal/gotrue/main.go:20 +0x14a

It's connecting to the database in localhost instead of the host postgres as specified.

@seankhliao
Copy link
Member

I think this should be filed with the driver, github.com/lib/pq, rather than with database/sql?

@vHanda
Copy link
Author

vHanda commented Nov 4, 2020

@seankhliao : You're right. I cannot reproduce this with the mysql driver.

package main

import (
	"database/sql"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	dbUrl := "\"vish@tcp(flower:3306)/gotrue\""
	db, err := sql.Open("mysql", dbUrl)
	if err != nil {
		log.Panic("sql connect failed", err)
		return
	}

	err = db.Ping()
	if err != nil {
		log.Panic("Ping failed: ", err)
	}
}
2020/11/04 23:39:05 Ping failed: dial tcp: lookup flower: no such host
panic: Ping failed: dial tcp: lookup flower: no such host

Filed in lib/pq - lib/pq#1009

@vHanda vHanda closed this as completed Nov 4, 2020
@golang golang locked and limited conversation to collaborators Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants