urlutil: improve error message for urls with port in path (#2377)

This commit is contained in:
Caleb Doxsey 2021-07-20 11:08:50 -06:00 committed by GitHub
parent fbf44261c1
commit 8a74fae2e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 1 deletions

View file

@ -18,6 +18,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"github.com/pomerium/pomerium/internal/httputil" "github.com/pomerium/pomerium/internal/httputil"
"github.com/pomerium/pomerium/internal/urlutil"
) )
// JWTClaimHeaders are headers to add to a request based on IDP claims. // JWTClaimHeaders are headers to add to a request based on IDP claims.
@ -215,7 +216,7 @@ func ParseWeightedURL(dst string) (*WeightedURL, error) {
return nil, err return nil, err
} }
u, err := url.Parse(to) u, err := urlutil.ParseAndValidateURL(to)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %w", to, err) return nil, fmt.Errorf("%s: %w", to, err)
} }

View file

@ -39,6 +39,9 @@ func ParseAndValidateURL(rawurl string) (*url.URL, error) {
} }
u, err := url.Parse(rawurl) u, err := url.Parse(rawurl)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "first path segment in URL cannot contain colon") {
err = fmt.Errorf("%w, have you specified protocol (ex: https)", err)
}
return nil, err return nil, err
} }
if err := ValidateURL(u); err != nil { if err := ValidateURL(u); err != nil {

View file

@ -48,6 +48,7 @@ func TestParseAndValidateURL(t *testing.T) {
{"bad hostname", "https://", nil, true}, {"bad hostname", "https://", nil, true},
{"bad parse", "https://^", nil, true}, {"bad parse", "https://^", nil, true},
{"empty string error", "", nil, true}, {"empty string error", "", nil, true},
{"path segment", "192.168.0.1:1234/path", nil, true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {