Commit 5f692a76 authored by Marcin Rataj's avatar Marcin Rataj Committed by Adin Schmahmann

fix: normalize remote service endpoint

Closes #7826
parent c23ea346
......@@ -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
}
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
}
}
}
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