Skip to content
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

Conversation

PatrickTaibel
Copy link
Contributor

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • Make sure you are requesting to pull a topic/feature/bugfix branch (right side). If pulling from your own
    fork, don't request your master!
  • Make sure you are making a pull request against the master branch (left side). Also, you should start
    your branch off our latest master.
  • My change requires a change to the documentation.
    • If you've changed APIs, describe what needs to be updated in the documentation.
    • If new config option added, ensure that it can be set via ENV variable
  • I have updated the documentation accordingly.
  • Modules and vendor dependencies have been updated; run go mod tidy && go mod vendor
  • When updating library version must provide reason/explanation for this update.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • Check your code additions will not fail linting checks:
    • go fmt -s
    • go vet

@buger
Copy link
Member

buger commented Apr 5, 2022

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,
	})
}

@PatrickTaibel
Copy link
Contributor Author

I've added a small test for this. Thank you very much for the pretty much finished test snippet!
I've changed the way to set the config though. With your way it seems to not restart/reinitialize the http handler and won't reread the HttpServerOptions. Therefore, I've set the config function in the StartTest call for this.

@titpetric
Copy link
Contributor

@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 titpetric changed the title Add a global request body size limit [TT-5186] Add a global request body size limit May 2, 2022
@PatrickTaibel
Copy link
Contributor Author

@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.
In my opinion, those are two different things. But if it's easier to handle on your side, I can for sure combine both changes here.

@titpetric
Copy link
Contributor

My mistake, it seemed like a patch of this one. Will do housekeeping there too, thanks :)

@PatrickTaibel PatrickTaibel force-pushed the pr/add-global-request-size-limit branch from 10df6a9 to b57f3a3 Compare May 24, 2022 13:06
@PatrickTaibel PatrickTaibel force-pushed the pr/add-global-request-size-limit branch from b57f3a3 to 232bf78 Compare July 29, 2022 08:12
titpetric added a commit that referenced this pull request Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants