mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-07-21 07:23:06 +00:00
limit req body size in login_handler.go
Signed-off-by: Ryan Richard <richardry@vmware.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// Copyright 2022-2024 the Pinniped contributors. All Rights Reserved.
|
||||
// Copyright 2022-2026 the Pinniped contributors. All Rights Reserved.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package login
|
||||
@@ -52,6 +52,8 @@ func NewHandler(
|
||||
auditLogger plog.AuditLogger,
|
||||
) http.Handler {
|
||||
loginHandler := httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20) // 1 MB limit
|
||||
|
||||
if err := auditLogger.AuditRequestParams(r, paramsSafeToLog()); err != nil {
|
||||
plog.DebugErr("error parsing callback request params", err)
|
||||
return httperr.New(http.StatusBadRequest, "error parsing request params")
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package login
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -116,6 +117,8 @@ func TestLoginEndpoint(t *testing.T) {
|
||||
method string
|
||||
path string
|
||||
csrfCookie string
|
||||
contentType string
|
||||
formParams url.Values
|
||||
getHandlerErr error
|
||||
postHandlerErr error
|
||||
|
||||
@@ -350,6 +353,17 @@ func TestLoginEndpoint(t *testing.T) {
|
||||
wantEncodedState: happyState,
|
||||
wantDecodedState: expectedHappyDecodedUpstreamStateParam(),
|
||||
},
|
||||
{
|
||||
name: "POST with body too large",
|
||||
method: http.MethodPost,
|
||||
path: happyPathWithState,
|
||||
csrfCookie: happyCSRFCookie,
|
||||
contentType: "application/x-www-form-urlencoded",
|
||||
formParams: map[string][]string{"k": {strings.Repeat("v", 1_048_576)}},
|
||||
wantStatus: http.StatusBadRequest,
|
||||
wantContentType: htmlContentType,
|
||||
wantBody: "Bad Request: error parsing request params\n",
|
||||
},
|
||||
{
|
||||
name: "happy GET request with err param which can be set by the real POST handler on redirects back to the GET handler",
|
||||
method: http.MethodGet,
|
||||
@@ -461,10 +475,19 @@ func TestLoginEndpoint(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), test.method, test.path, nil)
|
||||
var body io.Reader
|
||||
if test.method == http.MethodPost && len(test.formParams) > 0 {
|
||||
body = strings.NewReader(test.formParams.Encode())
|
||||
}
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), test.method, test.path, body)
|
||||
|
||||
if test.csrfCookie != "" {
|
||||
req.Header.Set("Cookie", test.csrfCookie)
|
||||
}
|
||||
if test.contentType != "" {
|
||||
req.Header.Set("Content-Type", test.contentType)
|
||||
}
|
||||
req, _ = auditid.NewRequestWithAuditID(req, func() string { return "fake-audit-id" })
|
||||
rsp := httptest.NewRecorder()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user