Skip to content

Commit

Permalink
json decoding: allow unknown fields
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Nov 4, 2022
1 parent f146349 commit e9775da
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion server/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (m *mockRelay) handleRegisterValidator(w http.ResponseWriter, req *http.Req
}

payload := []types.SignedValidatorRegistration{}
if err := DecodeJSON(req.Body, &payload); err != nil {
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
Expand Down
4 changes: 2 additions & 2 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.
log.Debug("registerValidator")

payload := []types.SignedValidatorRegistration{}
if err := DecodeJSON(req.Body, &payload); err != nil {
if err := json.NewDecoder(req.Body).Decode(&payload); err != nil {
m.respondError(w, http.StatusBadRequest, err.Error())
return
}
Expand Down Expand Up @@ -462,7 +462,7 @@ func (m *BoostService) handleGetPayload(w http.ResponseWriter, req *http.Request

// Decode the body now
payload := new(types.SignedBlindedBeaconBlock)
if err := DecodeJSON(bytes.NewReader(body), payload); err != nil {
if err := json.NewDecoder(bytes.NewReader(body)).Decode(payload); err != nil { // must allow unknown fields to ensure backwards compatibility of future payload changes (i.e. Capella, EIP-4844, etc)
log.WithError(err).WithField("body", string(body)).Error("could not decode request payload from the beacon-node (signed blinded beacon block)")
m.respondError(w, http.StatusBadRequest, err.Error())
return
Expand Down
14 changes: 12 additions & 2 deletions server/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"net/http/httptest"
Expand All @@ -18,6 +19,15 @@ import (
"github.com/stretchr/testify/require"
)

// _decodeJSON reads JSON from io.Reader and decodes it into a struct
func _decodeJSON(r io.Reader, dst any) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(dst); err != nil {
return err
}
return nil
}

type testBackend struct {
boost *BoostService
relays []*mockRelay
Expand Down Expand Up @@ -636,7 +646,7 @@ func TestGetPayloadWithTestdata(t *testing.T) {
require.NoError(t, err)
defer jsonFile.Close()
signedBlindedBeaconBlock := new(types.SignedBlindedBeaconBlock)
require.NoError(t, DecodeJSON(jsonFile, &signedBlindedBeaconBlock))
require.NoError(t, _decodeJSON(jsonFile, &signedBlindedBeaconBlock))

backend := newTestBackend(t, 1, time.Second)
mockResp := types.GetPayloadResponse{
Expand Down Expand Up @@ -664,7 +674,7 @@ func TestGetPayloadToOriginRelayOnly(t *testing.T) {
require.NoError(t, err)
defer jsonFile.Close()
signedBlindedBeaconBlock := new(types.SignedBlindedBeaconBlock)
require.NoError(t, DecodeJSON(jsonFile, &signedBlindedBeaconBlock))
require.NoError(t, _decodeJSON(jsonFile, &signedBlindedBeaconBlock))

// Create a test backend with 2 relays
backend := newTestBackend(t, 2, time.Second)
Expand Down
11 changes: 0 additions & 11 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,6 @@ func ComputeDomain(domainType types.DomainType, forkVersionHex, genesisValidator
return types.ComputeDomain(domainType, forkVersion, genesisValidatorsRoot), nil
}

// DecodeJSON reads JSON from io.Reader and decodes it into a struct
func DecodeJSON(r io.Reader, dst any) error {
decoder := json.NewDecoder(r)
decoder.DisallowUnknownFields()

if err := decoder.Decode(dst); err != nil {
return err
}
return nil
}

// GetURI returns the full request URI with scheme, host, path and args.
func GetURI(url *url.URL, path string) string {
u2 := *url
Expand Down
12 changes: 0 additions & 12 deletions server/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ func TestMakePostRequest(t *testing.T) {
require.Equal(t, 0, code)
}

func TestDecodeJSON(t *testing.T) {
// test disallows unknown fields
var x struct {
A int `json:"a"`
B int `json:"b"`
}
payload := bytes.NewReader([]byte(`{"a":1,"b":2,"c":3}`))
err := DecodeJSON(payload, &x)
require.Error(t, err)
require.Equal(t, "json: unknown field \"c\"", err.Error())
}

func TestSendHTTPRequestUserAgent(t *testing.T) {
done := make(chan bool, 1)

Expand Down

0 comments on commit e9775da

Please sign in to comment.