Configurable max request header size

This commit is contained in:
Lucas Rodriguez 2024-09-16 04:30:04 -05:00 committed by GitHub
parent ac1dad3d14
commit 9750bbc353
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 108 additions and 5 deletions

View file

@ -1511,3 +1511,63 @@ func (s *SimpleSuite) TestDenyFragment() {
require.NoError(s.T(), err)
assert.Equal(s.T(), http.StatusBadRequest, resp.StatusCode)
}
func (s *SimpleSuite) TestMaxHeaderBytes() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
listener, err := net.Listen("tcp", "127.0.0.1:9000")
require.NoError(s.T(), err)
ts := &httptest.Server{
Listener: listener,
Config: &http.Server{
Handler: handler,
MaxHeaderBytes: 1.25 * 1024 * 1024, // 1.25 MB
},
}
ts.Start()
defer ts.Close()
// The test server and traefik config file both specify a max request header size of 1.25 MB.
file := s.adaptFile("fixtures/simple_max_header_size.toml", struct {
TestServer string
}{ts.URL})
s.traefikCmd(withConfigFile(file))
testCases := []struct {
name string
headerSize int
expectedStatus int
}{
{
name: "1.25MB header",
headerSize: int(1.25 * 1024 * 1024),
expectedStatus: http.StatusOK,
},
{
name: "1.5MB header",
headerSize: int(1.5 * 1024 * 1024),
expectedStatus: http.StatusRequestHeaderFieldsTooLarge,
},
{
name: "500KB header",
headerSize: int(500 * 1024),
expectedStatus: http.StatusOK,
},
}
for _, test := range testCases {
s.Run(test.name, func() {
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000", nil)
require.NoError(s.T(), err)
req.Header.Set("X-Large-Header", strings.Repeat("A", test.headerSize))
err = try.Request(req, 2*time.Second, try.StatusCodeIs(test.expectedStatus))
require.NoError(s.T(), err)
})
}
}