diff --git a/middleware/internal/internal.go b/middleware/internal/internal.go index 7678ee0a3..90746b063 100644 --- a/middleware/internal/internal.go +++ b/middleware/internal/internal.go @@ -16,7 +16,10 @@ type Internal struct { Paths []string } -const redirectHeader string = "X-Accel-Redirect" +const ( + redirectHeader string = "X-Accel-Redirect" + maxRedirectCount int = 10 +) func isInternalRedirect(w http.ResponseWriter) bool { return w.Header().Get(redirectHeader) != "" @@ -37,7 +40,7 @@ func (i Internal) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) iw := internalResponseWriter{ResponseWriter: w} status, err := i.Next.ServeHTTP(iw, r) - for isInternalRedirect(iw) { + for c := 0; c < maxRedirectCount && isInternalRedirect(iw); c++ { // Redirect - adapt request URL path and send it again // "down the chain" r.URL.Path = iw.Header().Get(redirectHeader) @@ -46,6 +49,12 @@ func (i Internal) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) status, err = i.Next.ServeHTTP(iw, r) } + if isInternalRedirect(iw) { + // Too many redirect cycles + iw.ClearHeader() + return http.StatusInternalServerError, nil + } + return status, err }