Add internal loop detection

This commit is contained in:
Melon 2023-08-30 11:34:53 +01:00
parent a16617b131
commit 0dab17ea85
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 14 additions and 0 deletions

View File

@ -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":

View File

@ -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)