Skip to content

Commit

Permalink
fix domain validator
Browse files Browse the repository at this point in the history
Co-Authored-By: Alireza Ahmadi <alireza7@gmail.com>
  • Loading branch information
MHSanaei and alireza0 committed Jul 14, 2024
1 parent 315e8af commit d298f4f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions web/middleware/domainValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"net"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)
Expand All @@ -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()
}
}

0 comments on commit d298f4f

Please sign in to comment.