Skip to content
Merged
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
7 changes: 2 additions & 5 deletions url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package urlutil

import (
"bytes"
"net"
"net/url"
"strings"

Expand Down Expand Up @@ -146,11 +147,7 @@ func (u *URL) UpdatePort(newport string) {
if newport == "" {
return
}
if u.Port() != "" {
u.Host = strings.Replace(u.Host, u.Port(), newport, 1)
return
}
u.Host += ":" + newport
u.Host = net.JoinHostPort(u.Hostname(), newport)
}

// TrimPort if any
Expand Down
21 changes: 21 additions & 0 deletions url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ func TestPortUpdate(t *testing.T) {
require.Equalf(t, urlx.String(), expected, "expected %v but got %v", expected, urlx.String())
}

func TestPortUpdateWhenPortMatchesHost(t *testing.T) {
// UpdatePort uses strings.Replace on the whole Host, so the port substring
// can accidentally match a piece of the IP/hostname instead of the actual port.
testcases := []struct {
inputURL string
newport string
expected string
}{
// last octet "80" matches before the real ":80" port
{"http://37.228.93.80:80/", "443", "http://37.228.93.80:443/"},
{"http://37.228.93.443:80/", "8080", "http://37.228.93.443:8080/"},
{"http://[2001:db8::80]:80/", "8080", "http://[2001:db8::80]:8080/"},
}
for _, tc := range testcases {
urlx, err := Parse(tc.inputURL)
require.Nil(t, err)
urlx.UpdatePort(tc.newport)
require.Equalf(t, tc.expected, urlx.String(), "expected %v but got %v", tc.expected, urlx.String())
}
}

func TestUpdateRelPath(t *testing.T) {
// updates existing relative path with new one
exURL := "https://scanme.sh/somepath/abc?key=true"
Expand Down
Loading