Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
config: handle special chars in proxy settings with url.Userinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
talwai committed Mar 21, 2017
1 parent 73850da commit 9486db3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
15 changes: 12 additions & 3 deletions config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ func getProxySettings(m *ini.Section) *ProxySettings {
// URL turns ProxySettings into an idiomatic URL struct
func (p *ProxySettings) URL() (*url.URL, error) {
// construct scheme://user:pass@host:port
var userpass string
var userpass *url.Userinfo
if p.User != "" {
if p.Password != "" {
userpass = fmt.Sprintf("%s:%s@", p.User, p.Password)
userpass = url.UserPassword(p.User, p.Password)
} else {
userpass = url.User(p.User)
}
}

return url.Parse(fmt.Sprintf("%s://%s%s:%v", p.Scheme, userpass, p.Host, p.Port))
var path string
if userpass != nil {
path = fmt.Sprintf("%s://%s@%s:%v", p.Scheme, userpass.String(), p.Host, p.Port)
} else {
path = fmt.Sprintf("%s://%s:%v", p.Scheme, p.Host, p.Port)
}

return url.Parse(path)
}
48 changes: 48 additions & 0 deletions config/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ func TestGetProxySettings(t *testing.T) {
assert.Nil(err)
assert.Equal("https://myhost:3128", s.String())

// generic user name
f, _ = ini.Load([]byte(strings.Join([]string{
"[Main]",
"proxy_host = https://myhost",
"proxy_port = 3129",
"proxy_user = aaditya",
}, "\n")))

s, err = getURL(f)
assert.Nil(err)

assert.Equal("https://aaditya@myhost:3129", s.String())

// special char in user name <3
f, _ = ini.Load([]byte(strings.Join([]string{
"[Main]",
"proxy_host = myhost",
"proxy_port = 3129",
"proxy_user = léo",
}, "\n")))

s, err = getURL(f)
assert.Nil(err)

// user is url-encoded and decodes to original string
assert.Equal("http://l%C3%A9o@myhost:3129", s.String())
assert.Equal("léo", s.User.Username())

// generic user-pass
f, _ = ini.Load([]byte(strings.Join([]string{
"[Main]",
"proxy_host = myhost",
Expand All @@ -54,6 +83,7 @@ func TestGetProxySettings(t *testing.T) {
assert.Nil(err)
assert.Equal("http://aaditya:password_12@myhost:3129", s.String())

// user-pass with schemed host
f, _ = ini.Load([]byte(strings.Join([]string{
"[Main]",
"proxy_host = https://myhost",
Expand All @@ -65,4 +95,22 @@ func TestGetProxySettings(t *testing.T) {
s, err = getURL(f)
assert.Nil(err)
assert.Equal("https://aaditya:password_12@myhost:3129", s.String())

// special characters in password
f, _ = ini.Load([]byte(strings.Join([]string{
"[Main]",
"proxy_host = https://myhost",
"proxy_port = 3129",
"proxy_user = aaditya",
"proxy_password = /:!?&=@éÔγλῶσσα",
}, "\n")))

s, err = getURL(f)
assert.Nil(err)

// password is url-encoded and decodes to the original string
assert.Equal("https://aaditya:%2F%3A%21%3F&=%40%C3%A9%C3%94%CE%B3%CE%BB%E1%BF%B6%CF%83%CF%83%CE%B1@myhost:3129", s.String())

pass, _ := s.User.Password()
assert.Equal("/:!?&=@éÔγλῶσσα", pass)
}

0 comments on commit 9486db3

Please sign in to comment.