fix(admin): 审计筛选下沉 SQL + 工具数不再写死 + 模型删除加确认
清单里那批小毛病,逐条复核后修(「审计详情列缺失」那条已不成立,早补上了)。
1. 审计筛选只在当前页生效 —— 影响最大的一条。分页是服务端的,筛选却在
前端对已取回的 50 条做,于是搜一个用户 ID 显示"无结果"时,后面几页
可能还有几百条。审计的用途就是查证,"搜不到"会被读成"没发生过"。
改为 action/path/q 三个条件全部落到 SQL,前端只管发条件(防抖 300ms)。
2. 服务状态页把 mcp-go/mcp-py 的工具数写死成 23/4 —— 增删工具后一直骗人,
且服务离线时照样显示,看不出工具其实一个都没注册上。改取实际上报值。
3. 模型删除一点即删,无任何确认。补二次确认,并对"正在使用中"的模型
单独说明后果(删掉会立刻打断线上对话/向量能力)。
审计筛选补了 4 组回归测试,两条是踩出来的坑:
- q 的 OR 组必须带括号:gorm 以 AND 拼接各 Where,裸 OR 会让 action
条件被绕过(测试里用"同 IP 不同方法"两行钉死这个语义);
- LIKE 必须显式写 ESCAPE '\':Postgres 默认拿反斜杠当转义符,SQLite
不写就没有转义符——原来的写法在单测里静默失效,搜 "100%" 命中 0 条。
顺带把 ILIKE 换成 LOWER()+LIKE,这段才能被内存库覆盖。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -30,7 +30,14 @@ func (h *Handler) AuditList(c *gin.Context) {
|
||||
offset = n
|
||||
}
|
||||
}
|
||||
rows, err := h.db.ListAudit(c.Request.Context(), limit, offset)
|
||||
// 筛选下沉到 SQL:此前是前端在当前页 50 条里过滤,翻页外的记录搜不到,
|
||||
// 对审计来说等于给出错误结论。
|
||||
f := store.AuditFilter{
|
||||
Action: c.Query("action"),
|
||||
Path: c.Query("path"),
|
||||
Q: strings.TrimSpace(c.Query("q")),
|
||||
}
|
||||
rows, err := h.db.ListAudit(c.Request.Context(), limit, offset, f)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AppendAudit 追加一条审计留痕(best-effort:审计失败不应影响主流程,调用方忽略返回)。
|
||||
func (p *Postgres) AppendAudit(ctx context.Context, a *AuditLog) error {
|
||||
@@ -10,14 +13,47 @@ func (p *Postgres) AppendAudit(ctx context.Context, a *AuditLog) error {
|
||||
return p.db.WithContext(ctx).Create(a).Error
|
||||
}
|
||||
|
||||
// ListAudit 倒序列出审计留痕(管理端审计流;limit 限流、offset 翻页)。
|
||||
func (p *Postgres) ListAudit(ctx context.Context, limit, offset int) ([]AuditLog, error) {
|
||||
// AuditFilter 是审计流的服务端筛选条件。筛选必须落到 SQL:审计的用途是查证,
|
||||
// 若只在“当前页”里筛,搜不到就等于给出“没有这条记录”的错误结论。
|
||||
type AuditFilter struct {
|
||||
Action string // HTTP 方法,精确匹配
|
||||
Path string // 路径前缀
|
||||
Q string // 跨 actor / ip / detail / path 的模糊匹配
|
||||
}
|
||||
|
||||
// escapeLike 转义 LIKE 的通配符,让用户输入的 % 和 _ 按字面量匹配
|
||||
// (否则搜 "100%" 会退化成匹配任意串)。
|
||||
// 配套的 SQL 必须显式写 ESCAPE '\\':Postgres 默认就拿反斜杠当转义符,但 SQLite 不写
|
||||
// ESCAPE 就压根没有转义符——依赖隐式默认会在换库/单测时静默失效。
|
||||
func escapeLike(s string) string {
|
||||
return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(s)
|
||||
}
|
||||
|
||||
// ListAudit 倒序列出审计留痕(管理端审计流;limit 限流、offset 翻页,筛选走 SQL)。
|
||||
func (p *Postgres) ListAudit(ctx context.Context, limit, offset int, f AuditFilter) ([]AuditLog, error) {
|
||||
if p.db == nil {
|
||||
return nil, errStoreDisabled
|
||||
}
|
||||
limit, offset = clampPage(limit, offset)
|
||||
q := p.db.WithContext(ctx).Model(&AuditLog{})
|
||||
if f.Action != "" {
|
||||
q = q.Where("action = ?", f.Action)
|
||||
}
|
||||
if f.Path != "" {
|
||||
q = q.Where(`path LIKE ? ESCAPE '\'`, escapeLike(f.Path)+"%")
|
||||
}
|
||||
if f.Q != "" {
|
||||
like := "%" + strings.ToLower(escapeLike(f.Q)) + "%"
|
||||
// 用 LOWER()+LIKE 而非 Postgres 专有的 ILIKE:语义一样,但 SQLite 也支持,
|
||||
// 于是这段能被内存库单测覆盖(审计搜索量小,放弃索引可忽略)。
|
||||
// 括号不能省:gorm 把每个 Where 以 AND 拼接,裸的 OR 串会让优先级变成
|
||||
// `action = ? AND actor LIKE ? OR ip LIKE ? ...`,前面的条件直接失效。
|
||||
q = q.Where(`(LOWER(actor) LIKE ? ESCAPE '\' OR LOWER(ip) LIKE ? ESCAPE '\' `+
|
||||
`OR LOWER(detail) LIKE ? ESCAPE '\' OR LOWER(path) LIKE ? ESCAPE '\')`,
|
||||
like, like, like, like)
|
||||
}
|
||||
var out []AuditLog
|
||||
err := p.db.WithContext(ctx).Order("created_at desc").Limit(limit).Offset(offset).Find(&out).Error
|
||||
err := q.Order("created_at desc").Limit(limit).Offset(offset).Find(&out).Error
|
||||
return out, err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 审计筛选必须落到 SQL。此前是前端在“当前页 50 条”里过滤,翻页外的记录搜不到 ——
|
||||
// 审计的用途就是查证,“搜不到”会被当成“没发生过”,所以这里把筛选语义固化成回归测试。
|
||||
|
||||
func seedAudit(t *testing.T, p *Postgres, rows []AuditLog) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
for i := range rows {
|
||||
if rows[i].ID == "" {
|
||||
rows[i].ID = rows[i].Actor + rows[i].Path + rows[i].IP + itoa(i)
|
||||
}
|
||||
if err := p.AppendAudit(ctx, &rows[i]); err != nil {
|
||||
t.Fatalf("写审计失败: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func itoa(i int) string { return string(rune('a' + i)) }
|
||||
|
||||
func TestListAudit_FilterAction(t *testing.T) {
|
||||
p := newTestStore(t)
|
||||
seedAudit(t, p, []AuditLog{
|
||||
{Actor: "u1", Action: "POST", Path: "/api/v1/admin/models", IP: "10.0.0.1"},
|
||||
{Actor: "u2", Action: "DELETE", Path: "/api/v1/admin/models/9", IP: "10.0.0.2"},
|
||||
})
|
||||
|
||||
got, err := ListAuditT(p, AuditFilter{Action: "DELETE"})
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Actor != "u2" {
|
||||
t.Fatalf("按方法筛应只剩 u2 那条,得 %d 条 %+v", len(got), got)
|
||||
}
|
||||
}
|
||||
|
||||
// 最关键的一条:q 的 OR 组必须带括号。少了括号时 SQL 会变成
|
||||
// `action = 'DELETE' AND actor LIKE .. OR ip LIKE .. OR ..`,
|
||||
// AND 优先级高于 OR → 只要 ip/detail/path 命中,action 条件就被绕过。
|
||||
func TestListAudit_ActionAndQueryAreConjunctive(t *testing.T) {
|
||||
p := newTestStore(t)
|
||||
seedAudit(t, p, []AuditLog{
|
||||
{Actor: "alice", Action: "DELETE", Path: "/x", IP: "10.0.0.7"},
|
||||
{Actor: "bob", Action: "GET", Path: "/y", IP: "10.0.0.7"}, // 同 IP 但方法不符
|
||||
})
|
||||
|
||||
got, err := ListAuditT(p, AuditFilter{Action: "DELETE", Q: "10.0.0.7"})
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Actor != "alice" {
|
||||
t.Fatalf("方法与关键词应同时生效(AND),得 %d 条 %+v —— 括号可能丢了", len(got), got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAudit_QueryMatchesAcrossFields(t *testing.T) {
|
||||
p := newTestStore(t)
|
||||
seedAudit(t, p, []AuditLog{
|
||||
{Actor: "u1", Action: "POST", Path: "/tenants", IP: "1.1.1.1", Detail: "移除成员 zhang"},
|
||||
{Actor: "u2", Action: "POST", Path: "/models", IP: "2.2.2.2", Detail: "保存模型"},
|
||||
})
|
||||
|
||||
for _, tc := range []struct{ name, q, want string }{
|
||||
{"命中 detail", "zhang", "u1"},
|
||||
{"命中 ip", "2.2.2.2", "u2"},
|
||||
{"命中 path", "/tenants", "u1"},
|
||||
{"命中 actor", "u2", "u2"},
|
||||
{"大小写不敏感", "ZHANG", "u1"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := ListAuditT(p, AuditFilter{Q: tc.q})
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Actor != tc.want {
|
||||
t.Fatalf("q=%q 应只命中 %s,得 %d 条 %+v", tc.q, tc.want, len(got), got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 用户输入里的 % / _ 必须按字面量匹配,否则搜 "100%" 会退化成匹配任意串。
|
||||
func TestListAudit_QueryEscapesWildcards(t *testing.T) {
|
||||
p := newTestStore(t)
|
||||
seedAudit(t, p, []AuditLog{
|
||||
{Actor: "u1", Action: "POST", Path: "/a", IP: "1.1.1.1", Detail: "折扣 100% 生效"},
|
||||
{Actor: "u2", Action: "POST", Path: "/b", IP: "2.2.2.2", Detail: "无关记录"},
|
||||
})
|
||||
|
||||
got, err := ListAuditT(p, AuditFilter{Q: "100%"})
|
||||
if err != nil {
|
||||
t.Fatalf("查询失败: %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Actor != "u1" {
|
||||
t.Fatalf("%% 应按字面量匹配,得 %d 条 %+v", len(got), got)
|
||||
}
|
||||
}
|
||||
|
||||
// ListAuditT 是测试用的薄封装,省去每处都写 ctx/limit/offset。
|
||||
func ListAuditT(p *Postgres, f AuditFilter) ([]AuditLog, error) {
|
||||
return p.ListAudit(context.Background(), 50, 0, f)
|
||||
}
|
||||
@@ -28,6 +28,7 @@ func newTestStore(t *testing.T) *Postgres {
|
||||
if err := db.AutoMigrate(
|
||||
&User{}, &Tenant{}, &TenantMember{}, &CreditLedger{}, &PaymentOrder{},
|
||||
&RedeemCode{}, &CreditPack{}, &UsageEvent{}, &UsageRollup{}, &Setting{}, &Pricing{}, &LLMModel{},
|
||||
&AuditLog{},
|
||||
&KB{}, // 租户作用域模型,验证隔离插件
|
||||
); err != nil {
|
||||
t.Fatalf("AutoMigrate 失败: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user