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

net/url: userinfo.String() return unexpect url when the username is null string #51545

Closed
guanyux opened this issue Mar 8, 2022 · 2 comments
Closed

Comments

@guanyux
Copy link

guanyux commented Mar 8, 2022

What version of Go are you using (go version)?

$ go version
go version go1.16.4 darwin/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/guanyuding/Library/Caches/go-build"
GOENV="/Users/guanyuding/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/guanyuding/workspace/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/guanyuding/workspace"
GOPRIVATE=""
GOPROXY="https://goproxy.io,direct"
GOROOT="/Users/guanyuding/go.1.16.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/guanyuding/go.1.16.4/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5r/yzy1rx4154b9zcz88p4lzmd00000gn/T/go-build2377834471=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I want to access github api with token like this:

u, err := url.Parse(repo.Addr) // get url
u.User = url.UserPassword(repo.User, repo.Token)
return u.String()

unexpectedly I found that there is an extra colon in the url like this:
"https://:ghp_snNcRtoWc6Uy5Vxxxxxxx@github.com/tf-xxx/tfxxx.git/"

So I review source code about the String() in net/url/url.go , It will return unexpect url when the username is null string.

s := escape(u.username, encodeUserPassword)
if u.passwordSet {
	s += ":" + escape(u.password, encodeUserPassword)
}

What did you expect to see?

I expect that Userinfo.String() function could return correct string when username is null string.

// String returns the encoded userinfo information in the standard form
// of "username[:password]".
func (u *Userinfo) String() string {
	if u == nil {
		return ""
	}
	s := escape(u.username, encodeUserPassword)
	if u.passwordSet {
		**s += ":" + escape(u.password, encodeUserPassword)**
	}
	return s
}

What did you see instead?

@guanyux guanyux changed the title affected/package: net/url affected/package: net/url userinfo.String() return unexpect url when the username is null string Mar 8, 2022
@guanyux guanyux changed the title affected/package: net/url userinfo.String() return unexpect url when the username is null string userinfo.String() return unexpect url when the username is null string affected/package: net/url Mar 8, 2022
@mengzhuo mengzhuo changed the title userinfo.String() return unexpect url when the username is null string affected/package: net/url net/url: userinfo.String() return unexpect url when the username is null string Mar 8, 2022
@seankhliao
Copy link
Member

From RFC 3986, and the documentation the username is required while the password is optional. The colon separates the empty username from the password.

Closing as working as intended.

@guanyux
Copy link
Author

guanyux commented Mar 9, 2022

@seankhliao Thanks for your reply, I found there is a code logic problem that I should use url.User instead of url.UserPassword in this case .

@golang golang locked and limited conversation to collaborators Mar 9, 2023
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