Skip to content

Commit

Permalink
getHeader: verify that pubkey in message is the relay-key we trust (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris authored Aug 9, 2022
1 parent 222be4e commit e83a51b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
15 changes: 11 additions & 4 deletions server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"github.com/stretchr/testify/require"
)

var (
mockRelaySecretKeyHex = "0x4e343a647c5a5c44d76c2c58b63f02cdf3a9a0ec40f102ebc26363b4b1b95033"
// mockRelayPublicKeyHex = "0x8a1d7b8dd64e0aafe7ea7b6c95065c9364cf99d38470c12ee807d55f7de1529ad29ce2c422e0b65e3d5a05c02caca249"
skBytes, _ = hexutil.Decode(mockRelaySecretKeyHex)
mockRelaySecretKey, _ = bls.SecretKeyFromBytes(skBytes[:])
mockRelayPublicKey = bls.PublicKeyFromSecretKey(mockRelaySecretKey)
)

// mockRelay is used to fake a relay's behavior.
// You can override each of its handler by setting the instance's HandlerOverride_METHOD_TO_OVERRIDE to your own
// handler.
Expand Down Expand Up @@ -49,17 +57,16 @@ type mockRelay struct {

// newMockRelay creates a mocked relay which implements the backend.BoostBackend interface
// A secret key must be provided to sign default and custom response messages
func newMockRelay(t *testing.T, secretKey *bls.SecretKey) *mockRelay {
publicKey := bls.PublicKeyFromSecretKey(secretKey)
relay := &mockRelay{t: t, secretKey: secretKey, publicKey: publicKey, requestCount: make(map[string]int)}
func newMockRelay(t *testing.T) *mockRelay {
relay := &mockRelay{t: t, secretKey: mockRelaySecretKey, publicKey: mockRelayPublicKey, requestCount: make(map[string]int)}

// Initialize server
relay.Server = httptest.NewServer(relay.getRouter())

// Create the RelayEntry with correct pubkey
url, err := url.Parse(relay.Server.URL)
require.NoError(t, err)
urlWithKey := fmt.Sprintf("%s://%s@%s", url.Scheme, hexutil.Encode(publicKey.Compress()), url.Host)
urlWithKey := fmt.Sprintf("%s://%s@%s", url.Scheme, hexutil.Encode(mockRelayPublicKey.Compress()), url.Host)
relay.RelayEntry, err = NewRelayEntry(urlWithKey)
require.NoError(t, err)
return relay
Expand Down
9 changes: 3 additions & 6 deletions server/mock_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package server

import (
"bytes"
"github.com/flashbots/go-boost-utils/bls"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func Test_mockRelay(t *testing.T) {
t.Run("bad payload", func(t *testing.T) {
privateKey, _, err := bls.GenerateNewKeypair()
require.NoError(t, err)

relay := newMockRelay(t, privateKey)
relay := newMockRelay(t)
req, err := http.NewRequest("POST", pathRegisterValidator, bytes.NewReader([]byte("123")))
require.NoError(t, err)
rr := httptest.NewRecorder()
Expand Down
5 changes: 5 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ func (m *BoostService) handleGetHeader(w http.ResponseWriter, req *http.Request)
"value": responsePayload.Data.Message.Value.String(),
})

if relay.PublicKey != responsePayload.Data.Message.Pubkey {
log.Errorf("bid pubkey mismatch. expected: %s - got: %s", relay.PublicKey.String(), responsePayload.Data.Message.Pubkey.String())
return
}

// Verify the relay signature in the relay response
ok, err := types.VerifySignature(responsePayload.Data.Message, m.builderSigningDomain, relay.PublicKey[:], responsePayload.Data.Signature[:])
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"testing"
"time"

"github.com/flashbots/go-boost-utils/bls"

"github.com/flashbots/go-boost-utils/types"
"github.com/stretchr/testify/require"
)
Expand All @@ -32,12 +30,8 @@ func newTestBackend(t *testing.T, numRelays int, relayTimeout time.Duration) *te

relayEntries := make([]RelayEntry, numRelays)
for i := 0; i < numRelays; i++ {
// Generate private key for relay
blsPrivateKey, _, err := bls.GenerateNewKeypair()
require.NoError(t, err)

// Create a mock relay
backend.relays[i] = newMockRelay(t, blsPrivateKey)
backend.relays[i] = newMockRelay(t)
relayEntries[i] = backend.relays[i].RelayEntry
}

Expand Down

0 comments on commit e83a51b

Please sign in to comment.