Skip to content

Commit 2c6e8f9

Browse files
authored
fix registerValidator content type detection (#707)
1 parent 47cc71d commit 2c6e8f9

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

services/api/service.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,12 @@ func (api *RelayAPI) handleRegisterValidator(w http.ResponseWriter, req *http.Re
10181018
}
10191019

10201020
// Get the request content type
1021-
proposerContentType := req.Header.Get(HeaderContentType)
1021+
proposerContentType, _, err := getHeaderContentType(req.Header)
1022+
if err != nil {
1023+
api.log.WithError(err).Error("failed to parse proposer content type")
1024+
api.RespondError(w, http.StatusUnsupportedMediaType, err.Error())
1025+
return
1026+
}
10221027
log = log.WithField("proposerContentType", proposerContentType)
10231028

10241029
// Read the encoded validator registrations

services/api/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package api
22

33
import (
44
"fmt"
5+
"mime"
6+
"net/http"
57

68
builderApi "github.com/attestantio/go-builder-client/api"
79
"github.com/attestantio/go-eth2-client/spec"
@@ -184,3 +186,20 @@ func verifyBlockSignature(block *common.VersionedSignedBlindedBeaconBlock, domai
184186
func getPayloadAttributesKey(parentHash string, slot uint64) string {
185187
return fmt.Sprintf("%s-%d", parentHash, slot)
186188
}
189+
190+
// getHeaderContentType parses the Content-Type header and returns the media type and parameters.
191+
// It returns an empty mediaType string and nil parameters if the header is not set or empty.
192+
func getHeaderContentType(header http.Header) (mediatype string, params map[string]string, err error) {
193+
contentType := header.Get(HeaderContentType)
194+
if contentType == "" {
195+
return "", nil, nil
196+
}
197+
198+
// Parse the content type
199+
contentType, params, err = mime.ParseMediaType(contentType)
200+
if err != nil {
201+
return "", nil, errors.Wrap(err, "failed to parse Content-Type header")
202+
}
203+
204+
return contentType, params, nil
205+
}

services/api/utils_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestGetHeaderContentType(t *testing.T) {
11+
for _, tc := range []struct {
12+
header http.Header
13+
expected string
14+
}{
15+
{
16+
header: http.Header{"Content-Type": []string{"application/json"}},
17+
expected: ApplicationJSON,
18+
},
19+
{
20+
header: http.Header{"Content-Type": []string{"application/json; charset=utf-8"}},
21+
expected: ApplicationJSON,
22+
},
23+
{
24+
header: http.Header{"Content-Type": []string{""}},
25+
expected: "",
26+
},
27+
} {
28+
t.Run(tc.expected, func(t *testing.T) {
29+
contentType, _, err := getHeaderContentType(tc.header)
30+
require.NoError(t, err)
31+
require.Equal(t, tc.expected, contentType)
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)