Files
Logan.Li 1c50d565eb
CI / backend (push) Canceled after 0s
fix(security): webhook fail-closed、机密字段信封加密、限流信任代理修正
三项 P0 安全修复:

1. Webhook 验签 fail-closed
   - WaffoWebhook 未配置验签材料时从「告警放行」改为 503 拒绝,
     封死伪造 order.completed 结算订单的路径
   - 通用 PaymentWebhook 从「仅 release 拒绝」收紧为默认拒绝,
     仅显式 ALLOW_UNVERIFIED_WEBHOOK=1(本地调试)放行

2. 机密字段信封加密(新增 internal/secretbox)
   - AES-256-GCM,主密钥 OPSLIC_MASTER_KEY(64 位 hex),密文带
     enc:v1: 前缀;Encrypt 幂等、历史明文原样放行
   - PaymentGatewayConfig.SecretKey/WebhookSecret 与 PeerNode.
     SharedKey 挂 GORM 钩子透明加解密,业务代码零改动;SharedKey
     列宽 128→512(密文更长)
   - 启动 fail-fast 校验主密钥;MigratePlaintext 幂等迁移存量明文;
     节点同步只含业务表,机密表为节点本地,互不影响
   - docker-compose/.env.example 补 OPSLIC_MASTER_KEY 与说明

3. 限流信任代理修正
   - 默认 SetTrustedProxies(nil) 不信任任何 X-Forwarded-For,
     封死伪造 XFF 轮换 IP 绕过按 IP 限流;反代节点用
     TRUSTED_PROXIES 指定内网 CIDR,非法配置拒绝启动

验证:go build/vet/test 全绿(含 secretbox 6 条单测);
读取路径(Waffo 私钥/webhook 密钥/互联密钥)确认全走 GORM 钩子。

部署注意:上线须配置 OPSLIC_MASTER_KEY(双节点一致)与
WAFFO_WEBHOOK_PUBLIC_KEY(未配则回调 503,属预期);香港反代
节点配 TRUSTED_PROXIES。
2026-11-01 02:03:26 +08:00

57 lines
1.8 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package model
import (
"fmt"
"time"
"backend/internal/secretbox"
"gorm.io/gorm"
)
// Peer 节点状态:pending 已配置未握手,active 握手成功可同步,disabled 手动停用 // Logan.Li
const (
PeerStatusPending = "pending"
PeerStatusActive = "active"
PeerStatusDisabled = "disabled"
)
// PeerNode 互联节点:双向授权同步的对端(地址 + 互联密钥 + 游标) // Logan.Li
type PeerNode struct {
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"size:100;not null"`
BaseURL string `json:"base_url" gorm:"size:255;not null"` // 对端 https 地址(含 /api)
SharedKey string `json:"-" gorm:"size:512;not null"` // 互联密钥,双向一致,HMAC 签名用;信封加密存储(密文比明文长)
Status string `json:"status" gorm:"size:20;default:'pending'"`
RemoteNode string `json:"remote_node" gorm:"size:64"` // 对端节点 ID,握手后回填
LastSyncAt *time.Time `json:"last_sync_at,omitempty"` // 本地对端同步游标(增量起点)
LastResult string `json:"last_result" gorm:"size:255"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (PeerNode) TableName() string {
return "peer_nodes"
}
// BeforeSave 互联密钥信封加密后落库(幂等) // Logan.Li
func (p *PeerNode) BeforeSave(tx *gorm.DB) error {
enc, err := secretbox.Encrypt(p.SharedKey)
if err != nil {
return fmt.Errorf("加密 shared_key: %w", err)
}
p.SharedKey = enc
return nil
}
// AfterFind 读取时解密;历史明文原样返回 // Logan.Li
func (p *PeerNode) AfterFind(tx *gorm.DB) error {
dec, err := secretbox.Decrypt(p.SharedKey)
if err != nil {
return fmt.Errorf("解密 shared_key: %w", err)
}
p.SharedKey = dec
return nil
}