fix(network): handle port mismatch in CSRF Origin check caused by nginx Host configuration
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -33,6 +34,16 @@ func CheckWSOrigin(r *http.Request) bool {
|
|||||||
return true
|
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. 环境变量配置的允许列表校验
|
// 2. 环境变量配置的允许列表校验
|
||||||
allowedOrigins := os.Getenv("BH_ALLOWED_ORIGINS")
|
allowedOrigins := os.Getenv("BH_ALLOWED_ORIGINS")
|
||||||
if allowedOrigins != "" {
|
if allowedOrigins != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user