Skip to content

Commit

Permalink
samples: extauthz: do not return request body as a header when it exc…
Browse files Browse the repository at this point in the history
…eeds 60KB (#49363) (#950)

* samples: do not return request body as a header to avoid 431



* Typo fixes



---------

Signed-off-by: Jacek Ewertowski <[email protected]>
Co-authored-by: Jacek Ewertowski <[email protected]>
  • Loading branch information
2 people authored and openshift-merge-bot[bot] committed Jun 12, 2024
1 parent 9be6dc8 commit 4bb06f7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions samples/extauthz/cmd/extauthz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (s *extAuthzServerV3) allow(request *authv3.CheckRequest) *authv3.CheckResp
{
Header: &corev3.HeaderValue{
Key: receivedHeader,
Value: request.GetAttributes().String(),
Value: returnIfNotTooLong(request.GetAttributes().String()),
},
},
{
Expand Down Expand Up @@ -220,7 +220,7 @@ func (s *extAuthzServerV3) deny(request *authv3.CheckRequest) *authv3.CheckRespo
{
Header: &corev3.HeaderValue{
Key: receivedHeader,
Value: request.GetAttributes().String(),
Value: returnIfNotTooLong(request.GetAttributes().String()),
},
},
{
Expand Down Expand Up @@ -262,7 +262,7 @@ func (s *ExtAuthzServer) ServeHTTP(response http.ResponseWriter, request *http.R
if err != nil {
log.Printf("[HTTP] read body failed: %v", err)
}
l := fmt.Sprintf("%s %s%s, headers: %v, body: [%s]\n", request.Method, request.Host, request.URL, request.Header, body)
l := fmt.Sprintf("%s %s%s, headers: %v, body: [%s]\n", request.Method, request.Host, request.URL, request.Header, returnIfNotTooLong(string(body)))
if allowedValue == request.Header.Get(checkHeader) {
log.Printf("[HTTP][allowed]: %s", l)
response.Header().Set(resultHeader, resultAllowed)
Expand Down Expand Up @@ -358,3 +358,12 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
}

func returnIfNotTooLong(body string) string {
// Maximum size of a header accepted by Envoy is 60KiB, so when the request body is bigger than 60KB,
// we don't return it in a response header to avoid rejecting it by Envoy and returning 431 to the client
if len(body) > 60000 {
return "<too-long>"
}
return body
}

0 comments on commit 4bb06f7

Please sign in to comment.