fix(security): 告警渠道 webhook 与 Bark Device Key 响应脱敏
AlertNotifyChannel.Config 原先以真值返回。钉钉/飞书/企微的 webhook URL 与 Bark 的 device key 本身就是凭据,泄露即可直接向对应群或设备推送消息,而列表接口对 具备 recharge:manage 的商户成员可见。 - 新增 service.MaskAlertChannel(s):在 HTTP 响应层脱敏,只保留密钥末 4 位。 webhook 保留域名与路径以便辨认是哪一台机器人(钉钉/企微的 token 在查询串, 飞书的 uuid 在路径末段),Server 属主机地址不遮蔽 - UpdateAlertChannel 识别回传的脱敏占位值并沿用库中真实密钥,使「只改名称/开关」 不会被掩码覆盖;填写新值则正常替换 - 脱敏刻意不放在 service 层:CheckLowBalanceAndNotify 复用 ListAlertChannels 的结果真正去发消息,在那一层脱敏会直接打断告警发送 - 前端表单补充说明文案 验证:新增 6 组测试,覆盖脱敏规则、三种机器人 URL 形态、脱敏结果仍能通过校验、 掩码回传不覆盖真实密钥,以及「service 层必须返回真值」这条关键边界。
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -261,6 +262,97 @@ func ValidateAlertChannelInput(in AlertChannelInput) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 告警渠道密钥脱敏。
|
||||
//
|
||||
// 钉钉/飞书/企微的 webhook URL 与 Bark 的 device key 本身就是凭据,泄露即可直接
|
||||
// 向对应群或设备推送消息。而列表接口会把渠道配置返回给具备 recharge:manage 的
|
||||
// 商户成员,因此响应里只保留尾部 4 位用于辨认。
|
||||
//
|
||||
// 注意:脱敏只发生在 HTTP 响应层。ListAlertChannels 等 service 方法仍返回真实值,
|
||||
// 因为余额巡检要拿它们真正去发消息——在 service 层脱敏会直接打断告警发送。
|
||||
const alertSecretMask = "********"
|
||||
|
||||
// MaskAlertSecret 保留尾部 4 位,其余用掩码覆盖;空值原样返回。
|
||||
func MaskAlertSecret(secret string) string {
|
||||
secret = strings.TrimSpace(secret)
|
||||
if secret == "" {
|
||||
return ""
|
||||
}
|
||||
if len(secret) <= 4 {
|
||||
return alertSecretMask
|
||||
}
|
||||
return alertSecretMask + secret[len(secret)-4:]
|
||||
}
|
||||
|
||||
// IsMaskedAlertSecret 判断前端回传的值是否仍是脱敏占位值。
|
||||
// 用 Contains 而不是 HasPrefix:webhook 的掩码嵌在 URL 中间
|
||||
// (https://oapi.dingtalk.com/robot/send?access_token=********1234),只有 Bark 的
|
||||
// Key 才是整体掩码。真实密钥里出现连续 8 个星号实际上不可能。
|
||||
func IsMaskedAlertSecret(value string) bool {
|
||||
return strings.Contains(value, alertSecretMask)
|
||||
}
|
||||
|
||||
// maskAlertWebhookURL 只遮蔽 URL 中的密钥片段,保留域名与路径,让编辑表单仍能看出
|
||||
// 这是哪一个机器人;整体替换为占位值会让用户失去上下文。
|
||||
//
|
||||
// 全程在原始字符串上做替换,不走 url.URL.String():后者会把路径里的 * 转义成 %2A,
|
||||
// 让掩码变成 https://.../hook/%2A%2A%2A%2A%2A%2A%2A%2A5555 这种没法看的形式。
|
||||
func maskAlertWebhookURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Host == "" {
|
||||
return MaskAlertSecret(raw)
|
||||
}
|
||||
|
||||
masked := raw
|
||||
|
||||
// 查询串里逐个 value 遮蔽(钉钉 access_token、企业微信 key)。
|
||||
if idx := strings.Index(masked, "?"); idx >= 0 {
|
||||
head, query := masked[:idx+1], masked[idx+1:]
|
||||
parts := strings.Split(query, "&")
|
||||
for i, part := range parts {
|
||||
if kv := strings.SplitN(part, "=", 2); len(kv) == 2 {
|
||||
parts[i] = kv[0] + "=" + MaskAlertSecret(kv[1])
|
||||
}
|
||||
}
|
||||
masked = head + strings.Join(parts, "&")
|
||||
} else {
|
||||
// 无查询串时遮蔽路径末段(飞书 /open-apis/bot/v2/hook/<uuid>)。
|
||||
// 只在末段足够长时遮蔽,避免误伤 /robot/send 这类固定路径。
|
||||
segments := strings.Split(strings.TrimRight(parsed.Path, "/"), "/")
|
||||
if last := len(segments) - 1; last >= 0 && len(segments[last]) >= 16 {
|
||||
segment := segments[last]
|
||||
if at := strings.LastIndex(masked, segment); at >= 0 {
|
||||
masked = masked[:at] + MaskAlertSecret(segment) + masked[at+len(segment):]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return masked
|
||||
}
|
||||
|
||||
// MaskAlertChannel 返回用于接口响应的脱敏副本。Server 是主机地址而非密钥,保持原样。
|
||||
func MaskAlertChannel(channel model.AlertNotifyChannel) model.AlertNotifyChannel {
|
||||
channel.Config.WebhookURL = maskAlertWebhookURL(channel.Config.WebhookURL)
|
||||
channel.Config.Key = MaskAlertSecret(channel.Config.Key)
|
||||
return channel
|
||||
}
|
||||
|
||||
// MaskAlertChannels 批量脱敏;nil 保持 nil,便于调用方直接替换响应体。
|
||||
func MaskAlertChannels(channels []model.AlertNotifyChannel) []model.AlertNotifyChannel {
|
||||
if channels == nil {
|
||||
return nil
|
||||
}
|
||||
masked := make([]model.AlertNotifyChannel, 0, len(channels))
|
||||
for _, channel := range channels {
|
||||
masked = append(masked, MaskAlertChannel(channel))
|
||||
}
|
||||
return masked
|
||||
}
|
||||
|
||||
func (s *RechargeService) ListAlertChannels(merchantID uint) ([]model.AlertNotifyChannel, error) {
|
||||
var list []model.AlertNotifyChannel
|
||||
err := s.db.Where("merchant_id = ?", merchantID).Order("id ASC").Find(&list).Error
|
||||
@@ -288,9 +380,6 @@ func (s *RechargeService) CreateAlertChannel(merchantID uint, in AlertChannelInp
|
||||
}
|
||||
|
||||
func (s *RechargeService) UpdateAlertChannel(merchantID, channelID uint, in AlertChannelInput) (*model.AlertNotifyChannel, error) {
|
||||
if err := ValidateAlertChannelInput(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(strings.TrimSpace(in.Name)) > 64 {
|
||||
return nil, errors.New("渠道名称最长 64 个字符")
|
||||
}
|
||||
@@ -298,6 +387,17 @@ func (s *RechargeService) UpdateAlertChannel(merchantID, channelID uint, in Aler
|
||||
if err := s.db.Where("id = ? AND merchant_id = ?", channelID, merchantID).First(&channel).Error; err != nil {
|
||||
return nil, errors.New("通知渠道不存在")
|
||||
}
|
||||
// 前端拿到的是脱敏值。原样回传说明用户只改了名称或开关,沿用库中真实密钥;
|
||||
// 这一步必须在校验之前,否则占位值会让「只改名称」的提交直接校验失败。
|
||||
if IsMaskedAlertSecret(in.Config.WebhookURL) {
|
||||
in.Config.WebhookURL = channel.Config.WebhookURL
|
||||
}
|
||||
if IsMaskedAlertSecret(in.Config.Key) {
|
||||
in.Config.Key = channel.Config.Key
|
||||
}
|
||||
if err := ValidateAlertChannelInput(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channel.ChannelType = strings.TrimSpace(in.ChannelType)
|
||||
channel.Name = strings.TrimSpace(in.Name)
|
||||
channel.Config = in.Config
|
||||
|
||||
Reference in New Issue
Block a user