mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fn: agent MaxRequestSize limit (#998)
* fn: agent MaxRequestSize limit Currently, LimitRequestBody() exists to install a http request body size in http/gin server. For production enviroments, this is expected to be used. However, in agents we may need to verify/enforce these size limits and to be able to assert in case of missing limits is valuable. With this change, operators can define an agent env variable to limit this in addition to installing Gin/Http handler. http.MaxBytesReader is superior in some cases as it sets http headers (Connection: close) to guard against subsequent requests. However, NewClampReadCloser() is superior in other cases, where it can cleanly return an API error for this case alone (http.MaxBytesReader() does not return a clean error type for overflow case, which makes it difficult to use it without peeking into its implementation.) For lb agent, upcoming changes rely on such limits enabled and using gin/http handler (http.MaxBytesReader) makes such checks/safety validations difficult. * fn: read/write clamp code adjustment In case of overflows, opt for simple implementation of a partial write followed by return error.
This commit is contained in:
@@ -256,6 +256,10 @@ func (a *agent) GetCall(opts ...CallOpt) (Call, error) {
|
||||
// if we're not going to be able to run this call on this machine, bail here.
|
||||
return nil, models.ErrCallTimeoutServerBusy
|
||||
}
|
||||
err := setMaxBodyLimit(&a.cfg, &c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.da = a.da
|
||||
c.ct = a
|
||||
@@ -281,6 +285,16 @@ func (a *agent) GetCall(opts ...CallOpt) (Call, error) {
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func setMaxBodyLimit(cfg *AgentConfig, c *call) error {
|
||||
if cfg.MaxRequestSize > 0 && c.req.ContentLength > 0 && uint64(c.req.ContentLength) > cfg.MaxRequestSize {
|
||||
return models.ErrRequestContentTooBig
|
||||
}
|
||||
if c.req.Body != nil {
|
||||
c.req.Body = common.NewClampReadCloser(c.req.Body, cfg.MaxRequestSize, models.ErrRequestContentTooBig)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type call struct {
|
||||
*models.Call
|
||||
|
||||
|
||||
Reference in New Issue
Block a user