diff --git a/core/commands/pin/remotepin.go b/core/commands/pin/remotepin.go index 7b3de85e24d127b48d597a9643d05e423768fa86..b29860a3f4aa1e4b2ea70590ee6abd4dd37af927 100644 --- a/core/commands/pin/remotepin.go +++ b/core/commands/pin/remotepin.go @@ -10,6 +10,7 @@ import ( "time" neturl "net/url" + gopath "path" "golang.org/x/sync/errgroup" @@ -744,10 +745,20 @@ func normalizeEndpoint(endpoint string) (string, error) { if err != nil || !strings.HasPrefix(uri.Scheme, "http") { 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, "/") - // 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 } diff --git a/core/commands/pin/remotepin_test.go b/core/commands/pin/remotepin_test.go index 26265da6fb786e2cf133c6e7c963c10fbf58db52..d1dfa8fd5e4baf6f9f42637303e12b7e6ebeb9c6 100644 --- a/core/commands/pin/remotepin_test.go +++ b/core/commands/pin/remotepin_test.go @@ -22,16 +22,26 @@ func TestNormalizeEndpoint(t *testing.T) { }, { in: "https://3.example.com/pins/", - err: "", - out: "https://3.example.com", + err: "service endpoint should be provided without the /pins suffix", + out: "", }, { 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: "", - 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: "", out: "http://192.168.0.5:45000", }, @@ -44,14 +54,14 @@ func TestNormalizeEndpoint(t *testing.T) { for _, tc := range cases { 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() { t.Errorf("unexpected error for %q: expected %q; got %q", tc.in, tc.err, err) continue } + if out != tc.out { + t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out) + continue + } } }