Skip to content

Commit

Permalink
[Functions] Fix signature validation for empty payloads (#10579)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolekk authored Sep 11, 2023
1 parent 2d40de4 commit 9269ec0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
62 changes: 47 additions & 15 deletions core/services/ocr2/plugins/s4/messages_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package s4_test

import (
"crypto/ecdsa"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/s4"
s4_svc "github.com/smartcontractkit/chainlink/v2/core/services/s4"

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

Expand Down Expand Up @@ -55,6 +57,32 @@ func Test_MarshalUnmarshalQuery(t *testing.T) {
require.Equal(t, addressRange, ar)
}

func signRow(t *testing.T, row *s4.Row, address common.Address, pk *ecdsa.PrivateKey) {
t.Helper()

env := &s4_svc.Envelope{
Address: address.Bytes(),
SlotID: uint(row.Slotid),
Version: row.Version,
Expiration: row.Expiration,
Payload: row.Payload,
}
sig, err := env.Sign(pk)
require.NoError(t, err)
row.Signature = sig
}

func marshalUnmarshal(t *testing.T, row *s4.Row) *s4.Row {
t.Helper()

data, err := s4.MarshalRows([]*s4.Row{row})
require.NoError(t, err)
rows, err := s4.UnmarshalRows(data)
require.NoError(t, err)
require.Len(t, rows, 1)
return rows[0]
}

func Test_VerifySignature(t *testing.T) {
t.Parallel()

Expand All @@ -71,20 +99,24 @@ func Test_VerifySignature(t *testing.T) {
for addr[0] != 0 {
pk, addr = testutils.NewPrivateKeyAndAddress(t)
}
rows := generateTestRows(t, 1, time.Minute)
rows[0].Address = addr.Big().Bytes()
env := &s4_svc.Envelope{
Address: addr.Bytes(),
SlotID: uint(rows[0].Slotid),
Version: rows[0].Version,
Expiration: rows[0].Expiration,
Payload: rows[0].Payload,
}
sig, err := env.Sign(pk)
assert.NoError(t, err)
rows[0].Signature = sig
row := generateTestRows(t, 1, time.Minute)[0]
row.Address = addr.Big().Bytes()
signRow(t, row, addr, pk)

err = rows[0].VerifySignature()
require.NoError(t, err)
require.NoError(t, row.VerifySignature())
sameRow := marshalUnmarshal(t, row)
require.NoError(t, sameRow.VerifySignature())
})

t.Run("empty payload", func(t *testing.T) {
pk, addr := testutils.NewPrivateKeyAndAddress(t)
row := generateTestRows(t, 1, time.Minute)[0]
row.Payload = []byte{}
row.Address = addr.Big().Bytes()
signRow(t, row, addr, pk)

require.NoError(t, row.VerifySignature())
sameRow := marshalUnmarshal(t, row)
require.NoError(t, sameRow.VerifySignature())
})
}
8 changes: 7 additions & 1 deletion core/services/s4/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common"

"github.com/smartcontractkit/chainlink/v2/core/utils"
)

Expand Down Expand Up @@ -62,7 +63,12 @@ func (e Envelope) ToJson() ([]byte, error) {
if err != nil {
return nil, err
}
payload, err := json.Marshal(e.Payload)
nonNilPayload := e.Payload
if nonNilPayload == nil {
// prevent unwanted "null" values in JSON representation
nonNilPayload = []byte{}
}
payload, err := json.Marshal(nonNilPayload)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9269ec0

Please sign in to comment.