-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TT-5186] Add a global request body size limit #3978
[TT-5186] Add a global request body size limit #3978
Conversation
Nice! Clean and simple! Would also want add simple test to cover it? The test can look like this (have not tested): func TestRequestBodyLimit(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()
globalConf := ts.Gw.GetConfig()
globalConf. MaxRequestBodySize = 1
ts.Gw.SetConfig(globalConf)
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.UseKeylessAccess = true
})
_, _ = ts.Run(t, test.TestCase{
Path: "/sample/", Method: "POST", Data: "test", Code: http.StatusRequestEntityTooLarge,
})
} |
I've added a small test for this. Thank you very much for the pretty much finished test snippet! |
@PatrickTaibel I'm noticing this is used as the base of the following PR: #3979 - can you please include those changes here, and close that PR? |
@titpetric Those two PRs are independent of each other (and both branches are directly based on master). This one adds a global serverwide hard limit on the request body size. The other PR fixes enabling the request size limit middleware which is API specific. |
My mistake, it seemed like a patch of this one. Will do housekeeping there too, thanks :) |
10df6a9
to
b57f3a3
Compare
b57f3a3
to
232bf78
Compare
https://tyktech.atlassian.net/browse/TT-5186 Closes #3978 --------- Co-authored-by: Patrick Taibel <[email protected]> Co-authored-by: Tit Petric <[email protected]>
Description
Adds a global request body size limit to the config. For incoming requests the Content-Length will be checked against the limit. If it's set, the limit enabled and the Content-Length exceeds the limit a status 413 (Request Entity Too Large) will be returned and the further handling aborted. Additionally, a
http.MaxBytesReader
is applied to the request body to force any reads to be limited to the global server limit (e.g. when the Content-Length is not set or manipulated).Related Issue
#3976
Motivation and Context
Tyk API Gateway copies the whole request into memory at the beginning of the request handling. Large requests could fill up memory if they are not blocked. At the moment this would need to be done by an additional load balancer or proxy in front.
How This Has Been Tested
As long as the limit is not enabled (which is the default case) there is no change in behavior. Therefore, no existing installations are affected by the change. In case it's enabled only requests larger then the configured limit are affected.
Types of changes
Checklist
fork, don't request your
master
!master
branch (left side). Also, you should startyour branch off our latest
master
.go mod tidy && go mod vendor
go fmt -s
go vet