Skip to content

Commit

Permalink
Do not allow relays to have point-at-infinity pubkey (#493)
Browse files Browse the repository at this point in the history
* Do not allow relays to have point-at-infinity pubkey

* Add more dashes

* Initialize pointAtInfinity once

* Replace "pubkey" with "public key" in test name
  • Loading branch information
jtraglia authored Apr 19, 2023
1 parent 7735b00 commit 9acaf56
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
5 changes: 4 additions & 1 deletion server/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ package server

import "fmt"

// ErrMissingRelayPubkey is returned if a new RelayEntry URL has no public key
// ErrMissingRelayPubkey is returned if a new RelayEntry URL has no public key.
var ErrMissingRelayPubkey = fmt.Errorf("missing relay public key")

// ErrPointAtInfinityPubkey is returned if a new RelayEntry URL has an all-zero public key.
var ErrPointAtInfinityPubkey = fmt.Errorf("relay public key cannot be the point-at-infinity")
16 changes: 15 additions & 1 deletion server/relay_entry.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package server

import (
"bytes"
"net/url"
"strings"

"github.com/flashbots/go-boost-utils/types"
)

// The point-at-infinity is 48 zero bytes.
var pointAtInfinityPubkey = [48]byte{}

// RelayEntry represents a relay that mev-boost connects to.
type RelayEntry struct {
PublicKey types.PublicKey
Expand Down Expand Up @@ -41,8 +45,18 @@ func NewRelayEntry(relayURL string) (entry RelayEntry, err error) {
return entry, ErrMissingRelayPubkey
}

// Convert the username string to a public key.
err = entry.PublicKey.UnmarshalText([]byte(entry.URL.User.Username()))
return entry, err
if err != nil {
return entry, err
}

// Check if the public key is the point-at-infinity.
if bytes.Equal(entry.PublicKey[:], pointAtInfinityPubkey[:]) {
return entry, ErrPointAtInfinityPubkey
}

return entry, nil
}

// RelayEntriesToStrings returns the string representation of a list of relay entries
Expand Down
45 changes: 21 additions & 24 deletions server/relay_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,58 @@ func TestParseRelaysURLs(t *testing.T) {
expectedURL string
}{
{
name: "Relay URL with protocol scheme",
relayURL: fmt.Sprintf("http://%[email protected]", publicKey.String()),

name: "Relay URL with protocol scheme",
relayURL: fmt.Sprintf("http://%[email protected]", publicKey.String()),
expectedURI: "http://foo.com",
expectedPublicKey: publicKey.String(),
expectedURL: fmt.Sprintf("http://%[email protected]", publicKey.String()),
},
{
name: "Relay URL without protocol scheme, without public key",
relayURL: "foo.com",

name: "Relay URL without protocol scheme, without public key",
relayURL: "foo.com",
expectedErr: ErrMissingRelayPubkey,
},
{
name: "Relay URL without protocol scheme and with public key",
relayURL: publicKey.String() + "@foo.com",

name: "Relay URL without protocol scheme and with public key",
relayURL: publicKey.String() + "@foo.com",
expectedURI: "http://foo.com",
expectedPublicKey: publicKey.String(),
expectedURL: "http://" + publicKey.String() + "@foo.com",
},
{
name: "Relay URL with public key host and port",
relayURL: publicKey.String() + "@foo.com:9999",

name: "Relay URL with public key host and port",
relayURL: publicKey.String() + "@foo.com:9999",
expectedURI: "http://foo.com:9999",
expectedPublicKey: publicKey.String(),
expectedURL: "http://" + publicKey.String() + "@foo.com:9999",
},
{
name: "Relay URL with IP and port",
relayURL: publicKey.String() + "@12.345.678:9999",

name: "Relay URL with IP and port",
relayURL: publicKey.String() + "@12.345.678:9999",
expectedURI: "http://12.345.678:9999",
expectedPublicKey: publicKey.String(),
expectedURL: "http://" + publicKey.String() + "@12.345.678:9999",
},
{
name: "Relay URL with https IP and port",
relayURL: "https://" + publicKey.String() + "@12.345.678:9999",

name: "Relay URL with https IP and port",
relayURL: "https://" + publicKey.String() + "@12.345.678:9999",
expectedURI: "https://12.345.678:9999",
expectedPublicKey: publicKey.String(),
expectedURL: "https://" + publicKey.String() + "@12.345.678:9999",
},
{
name: "Invalid relay public key",
relayURL: "http://[email protected]",

name: "Relay URL with invalid public key",
relayURL: "http://[email protected]",
expectedErr: types.ErrLength,
},
{
name: "Relay URL with query arg",
relayURL: fmt.Sprintf("http://%[email protected]?id=foo&bar=1", publicKey.String()),

name: "Relay URL with point-at-infinity public key",
relayURL: "http://0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@foo.com",
expectedErr: ErrPointAtInfinityPubkey,
},
{
name: "Relay URL with query arg",
relayURL: fmt.Sprintf("http://%[email protected]?id=foo&bar=1", publicKey.String()),
expectedURI: "http://foo.com?id=foo&bar=1",
expectedPublicKey: publicKey.String(),
expectedURL: fmt.Sprintf("http://%[email protected]?id=foo&bar=1", publicKey.String()),
Expand Down

0 comments on commit 9acaf56

Please sign in to comment.