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:
@@ -43,25 +43,48 @@ func newCircuitBreaker(policy ...ResiliencePolicy) *circuitBreaker {
|
||||
return &circuitBreaker{threshold: settings.CircuitThreshold, openFor: settings.CircuitOpenDuration}
|
||||
}
|
||||
|
||||
func (c *circuitBreaker) allow(now time.Time) bool {
|
||||
// allow returns whether the request may proceed and whether it is the
|
||||
// half-open probe (the single request allowed through an open circuit to
|
||||
// test recovery).
|
||||
func (c *circuitBreaker) allow(now time.Time) (allowed bool, probe bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.openUntil.IsZero() {
|
||||
return true
|
||||
return true, false
|
||||
}
|
||||
if now.Before(c.openUntil) || c.halfOpenRun {
|
||||
return false
|
||||
return false, false
|
||||
}
|
||||
c.halfOpenRun = true
|
||||
return true
|
||||
return true, true
|
||||
}
|
||||
|
||||
func (c *circuitBreaker) success() {
|
||||
func (c *circuitBreaker) success(probe bool) {
|
||||
c.mu.Lock()
|
||||
c.failures = 0
|
||||
c.openUntil = time.Time{}
|
||||
defer c.mu.Unlock()
|
||||
if c.openUntil.IsZero() {
|
||||
// 关闭状态下普通成功:仅清零失败计数。
|
||||
c.failures = 0
|
||||
return
|
||||
}
|
||||
if probe && c.halfOpenRun {
|
||||
// 半开探针成功:关闭电路,恢复正常流量。
|
||||
c.failures = 0
|
||||
c.openUntil = time.Time{}
|
||||
c.halfOpenRun = false
|
||||
return
|
||||
}
|
||||
// 电路已打开而请求在打开前就通过 allow():陈旧成功不得关闭电路,
|
||||
// 否则刚触发熔断的上游被一个在途成功立即放行。
|
||||
}
|
||||
|
||||
// abortProbe 在探针请求被客户端取消(而非上游失败)时调用:
|
||||
// 既无成功也无失败的证据,释放探针名额但不改变电路状态,让下一次
|
||||
// allow() 重新发起探针。
|
||||
func (c *circuitBreaker) abortProbe() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.halfOpenRun = false
|
||||
c.mu.Unlock()
|
||||
}
|
||||
|
||||
func (c *circuitBreaker) failure(now time.Time) {
|
||||
@@ -82,7 +105,8 @@ type resilientTransport struct {
|
||||
}
|
||||
|
||||
func (t *resilientTransport) RoundTrip(request *http.Request) (*http.Response, error) {
|
||||
if !t.circuit.allow(time.Now()) {
|
||||
allowed, probe := t.circuit.allow(time.Now())
|
||||
if !allowed {
|
||||
return nil, ErrCircuitOpen
|
||||
}
|
||||
replayable := request.Method == http.MethodGet || request.Method == http.MethodHead ||
|
||||
@@ -102,6 +126,11 @@ func (t *resilientTransport) RoundTrip(request *http.Request) (*http.Response, e
|
||||
}
|
||||
current = request.Clone(request.Context())
|
||||
if request.Body != nil && request.Body != http.NoBody {
|
||||
if request.GetBody == nil {
|
||||
// 请求体不可重放(如 GET/HEAD 携带未设置 GetBody 的 body):
|
||||
// 放弃重试,避免把已消费的空 body 重发或调用 nil 方法。
|
||||
break
|
||||
}
|
||||
body, bodyErr := request.GetBody()
|
||||
if bodyErr != nil {
|
||||
err = bodyErr
|
||||
@@ -119,14 +148,24 @@ func (t *resilientTransport) RoundTrip(request *http.Request) (*http.Response, e
|
||||
_ = response.Body.Close()
|
||||
}
|
||||
}
|
||||
if retryableResult(response, err) {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
// 客户端取消/超时不是上游故障证据:不计成败。若本请求是半开
|
||||
// 探针,释放探针名额让电路保持打开,由下一次请求重新探测。
|
||||
if probe {
|
||||
t.circuit.abortProbe()
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
if failureResult(response, err) {
|
||||
t.circuit.failure(time.Now())
|
||||
} else {
|
||||
t.circuit.success()
|
||||
t.circuit.success(probe)
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
// retryableResult 决定是否值得重试:仅传输错误与 502/503/504 会重试,
|
||||
// 500 等其余 5xx 不做自动重试(响应可能已被上游处理,重试有副作用)。
|
||||
func retryableResult(response *http.Response, err error) bool {
|
||||
if err != nil {
|
||||
return !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded)
|
||||
@@ -134,6 +173,15 @@ func retryableResult(response *http.Response, err error) bool {
|
||||
return response != nil && (response.StatusCode == http.StatusBadGateway || response.StatusCode == http.StatusServiceUnavailable || response.StatusCode == http.StatusGatewayTimeout)
|
||||
}
|
||||
|
||||
// failureResult 决定是否计入熔断失败:所有 5xx 都视为上游故障。否则持续返回
|
||||
// 500 的上游永远不会触发熔断,而 success() 还会不断清零失败计数,熔断保护失效。
|
||||
func failureResult(response *http.Response, err error) bool {
|
||||
if err != nil {
|
||||
return !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded)
|
||||
}
|
||||
return response != nil && response.StatusCode >= http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func waitBackoff(ctx context.Context, duration time.Duration) error {
|
||||
if duration <= 0 {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user