Files
mirror-3x-ui/internal/web/middleware/domainValidator.go
T
Aleksey Fedorov 1e4282bd23 fix(domain): compare panel and subscription domains case-insensitively (#7181)
DomainValidatorMiddleware compared the saved domain with the request
host byte for byte. Host names are case-insensitive and may end with the
root's dot, so a domain saved as Panel.Example.com, with a stray space,
or a Host header like panel.example.com. answered every request with
403, the settings page included, and the admin could not undo it from
the UI.

Both sides are now normalized before the comparison. Saving the settings
stores the domain in that form and refuses a scheme, port or path, which
could never equal the port-stripped host and locked the panel the same
way.
2026-11-23 23:34:53 +03:30

38 lines
1.1 KiB
Go

// Package middleware provides HTTP middleware functions for the 3x-ui web panel,
// including domain validation utilities.
package middleware
import (
"net"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// DomainValidatorMiddleware returns a Gin middleware that validates the request domain.
// It extracts the host from the request, strips any port number, and compares it
// against the configured domain. Requests from unauthorized domains are rejected
// with HTTP 403 Forbidden status.
func DomainValidatorMiddleware(domain string) gin.HandlerFunc {
domain = normalizeHost(domain)
return func(c *gin.Context) {
host := c.Request.Host
if colonIndex := strings.LastIndex(host, ":"); colonIndex != -1 {
host, _, _ = net.SplitHostPort(c.Request.Host)
}
if normalizeHost(host) != domain {
c.AbortWithStatus(http.StatusForbidden)
return
}
c.Next()
}
}
// Host names are case-insensitive and may end with the root's dot (RFC 4343, 1034).
func normalizeHost(host string) string {
return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(host)), ".")
}