Skip to content

Commit 83c2d74

Browse files
jtragliametachris
andauthored
Enforce configurable max payload size for service requests (#684)
* Enforce max message size for service requests * Rename & make env configurable --------- Co-authored-by: Chris Hager <[email protected]>
1 parent 604bb5f commit 83c2d74

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ redis-cli DEL boost-relay/sepolia:validators-registration boost-relay/sepolia:va
123123

124124
* `ACTIVE_VALIDATOR_HOURS` - number of hours to track active proposers in redis (default: `3`)
125125
* `API_MAX_HEADER_BYTES` - http maximum header bytes (default: `60_000`)
126+
* `API_MAX_PAYLOAD_BYTES` - http maximum payload bytes (default: `15_728_640`)
126127
* `API_TIMEOUT_READ_MS` - http read timeout in milliseconds (default: `1_500`)
127128
* `API_TIMEOUT_READHEADER_MS` - http read header timeout in milliseconds (default: `600`)
128129
* `API_TIMEOUT_WRITE_MS` - http write timeout in milliseconds (default: `10_000`)

services/api/service.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ var (
9999
apiIdleTimeoutMs = cli.GetEnvInt("API_TIMEOUT_IDLE_MS", 3_000)
100100
apiWriteTimeoutMs = cli.GetEnvInt("API_TIMEOUT_WRITE_MS", 10_000)
101101
apiMaxHeaderBytes = cli.GetEnvInt("API_MAX_HEADER_BYTES", 60_000)
102+
apiMaxPayloadBytes = cli.GetEnvInt("API_MAX_PAYLOAD_BYTES", 15*1024*1024) // 15 MiB
102103

103104
// api shutdown: wait time (to allow removal from load balancer before stopping http server)
104105
apiShutdownWaitDuration = common.GetEnvDurationSec("API_SHUTDOWN_WAIT_SEC", 30)
@@ -1040,9 +1041,10 @@ func (api *RelayAPI) handleRegisterValidator(w http.ResponseWriter, req *http.Re
10401041
return
10411042
}
10421043

1043-
body, err := io.ReadAll(req.Body)
1044+
limitReader := io.LimitReader(req.Body, int64(apiMaxPayloadBytes))
1045+
body, err := io.ReadAll(limitReader)
10441046
if err != nil {
1045-
log.WithError(err).WithField("contentLength", req.ContentLength).Warn("failed to read request body")
1047+
log.WithError(err).Warn("failed to read request body")
10461048
api.RespondError(w, http.StatusBadRequest, "failed to read request body")
10471049
return
10481050
}
@@ -1408,7 +1410,8 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request)
14081410
}
14091411

14101412
// Read the body first, so we can decode it later
1411-
body, err := io.ReadAll(req.Body)
1413+
limitReader := io.LimitReader(req.Body, int64(apiMaxPayloadBytes))
1414+
body, err := io.ReadAll(limitReader)
14121415
if err != nil {
14131416
if strings.Contains(err.Error(), "i/o timeout") {
14141417
log.WithError(err).Error("getPayload request failed to decode (i/o timeout)")
@@ -2044,7 +2047,7 @@ func (api *RelayAPI) handleSubmitNewBlock(w http.ResponseWriter, req *http.Reque
20442047
}
20452048
}
20462049

2047-
limitReader := io.LimitReader(r, 10*1024*1024) // 10 MB
2050+
limitReader := io.LimitReader(r, int64(apiMaxPayloadBytes))
20482051
requestPayloadBytes, err := io.ReadAll(limitReader)
20492052
if err != nil {
20502053
log.WithError(err).Warn("could not read payload")

0 commit comments

Comments
 (0)