Pārlūkot izejas kodu

fix domain validator

Co-Authored-By: Alireza Ahmadi <[email protected]>
mhsanaei 4 mēneši atpakaļ
vecāks
revīzija
d298f4ffbd
1 mainītis faili ar 11 papildinājumiem un 5 dzēšanām
  1. 11 5
      web/middleware/domainValidator.go

+ 11 - 5
web/middleware/domainValidator.go

@@ -3,6 +3,7 @@ package middleware
 import (
 	"net"
 	"net/http"
+	"strings"
 
 	"github.com/gin-gonic/gin"
 )
@@ -14,12 +15,17 @@ func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
 			host = c.GetHeader("X-Real-IP")
 		}
 		if host == "" {
-			host, _, _ := net.SplitHostPort(c.Request.Host)
-			if host != domain {
-				c.AbortWithStatus(http.StatusForbidden)
-				return
+			host = c.Request.Host
+			if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
+				host, _, _ = net.SplitHostPort(host)
 			}
-		c.Next()
 		}
+
+		if host != domain {
+			c.AbortWithStatus(http.StatusForbidden)
+			return
+		}
+
+		c.Next()
 	}
 }