Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add percentencode impl #193

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package multiaddr
import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"regexp"
"strings"
"testing"
"time"
Expand All @@ -12,6 +14,7 @@ import (

"github.com/ipfs/go-cid"
mh "github.com/multiformats/go-multihash"
"github.com/multiformats/go-varint"
)

func newMultiaddr(t *testing.T, a string) Multiaddr {
Expand Down Expand Up @@ -572,6 +575,9 @@ func TestRoundTrip(t *testing.T) {
"/ip4/127.0.0.1/udp/123",
"/ip4/127.0.0.1/udp/123/ip6/::",
"/ip4/127.0.0.1/udp/1234/quic-v1/webtransport/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g",
"/percentencode/hello%2Fworld",
"/percentencode/hello%2Fworld%25",
"/percentencode/%25hello%2Fworld%25",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
} {
Expand All @@ -586,6 +592,24 @@ func TestRoundTrip(t *testing.T) {
}
}

func TestRoundTripPercentEncodeLowercase(t *testing.T) {
// Same as TestRoundTrip, but with lowercase percent-encoded characters.
for _, s := range []string{
"/percentencode/hello%2fworld",
"/percentencode/hello%2fworld%25",
"/percentencode/%25hello%2fworld%25",
} {
ma, err := NewMultiaddr(s)
if err != nil {
t.Errorf("error when parsing %q: %s", s, err)
continue
}
if ma.String() != strings.Replace(s, "%2f", "%2F", -1) {
t.Errorf("failed to round trip %q, got %q", s, ma.String())
}
}
}

func TestIPFSvP2P(t *testing.T) {
var (
p2pAddr = "/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"
Expand Down Expand Up @@ -811,3 +835,65 @@ func TestContains(t *testing.T) {
require.False(t, Contains(addrs, newMultiaddr(t, "/ip4/4.3.2.1/udp/1234/utp")))
require.False(t, Contains(nil, a1))
}

func TestInvalidPercentEncode(t *testing.T) {
testcases := []string{
"/percentencode/asdfooo%",
"/percentencode/%",
"/percentencode/%FFooo",
}
for _, tc := range testcases {
_, err := NewMultiaddr(tc)
require.Error(t, err)
}
}

func TestByteRepresentationOfPercentEncode(t *testing.T) {
msg := "HELLO%2FDINO! 🦖"
fmt.Println("msg is", msg, len(msg))
ma, err := NewMultiaddr("/percentencode/" + msg)
require.NoError(t, err)
expected := []byte("%_HELLO/DINO! 🦖")
expected[1] = 16 // msg len
require.Equal(t, expected, ma.Bytes())
}

func FuzzPercentEncode(f *testing.F) {
f.Add("/percentencode/helloworld")
manySlashes := regexp.MustCompile(`\/+`)
f.Fuzz(func(t *testing.T, in string) {
in = strings.TrimRight(in, "/")
in = manySlashes.ReplaceAllString(in, "/")
in = strings.ReplaceAll(in, "%2f", "%2F")
ma, err := NewMultiaddr(in)
if err == nil {
require.Equal(t, in, ma.String())
}
})
}

func FuzzPercentEncodeDecoding(f *testing.F) {
b := []byte("%_HELLO/DINO! 🦖")
b[1] = 16 // msg len
f.Add(b)
f.Fuzz(func(t *testing.T, in []byte) {
if len(in) < 3 {
return
}

var buf bytes.Buffer

buf.WriteByte('%')
varintLen := varint.ToUvarint(uint64(len(in)))
buf.ReadFrom(bytes.NewReader(varintLen))
buf.ReadFrom(bytes.NewReader(in))

ma, err := NewMultiaddrBytes(buf.Bytes())
if err == nil {
s := ma.String()
ma, err := NewMultiaddr(s)
require.NoError(t, err)
require.Equal(t, s, ma.String())
}
})
}
9 changes: 9 additions & 0 deletions protocols.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
P_DNSADDR = 56
P_UDP = 273
P_DCCP = 33
P_PERCENTENCODE = 37 // 37 = "%" in ascii
P_IP6 = 41
P_IP6ZONE = 42
P_IPCIDR = 43
Expand Down Expand Up @@ -267,6 +268,13 @@ var (
Code: P_WEBRTC,
VCode: CodeToVarint(P_WEBRTC),
}
protoPercentEncode = Protocol{
Name: "percentencode",
Code: P_PERCENTENCODE,
VCode: CodeToVarint(P_PERCENTENCODE),
Size: LengthPrefixedVarSize,
Transcoder: TranscoderPercentEncode,
}
)

func init() {
Expand Down Expand Up @@ -306,6 +314,7 @@ func init() {
protoWSS,
protoPlaintextV2,
protoWebRTC,
protoPercentEncode,
} {
if err := AddProtocol(p); err != nil {
panic(err)
Expand Down
77 changes: 77 additions & 0 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,80 @@ func certHashStB(s string) ([]byte, error) {
func certHashBtS(b []byte) (string, error) {
return multibase.Encode(multibase.Base64url, b)
}

var TranscoderPercentEncode = NewTranscoderFromFunctions(percentEncodeStB, percentEncodeBtS, nil)

func percentEncodeStB(s string) ([]byte, error) {
size := 0
for i := 0; i < len(s); {
switch c := s[i]; c {
case '%':
size++
i += 3
default:
size++
i++
}
}

buf := make([]byte, size)

j := 0
for i := 0; i < len(s); {
switch c := s[i]; c {
case '%':
if i+3 > len(s) {
return nil, fmt.Errorf("invalid percent encoding: %s", string(s[i:]))
}
switch string(s[i : i+3]) {
case "%25":
buf[j] = '%'
case "%2F", "%2f":
buf[j] = '/'
default:
return nil, fmt.Errorf("invalid percent encoding: %s. Only / and %% are escaped", string(s[i:i+3]))
}
i += 3
j++
default:
buf[j] = c
i++
j++
}
}

return buf, nil

}

func percentEncodeBtS(b []byte) (string, error) {
// The following code is a loose copy from Go's url.QueryEscape
hexCount := 0
for _, c := range b {
switch c {
case '%', '/':
hexCount++
default:
}
}

var s strings.Builder
s.Grow(len(b) + hexCount*2)

for _, c := range b {
switch c {
case '%':
s.WriteByte('%')
s.WriteByte('2')
s.WriteByte('5')
case '/':
s.WriteByte('%')
s.WriteByte('2')
s.WriteByte('F')
default:
s.WriteByte(c)
}
}

return s.String(), nil
}