From 0dab17ea851827fcee44e3ff1ea4767689148a63 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Wed, 30 Aug 2023 11:34:53 +0100 Subject: [PATCH] Add internal loop detection --- servers/https.go | 5 +++++ target/route.go | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/servers/https.go b/servers/https.go index 269906e..7a9af86 100644 --- a/servers/https.go +++ b/servers/https.go @@ -73,6 +73,11 @@ func setupRateLimiter(rateLimit uint64, next http.Handler) http.Handler { func setupFaviconMiddleware(fav *favicons.Favicons, next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + if req.Header.Get("X-Violet-Loop-Detect") == "1" { + rw.WriteHeader(http.StatusLoopDetected) + _, _ = rw.Write([]byte("Detected a routing loop\n")) + return + } if req.Header.Get("X-Violet-Raw-Favicon") != "1" { switch req.URL.Path { case "/favicon.svg", "/favicon.png", "/favicon.ico": diff --git a/target/route.go b/target/route.go index c887cf3..ba557ef 100644 --- a/target/route.go +++ b/target/route.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/1f349/violet/proxy" "github.com/1f349/violet/utils" + "github.com/google/uuid" websocket2 "github.com/gorilla/websocket" "github.com/rs/cors" "golang.org/x/net/http/httpguts" @@ -155,6 +156,8 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) { return } + req2.Header.Set("X-Violet-Loop-Detect", "1") + // serve request with reverse proxy var resp *http.Response if r.HasFlag(FlagIgnoreCert) { @@ -167,6 +170,12 @@ func (r Route) internalServeHTTP(rw http.ResponseWriter, req *http.Request) { utils.RespondVioletError(rw, http.StatusBadGateway, "error receiving internal round trip response") return } + if resp.StatusCode == http.StatusLoopDetected { + u := uuid.New() + log.Printf("[ServeRoute::ServeHTTP()] Loop Detected: %s %s '%s' -> '%s'\n", u, req.Method, req.URL.String(), req2.URL.String()) + utils.RespondVioletError(rw, http.StatusLoopDetected, "error loop detected: "+u.String()) + return + } // copy headers and status code copyHeader(rw.Header(), resp.Header)