Skip to content

Commit

Permalink
validate that certhashes are valid multihashes
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 20, 2022
1 parent 494efd0 commit db19e64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func TestConstructFails(t *testing.T) {
"/ip4/127.0.0.1/tcp",
"/ip4/127.0.0.1/quic/1234",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp", // 1 character missing from certhash
"/ip4/127.0.0.1/ipfs",
"/ip4/127.0.0.1/ipfs/tcp",
"/ip4/127.0.0.1/p2p",
Expand Down Expand Up @@ -159,8 +160,8 @@ func TestConstructSucceeds(t *testing.T) {
"/ip4/127.0.0.1/tcp/1234/",
"/ip4/127.0.0.1/udp/1234/quic",
"/ip4/127.0.0.1/udp/1234/quic/webtransport",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/zt1Zv2yaZ",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/zt1Zv2yaZ/certhash/zt1Zv2yaY",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/ipfs/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7",
Expand Down Expand Up @@ -546,8 +547,7 @@ func TestRoundTrip(t *testing.T) {
"/ip4/127.0.0.1/tcp/123/tls",
"/ip4/127.0.0.1/udp/123",
"/ip4/127.0.0.1/udp/123/ip6/::",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/zt1Zv2yaZ",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/zt1Zv2yaZ/certhash/zt1Zv2yaY",
"/ip4/127.0.0.1/udp/1234/quic/webtransport/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
"/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
} {
Expand Down
8 changes: 7 additions & 1 deletion transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,13 @@ var TranscoderCertHash = NewTranscoderFromFunctions(certHashStB, certHashBtS, ni

func certHashStB(s string) ([]byte, error) {
_, data, err := multibase.Decode(s)
return data, err
if err != nil {
return nil, err
}
if _, err := mh.Decode(data); err != nil {
return nil, err
}
return data, nil
}

func certHashBtS(b []byte) (string, error) {
Expand Down

0 comments on commit db19e64

Please sign in to comment.