整改基线:封存可信代理地址解析与远程回归候选
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-18 00:52:10 +08:00
1 parent 05b244532d
commit a4280e7abc
8 files changed
+355 -21

No files matched your search

+1 -1
View File
@@ -83,7 +83,7 @@ func loginRemoteHost(req *http.Request) string {
if req == nil {
return "unknown"
}
host := normalizeRemoteHost(req.RemoteAddr)
host := remoteIP(req)
if host == "" {
host = "unknown"
}
+2 -20
View File
@@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"net"
"net/http"
"net/url"
"strconv"
@@ -23,6 +22,7 @@ import (
"git.pcux.cn/xuans/Smail/services/recovery"
"git.pcux.cn/xuans/Smail/services/verification"
webauthnservice "git.pcux.cn/xuans/Smail/services/webauthn"
"git.pcux.cn/xuans/Smail/utils/clientip"
"git.pcux.cn/xuans/Smail/utils/context"
"git.pcux.cn/xuans/Smail/utils/password"
log "github.com/sirupsen/logrus"
@@ -1076,25 +1076,7 @@ func DisableExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http
}
func remoteIP(req *http.Request) string {
if req == nil {
return ""
}
return normalizeRemoteHost(req.RemoteAddr)
}
func normalizeRemoteHost(value string) string {
value = strings.TrimSpace(value)
if value == "" {
return ""
}
if host, _, err := net.SplitHostPort(value); err == nil {
value = host
}
value = strings.Trim(value, "[]")
if parsed := net.ParseIP(value); parsed != nil {
return parsed.String()
}
return value
return clientip.Resolve(req, config.CurrentHTTPConfig().TrustedProxyCIDRs)
}
func maskEmail(value string) string {
@@ -4,6 +4,8 @@ import (
"net/http"
"net/http/httptest"
"testing"
"git.pcux.cn/xuans/Smail/config"
)
func TestRemoteIPNormalizesIPv4AndIPv6Addresses(t *testing.T) {
@@ -31,6 +33,43 @@ func TestRemoteIPNormalizesIPv4AndIPv6Addresses(t *testing.T) {
}
}
func TestRemoteIPTrustedProxySeparatesLoginLimits(t *testing.T) {
oldConfig, oldManager := config.Instance, config.CurrentManager()
config.SetRuntimeManager(nil)
config.Instance = &config.Config{
TLSMode: config.HTTPSTLSModeDirect,
TrustedProxyCIDRs: []string{"127.0.0.1/32"},
}
t.Cleanup(func() {
config.Instance = oldConfig
config.SetRuntimeManager(oldManager)
})
first := httptest.NewRequest(http.MethodPost, "https://mail.example.com/api/login", nil)
first.RemoteAddr = "127.0.0.1:1234"
first.Header.Set("X-Forwarded-For", "192.0.2.10")
second := first.Clone(first.Context())
second.Header.Set("X-Forwarded-For", "192.0.2.11")
limiter := newLoginLimiter(1)
limiter.recordFailure(limiter.ipKey(first))
if !limiter.blocked(limiter.ipKey(first)) || limiter.blocked(limiter.ipKey(second)) {
t.Fatal("可信代理后的两个客户端共享了 IP 限流状态")
}
if limiter.key(first, "user") == limiter.key(second, "user") {
t.Fatal("账号与 IP 组合限流没有区分客户端")
}
if remoteIP(first) != "192.0.2.10" || loginRemoteHost(first) != remoteIP(first) {
t.Fatal("安全验证与登录限流的客户端地址不一致")
}
second.RemoteAddr = "198.51.100.10:4321"
second.Header.Set("X-Forwarded-For", "192.0.2.10")
if remoteIP(second) != "198.51.100.10" {
t.Fatal("非可信连接能够伪造安全验证来源")
}
if first.RemoteAddr != "127.0.0.1:1234" {
t.Fatal("代理解析修改了真实连接来源")
}
}
func TestRemoteIPPreservesNonIPHostAndHandlesMissingAddress(t *testing.T) {
request := httptest.NewRequest(http.MethodPost, "https://mail.example.com/", nil)
request.RemoteAddr = "proxy.example:443"