@cryptotaxi247 / kubo / commits / 6851ede8e

refactor: normalization with path.Clean

Marcin Rataj committed Dec 15, 2020 at 21:58 UTC 6851ede8eb2878a967f8052c4b01b6d1fb946db2
2 files changed +32 -11
core/commands/pin/remotepin.go
+14 -3
@@ -10,6 +10,7 @@ import (
10 "time"
11
12 neturl "net/url"
13 + gopath "path"
14
15 "golang.org/x/sync/errgroup"
16
@@ -744,10 +745,20 @@ func normalizeEndpoint(endpoint string) (string, error) {
745 if err != nil || !strings.HasPrefix(uri.Scheme, "http") {
746 return "", fmt.Errorf("service endpoint must be a valid HTTP URL")
747 }
747 - // avoid //pins (https://github.com/ipfs/go-ipfs/issues/7826)
748 +
749 + // cleanup trailing and duplicate slashes (https://github.com/ipfs/go-ipfs/issues/7826)
750 + uri.Path = gopath.Clean(uri.Path)
751 + uri.Path = strings.TrimSuffix(uri.Path, ".")
752 uri.Path = strings.TrimSuffix(uri.Path, "/")
749 - // avoid /pins/pins
750 - uri.Path = strings.TrimSuffix(uri.Path, "/pins")
753 +
754 + // remove any query params
755 + if uri.RawQuery != "" || uri.RawFragment != "" {
756 + return "", fmt.Errorf("service endpoint should be provided without any query parameters")
757 + }
758 +
759 + if strings.HasSuffix(uri.Path, "/pins") {
760 + return "", fmt.Errorf("service endpoint should be provided without the /pins suffix")
761 + }
762
763 return uri.String(), nil
764 }
core/commands/pin/remotepin_test.go
+18 -8
@@ -22,16 +22,26 @@ func TestNormalizeEndpoint(t *testing.T) {
22 },
23 {
24 in: "https://3.example.com/pins/",
25 - err: "",
26 - out: "https://3.example.com",
25 + err: "service endpoint should be provided without the /pins suffix",
26 + out: "",
27 },
28 {
29 in: "https://4.example.com/pins",
30 + err: "service endpoint should be provided without the /pins suffix",
31 + out: "",
32 + },
33 + {
34 + in: "https://5.example.com/./some//nonsense/../path/../path/",
35 err: "",
31 - out: "https://4.example.com",
36 + out: "https://5.example.com/some/path",
37 + },
38 + {
39 + in: "https://6.example.com/endpoint/?query=val",
40 + err: "service endpoint should be provided without any query parameters",
41 + out: "",
42 },
43 {
34 - in: "http://192.168.0.5:45000/pins",
44 + in: "http://192.168.0.5:45000/",
45 err: "",
46 out: "http://192.168.0.5:45000",
47 },
@@ -44,14 +54,14 @@ func TestNormalizeEndpoint(t *testing.T) {
54
55 for _, tc := range cases {
56 out, err := normalizeEndpoint(tc.in)
47 - if out != tc.out {
48 - t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out)
49 - continue
50 - }
57 if err != nil && tc.err != err.Error() {
58 t.Errorf("unexpected error for %q: expected %q; got %q", tc.in, tc.err, err)
59 continue
60 }
61 + if out != tc.out {
62 + t.Errorf("unexpected endpoint for %q: expected %q; got %q", tc.in, tc.out, out)
63 + continue
64 + }
65 }
66
67 }