Files
nezha_domains/service/singleton/testhelpers.go
T
naiba 39d398066d fix(api): redact third-party credentials in ddns/notification list
GET /api/v1/ddns and /api/v1/notification returned full objects with
plaintext credentials (Cloudflare/TencentCloud secrets, webhook URLs
with embedded bot tokens, Authorization headers). Redact these fields
in the list responses.

Since the frontend edit form repopulates from the list endpoint, the
update handlers now treat an empty submitted credential as "no change"
and preserve the stored value, preventing accidental secret wipes.

Ref: GHSA-ww5p-j6cj-6mqq
2026-06-20 01:54:50 +00:00

68 lines
1.9 KiB
Go

package singleton
import "github.com/nezhahq/nezha/model"
// NewEmptyServerClassForTest 构造一个不依赖 DB 的空 ServerClass,仅用于单测。
// 生产路径请用 NewServerClass。
func NewEmptyServerClassForTest() *ServerClass {
sc := &ServerClass{
class: class[uint64, *model.Server]{
list: make(map[uint64]*model.Server),
},
uuidToID: make(map[string]uint64),
}
model.OwnerServerIDsLookup = sc.ownerServerIDs
model.AllServerIDsLookup = sc.allServerIDs
model.OwnerIsAdminLookup = ownerIsAdmin
return sc
}
// InsertForTest 把一个 server 直接塞进内存表与排序快照,跳过 DB & InitServer 逻辑。
// 调用方需保证 server.ID 已经设置。
func (c *ServerClass) InsertForTest(s *model.Server) {
c.listMu.Lock()
c.list[s.ID] = s
if s.UUID != "" {
c.uuidToID[s.UUID] = s.ID
}
c.listMu.Unlock()
c.sortList()
}
// NewEmptyDDNSClassForTest 构造一个不依赖 DB 的空 DDNSClass,仅用于单测。
func NewEmptyDDNSClassForTest() *DDNSClass {
return &DDNSClass{
class: class[uint64, *model.DDNSProfile]{
list: make(map[uint64]*model.DDNSProfile),
},
}
}
// InsertForTest 把一个 DDNS profile 直接塞进内存表,跳过 DB。
func (c *DDNSClass) InsertForTest(p *model.DDNSProfile) {
c.listMu.Lock()
c.list[p.ID] = p
c.listMu.Unlock()
c.sortList()
}
// NewEmptyNotificationClassForTest 构造空 NotificationClass。
func NewEmptyNotificationClassForTest() *NotificationClass {
return &NotificationClass{
class: class[uint64, *model.Notification]{
list: make(map[uint64]*model.Notification),
},
groupToIDList: make(map[uint64]map[uint64]*model.Notification),
idToGroupList: make(map[uint64]map[uint64]struct{}),
groupList: make(map[uint64]string),
}
}
// InsertForTest 把一个 Notification 直接塞进内存表。
func (c *NotificationClass) InsertForTest(n *model.Notification) {
c.listMu.Lock()
c.list[n.ID] = n
c.listMu.Unlock()
c.sortList()
}