Commit 6851ede8 authored by Marcin Rataj's avatar Marcin Rataj Committed by Adin Schmahmann

refactor: normalization with path.Clean

parent 5f692a76
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"time" "time"
neturl "net/url" neturl "net/url"
gopath "path"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
...@@ -744,10 +745,20 @@ func normalizeEndpoint(endpoint string) (string, error) { ...@@ -744,10 +745,20 @@ func normalizeEndpoint(endpoint string) (string, error) {
if err != nil || !strings.HasPrefix(uri.Scheme, "http") { if err != nil || !strings.HasPrefix(uri.Scheme, "http") {
return "", fmt.Errorf("service endpoint must be a valid HTTP URL") return "", fmt.Errorf("service endpoint must be a valid HTTP URL")
} }
// avoid //pins (https://github.com/ipfs/go-ipfs/issues/7826)
// cleanup trailing and duplicate slashes (https://github.com/ipfs/go-ipfs/issues/7826)
uri.Path = gopath.Clean(uri.Path)
uri.Path = strings.TrimSuffix(uri.Path, ".")
uri.Path = strings.TrimSuffix(uri.Path, "/") uri.Path = strings.TrimSuffix(uri.Path, "/")
// avoid /pins/pins
uri.Path = strings.TrimSuffix(uri.Path, "/pins") // remove any query params
if uri.RawQuery != "" || uri.RawFragment != "" {
return "", fmt.Errorf("service endpoint should be provided without any query parameters")
}
if strings.HasSuffix(uri.Path, "/pins") {
return "", fmt.Errorf("service endpoint should be provided without the /pins suffix")
}
return uri.String(), nil return uri.String(), nil
} }
...@@ -22,16 +22,26 @@ func TestNormalizeEndpoint(t *testing.T) { ...@@ -22,16 +22,26 @@ func TestNormalizeEndpoint(t *testing.T) {
}, },
{ {
in: "https://3.example.com/pins/", in: "https://3.example.com/pins/",
err: "", err: "service endpoint should be provided without the /pins suffix",
out: "https://3.example.com", out: "",
}, },
{ {
in: "https://4.example.com/pins", in: "https://4.example.com/pins",
err: "service endpoint should be provided without the /pins suffix",
out: "",
},
{
in: "https://5.example.com/./some//nonsense/../path/../path/",
err: "", err: "",
out: "https://4.example.com", out: "https://5.example.com/some/path",
},
{
in: "https://6.example.com/endpoint/?query=val",
err: "service endpoint should be provided without any query parameters",
out: "",
}, },
{ {
in: "http://192.168.0.5:45000/pins", in: "http://192.168.0.5:45000/",
err: "", err: "",
out: "http://192.168.0.5:45000", out: "http://192.168.0.5:45000",
}, },
...@@ -44,14 +54,14 @@ func TestNormalizeEndpoint(t *testing.T) { ...@@ -44,14 +54,14 @@ func TestNormalizeEndpoint(t *testing.T) {
for _, tc := range cases { for _, tc := range cases {
out, err := normalizeEndpoint(tc.in) out, err := normalizeEndpoint(tc.in)
if out != tc.out {
t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out)
continue
}
if err != nil && tc.err != err.Error() { if err != nil && tc.err != err.Error() {
t.Errorf("unexpected error for %q: expected %q; got %q", tc.in, tc.err, err) t.Errorf("unexpected error for %q: expected %q; got %q", tc.in, tc.err, err)
continue continue
} }
if out != tc.out {
t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out)
continue
}
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment