fix(network): handle port mismatch in CSRF Origin check caused by nginx Host configuration

This commit is contained in:
duorameng
2026-05-19 15:31:11 +08:00
parent cded935c4d
commit a807522eed
+11
View File
@@ -1,6 +1,7 @@
package utils
import (
"net"
"net/http"
"net/url"
"os"
@@ -33,6 +34,16 @@ func CheckWSOrigin(r *http.Request) bool {
return true
}
// 容错处理:如果因为 Nginx 配置了 $host 而丢掉了端口,导致一方带端口一方不带端口时,尝试忽略端口进行域名比对
uHostOnly := u.Hostname()
rHostOnly := r.Host
if h, _, err := net.SplitHostPort(r.Host); err == nil {
rHostOnly = h
}
if strings.EqualFold(uHostOnly, rHostOnly) {
return true
}
// 2. 环境变量配置的允许列表校验
allowedOrigins := os.Getenv("BH_ALLOWED_ORIGINS")
if allowedOrigins != "" {