三项 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。
This commit is contained in:
1 parent
e0cef06234
commit
1c50d565eb
10 files changed
+360
-11
No files matched your search
@@ -0,0 +1,74 @@
|
||||
package secretbox
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func withTestKey(t *testing.T) {
|
||||
t.Helper()
|
||||
t.Setenv("OPSLIC_MASTER_KEY", strings.Repeat("ab", 32))
|
||||
if err := LoadMasterKey(); err != nil {
|
||||
t.Fatalf("LoadMasterKey: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundtrip(t *testing.T) {
|
||||
withTestKey(t)
|
||||
for _, plain := range []string{"waffo-secret-123", "-----BEGIN RSA PRIVATE KEY-----\nabc\n-----END RSA PRIVATE KEY-----", "x", ""} {
|
||||
enc, err := Encrypt(plain)
|
||||
if err != nil {
|
||||
t.Fatalf("Encrypt(%q): %v", plain, err)
|
||||
}
|
||||
if plain != "" && !strings.HasPrefix(enc, Prefix) {
|
||||
t.Fatalf("密文缺少前缀: %q", enc)
|
||||
}
|
||||
got, err := Decrypt(enc)
|
||||
if err != nil {
|
||||
t.Fatalf("Decrypt: %v", err)
|
||||
}
|
||||
if got != plain {
|
||||
t.Fatalf("roundtrip 不一致: got %q want %q", got, plain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptIdempotent(t *testing.T) {
|
||||
withTestKey(t)
|
||||
once, _ := Encrypt("same-value")
|
||||
twice, _ := Encrypt(once)
|
||||
if once != twice {
|
||||
t.Fatal("对已加密值再次 Encrypt 必须原样返回(幂等)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecryptPlaintextPassthrough(t *testing.T) {
|
||||
withTestKey(t)
|
||||
got, err := Decrypt("legacy-plaintext")
|
||||
if err != nil || got != "legacy-plaintext" {
|
||||
t.Fatalf("历史明文应原样返回: got %q err %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTamperDetection(t *testing.T) {
|
||||
withTestKey(t)
|
||||
enc, _ := Encrypt("secret")
|
||||
tampered := enc[:len(enc)-2] + "A="
|
||||
if _, err := Decrypt(tampered); err == nil {
|
||||
t.Fatal("篡改后的密文必须解密失败")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadMasterKeyFailFast(t *testing.T) {
|
||||
t.Setenv("OPSLIC_MASTER_KEY", "")
|
||||
if err := LoadMasterKey(); err == nil {
|
||||
t.Fatal("缺失主密钥必须报错")
|
||||
}
|
||||
t.Setenv("OPSLIC_MASTER_KEY", "not-hex")
|
||||
if err := LoadMasterKey(); err == nil {
|
||||
t.Fatal("非法主密钥必须报错")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) { os.Exit(m.Run()) }
|
||||
Reference in new issue
Block a user