Skip to content

Commit d31f376

Browse files
committed
PR comments
1 parent e0315b9 commit d31f376

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

multiaddr_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ var good = []string{
192192
"/ip4/127.0.0.1/tcp/127/wss",
193193
"/ip4/127.0.0.1/tcp/127/webrtc-direct",
194194
"/ip4/127.0.0.1/tcp/127/webrtc",
195+
"/http-path/tmp%2Fbar",
196+
"/http-path/tmp%2Fbar%2Fbaz",
197+
"/http-path/foo",
198+
"/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo",
199+
"/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
195200
}
196201

197202
func TestConstructSucceeds(t *testing.T) {
@@ -927,16 +932,28 @@ func TestDNS(t *testing.T) {
927932

928933
func TestHTTPPath(t *testing.T) {
929934
t.Run("bad addr", func(t *testing.T) {
930-
badAddr := "/http-path/thisIsMissingAfullBytes%f"
935+
badAddr := "/http-path/thisIsMissingAfullByte%f"
931936
_, err := NewMultiaddr(badAddr)
932937
require.Error(t, err)
933938
})
934939

940+
t.Run("only reads the http-path part", func(t *testing.T) {
941+
addr := "/http-path/tmp%2Fbar/p2p-circuit" // The http-path only reference the part immediately after it. It does not include the rest of the multiaddr (like the /path component sometimes does)
942+
m, err := NewMultiaddr(addr)
943+
require.NoError(t, err)
944+
m.ValueForProtocol(P_HTTP_PATH)
945+
v, err := m.ValueForProtocol(P_HTTP_PATH)
946+
require.NoError(t, err)
947+
require.Equal(t, "tmp%2Fbar", v)
948+
})
949+
935950
t.Run("round trip", func(t *testing.T) {
936951
cases := []string{
937952
"/http-path/tmp%2Fbar",
938953
"/http-path/tmp%2Fbar%2Fbaz",
939954
"/http-path/foo",
955+
"/ip4/127.0.0.1/tcp/0/p2p/12D3KooWCryG7Mon9orvQxcS1rYZjotPgpwoJNHHKcLLfE4Hf5mV/http-path/foo",
956+
"/ip4/127.0.0.1/tcp/443/tls/sni/example.com/http/http-path/foo",
940957
}
941958
for _, c := range cases {
942959
m, err := NewMultiaddr(c)

transcoders.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,25 @@ var TranscoderHTTPPath = NewTranscoderFromFunctions(httpPathStB, httpPathBtS, va
460460

461461
func httpPathStB(s string) ([]byte, error) {
462462
unescaped, err := url.QueryUnescape(s)
463+
if err != nil {
464+
return nil, err
465+
}
466+
if len(unescaped) == 0 {
467+
return nil, fmt.Errorf("empty http path is not allowed")
468+
}
463469
return []byte(unescaped), err
464470
}
465471

466472
func httpPathBtS(b []byte) (string, error) {
473+
if len(b) == 0 {
474+
return "", fmt.Errorf("empty http path is not allowed")
475+
}
467476
return url.QueryEscape(string(b)), nil
468477
}
469478

470479
func validateHTTPPath(b []byte) error {
480+
if len(b) == 0 {
481+
return fmt.Errorf("empty http path is not allowed")
482+
}
471483
return nil // We can represent any byte slice when we escape it.
472484
}

0 commit comments

Comments
 (0)