0.10.1: 安全与业务逻辑加固、新品牌与部署加固
三轮审查修复(60+ 项),相对远端 main(b536672)的关键变更:
- 安全: 数据面 SSRF 拨号防护(防 DNS rebinding)/上游凭据剥离/登录防枚举
与锁定态统一/可信代理(X-Forwarded-For)限流加固/会话版本失效机制/
撤销即时传播/弱密钥拒绝启动/脱敏字节级重写(保签名契约)
- 业务逻辑: 裸 body 上传 panic/bootstrap 审计管线卡死/定价通配符优先级/
全局工具可见性/调度器停机补跑/TOTP 挑战令牌消费顺序/熔断探针语义/
>4MB 响应 token 计量/管理员重置密码作废会话 等
- 前端: 新 logo(语枢 AI 网关主题)/Provider 凭据异常警示/删除入口/
后端错误消息透传/localStorage 敏感数据收敛
- 部署: CREDENTIAL_MASTER_KEY 持久化与弱值拒绝/Provider DELETE 接口/
nginx 安全头/worker 内存限制
- 新增迁移 000029(key_hash 索引)/000030(usage_daily 币种维度)
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -24,13 +25,55 @@ import (
|
||||
// If Redis is unavailable or not configured the limiter fails OPEN (login
|
||||
// proceeds, account lockout still applies) rather than locking every user out.
|
||||
type LoginLimiter struct {
|
||||
client *redis.Client
|
||||
max int
|
||||
window time.Duration
|
||||
client *redis.Client
|
||||
max int
|
||||
window time.Duration
|
||||
trusted []netip.Prefix
|
||||
}
|
||||
|
||||
func NewLoginLimiter(client *redis.Client, max int, window time.Duration) *LoginLimiter {
|
||||
return &LoginLimiter{client: client, max: max, window: window}
|
||||
func NewLoginLimiter(client *redis.Client, max int, window time.Duration, trustedProxies []netip.Prefix) *LoginLimiter {
|
||||
return &LoginLimiter{client: client, max: max, window: window, trusted: trustedProxies}
|
||||
}
|
||||
|
||||
// ClientIP 提取用于登录限流的客户端 IP。仅当直连对端(RemoteAddr)属于可信
|
||||
// 代理网段时才采信 X-Forwarded-For;否则任何公网客户端都可以伪造该头,把
|
||||
// 每 IP 滑动窗口的键旋转掉,彻底绕过登录限流。
|
||||
func (l *LoginLimiter) ClientIP(r *http.Request) string {
|
||||
if l != nil && len(l.trusted) > 0 {
|
||||
peer, err := netip.ParseAddr(peerHost(r.RemoteAddr))
|
||||
if err == nil {
|
||||
peer = peer.Unmap()
|
||||
trustedPeer := false
|
||||
for _, prefix := range l.trusted {
|
||||
if prefix.Contains(peer) {
|
||||
trustedPeer = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if trustedPeer {
|
||||
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
|
||||
if first := strings.TrimSpace(strings.Split(fwd, ",")[0]); first != "" {
|
||||
return first
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return peerHost(r.RemoteAddr)
|
||||
}
|
||||
|
||||
func peerHost(remoteAddr string) string {
|
||||
host, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
host = remoteAddr
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
// ClientIP 兼容旧签名:未配置可信代理时退化为"仅信任内网对端"。
|
||||
// 保留给直接调用方;HTTP 处理路径统一走 LoginLimiter.ClientIP。
|
||||
func ClientIP(r *http.Request) string {
|
||||
return NewLoginLimiter(nil, 0, 0, nil).ClientIP(r)
|
||||
}
|
||||
|
||||
// Allow reports whether a login attempt from ip may proceed.
|
||||
@@ -85,20 +128,3 @@ redis.call('ZADD', key, now, ARGV[4])
|
||||
redis.call('EXPIRE', key, ARGV[5])
|
||||
return {0, count + 1}
|
||||
`)
|
||||
|
||||
// ClientIP extracts the caller's IP for login rate limiting. X-Forwarded-For
|
||||
// is trusted here because nginx is the only ingress and overwrites the header
|
||||
// on every proxy hop; the first value is the client address. Falls back to
|
||||
// RemoteAddr for direct connections.
|
||||
func ClientIP(r *http.Request) string {
|
||||
if fwd := r.Header.Get("X-Forwarded-For"); fwd != "" {
|
||||
if first := strings.TrimSpace(strings.Split(fwd, ",")[0]); first != "" {
|
||||
return first
|
||||
}
|
||||
}
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
host = r.RemoteAddr
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user