Skip to content

Commit

Permalink
Enforce configurable max payload size for service requests (#684)
Browse files Browse the repository at this point in the history
* Enforce max message size for service requests

* Rename & make env configurable

---------

Co-authored-by: Chris Hager <[email protected]>
  • Loading branch information
jtraglia and metachris authored Feb 21, 2025
1 parent 604bb5f commit 83c2d74
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ redis-cli DEL boost-relay/sepolia:validators-registration boost-relay/sepolia:va

* `ACTIVE_VALIDATOR_HOURS` - number of hours to track active proposers in redis (default: `3`)
* `API_MAX_HEADER_BYTES` - http maximum header bytes (default: `60_000`)
* `API_MAX_PAYLOAD_BYTES` - http maximum payload bytes (default: `15_728_640`)
* `API_TIMEOUT_READ_MS` - http read timeout in milliseconds (default: `1_500`)
* `API_TIMEOUT_READHEADER_MS` - http read header timeout in milliseconds (default: `600`)
* `API_TIMEOUT_WRITE_MS` - http write timeout in milliseconds (default: `10_000`)
Expand Down
11 changes: 7 additions & 4 deletions services/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var (
apiIdleTimeoutMs = cli.GetEnvInt("API_TIMEOUT_IDLE_MS", 3_000)
apiWriteTimeoutMs = cli.GetEnvInt("API_TIMEOUT_WRITE_MS", 10_000)
apiMaxHeaderBytes = cli.GetEnvInt("API_MAX_HEADER_BYTES", 60_000)
apiMaxPayloadBytes = cli.GetEnvInt("API_MAX_PAYLOAD_BYTES", 15*1024*1024) // 15 MiB

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

body, err := io.ReadAll(req.Body)
limitReader := io.LimitReader(req.Body, int64(apiMaxPayloadBytes))
body, err := io.ReadAll(limitReader)
if err != nil {
log.WithError(err).WithField("contentLength", req.ContentLength).Warn("failed to read request body")
log.WithError(err).Warn("failed to read request body")
api.RespondError(w, http.StatusBadRequest, "failed to read request body")
return
}
Expand Down Expand Up @@ -1408,7 +1410,8 @@ func (api *RelayAPI) handleGetPayload(w http.ResponseWriter, req *http.Request)
}

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

limitReader := io.LimitReader(r, 10*1024*1024) // 10 MB
limitReader := io.LimitReader(r, int64(apiMaxPayloadBytes))
requestPayloadBytes, err := io.ReadAll(limitReader)
if err != nil {
log.WithError(err).Warn("could not read payload")
Expand Down

0 comments on commit 83c2d74

Please sign in to comment.