From 5f692a76de01918b202aab1bc5d6657bb177a0ea Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 14 Dec 2020 19:56:44 +0100 Subject: [PATCH] fix: normalize remote service endpoint Closes #7826 --- core/commands/pin/remotepin.go | 29 ++++++++++++--- core/commands/pin/remotepin_test.go | 57 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 core/commands/pin/remotepin_test.go diff --git a/core/commands/pin/remotepin.go b/core/commands/pin/remotepin.go index 9d1e3d60b..7b3de85e2 100644 --- a/core/commands/pin/remotepin.go +++ b/core/commands/pin/remotepin.go @@ -443,12 +443,12 @@ TIP: } name := req.Arguments[0] - url := strings.TrimSuffix(req.Arguments[1], "/pins") // fix /pins/pins :-) + url := req.Arguments[1] key := req.Arguments[2] - u, err := neturl.ParseRequestURI(url) - if err != nil || !strings.HasPrefix(u.Scheme, "http") { - return fmt.Errorf("service endpoint must be a valid HTTP URL") + endpoint, err := normalizeEndpoint(url) + if err != nil { + return err } cfg, err := repo.Config() @@ -465,7 +465,7 @@ TIP: cfg.Pinning.RemoteServices[name] = config.RemotePinningService{ Api: config.RemotePinningServiceApi{ - Endpoint: url, + Endpoint: endpoint, Key: key, }, } @@ -708,7 +708,11 @@ func getRemotePinService(env cmds.Environment, name string) (*pinclient.Client, if err != nil { return nil, err } - return pinclient.NewClient(url, key), nil + endpoint, err := normalizeEndpoint(url) + if err != nil { + return nil, err + } + return pinclient.NewClient(endpoint, key), nil } func getRemotePinServiceInfo(env cmds.Environment, name string) (url, key string, err error) { @@ -734,3 +738,16 @@ func getRemotePinServiceInfo(env cmds.Environment, name string) (url, key string } return service.Api.Endpoint, service.Api.Key, nil } + +func normalizeEndpoint(endpoint string) (string, error) { + uri, err := neturl.ParseRequestURI(endpoint) + 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) + uri.Path = strings.TrimSuffix(uri.Path, "/") + // avoid /pins/pins + uri.Path = strings.TrimSuffix(uri.Path, "/pins") + + return uri.String(), nil +} diff --git a/core/commands/pin/remotepin_test.go b/core/commands/pin/remotepin_test.go new file mode 100644 index 000000000..26265da6f --- /dev/null +++ b/core/commands/pin/remotepin_test.go @@ -0,0 +1,57 @@ +package pin + +import ( + "testing" +) + +func TestNormalizeEndpoint(t *testing.T) { + cases := []struct { + in string + err string + out string + }{ + { + in: "https://1.example.com", + err: "", + out: "https://1.example.com", + }, + { + in: "https://2.example.com/", + err: "", + out: "https://2.example.com", + }, + { + in: "https://3.example.com/pins/", + err: "", + out: "https://3.example.com", + }, + { + in: "https://4.example.com/pins", + err: "", + out: "https://4.example.com", + }, + { + in: "http://192.168.0.5:45000/pins", + err: "", + out: "http://192.168.0.5:45000", + }, + { + in: "foo://4.example.com/pins", + err: "service endpoint must be a valid HTTP URL", + out: "", + }, + } + + 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 + } + } + +} -- GitLab