Files
sundynix-agentix/sundynix-gateway/internal/handler/completeness_a_test.go
T
Blizzard ac38d5e663 fix(prod): 后端生产级 A 类硬伤全清(7 项:授权/崩溃点/全表扫/限流/鉴权/限额)
部署前生产级审计(可靠性/数据层/安全三路)后,清掉 7 处代码级硬伤:

A1 后台定时器 goroutine 无 panic recover → 单个 DB panic 崩整个 gateway。加 safeGo/
   safeCall,包住订阅/掉单补偿/微信推送/探针 goroutine,单轮 tick 再兜一层。
A2 提示词控制面(建/激活/停用,热广播全服务)只 RequireAuth → 任意登录用户改全局提示词。
   三写端点+列表挂 RequireAdmin。
A3 HITL 审批端点无角色门 → viewer 可放行烧钱执行。加 RequireTenantRole(member)。
A4 审计/护栏列表 limit 无校验,limit=-1 让 gorm 取消 LIMIT 全表扫。加 clampLimit/
   clampOffset,AdminTasks/AdminSpaces 补上界。
A5 限流 Redis 一挂就完全放行(fail-open)。加进程内固定窗口兜底(fail-safe) + 登录/注册
   按 IP 专用严限流(10/min)。
A6 公开 by-id 端点(stream/exec/report导出/kb导入流)无鉴权无租户过滤。加
   AuthFromHeaderOrQuery(从 ?token= 取 JWT) + task/report 按 owner 归属校验;桌面端
   5 处 EventSource/下载 URL 经 tokenQuery 附 JWT。
A7 文件上传无大小上限(整文件进内存 OOM 面) → 50MB 闸(KB_MAX_UPLOAD_BYTES)+ LimitReader;
   http.Server 加 ReadHeaderTimeout/ReadTimeout/MaxHeaderBytes(不设 WriteTimeout 保 SSE)。

带单测:clampLimit/safeCall/procLimiter/AuthFromHeaderOrQuery/TaskOwner。
build+vet+全量 test 绿;desktop tsc 绿。B(迁移工具/实时探针/出网韧性/登录锁定/leader选举)
与 C(TLS/PG HA/K8s/备份自动化/可观测)分期后做,参照 production_readiness.md。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 15:04:47 +08:00

45 lines
1.3 KiB
Go
Raw 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 handler
import "testing"
// A4limit 必须夹紧——负数(limit=-1 会让 gorm 取消 LIMIT 全表扫)、0、超大都要归位。
func TestClampLimit(t *testing.T) {
cases := []struct {
v string
def, max int
want int
}{
{"", 50, 200, 50}, // 空 → 默认
{"-1", 50, 200, 50}, // 负数 → 默认(关键:堵住全表扫)
{"0", 50, 200, 50}, // 0 → 默认
{"abc", 50, 200, 50}, // 非法 → 默认
{"100", 50, 200, 100}, // 合法 → 原值
{"999", 50, 200, 200}, // 超上界 → 夹到 max
}
for _, tc := range cases {
if got := clampLimit(tc.v, tc.def, tc.max); got != tc.want {
t.Fatalf("clampLimit(%q,%d,%d)=%d want %d", tc.v, tc.def, tc.max, got, tc.want)
}
}
if clampOffset("-5") != 0 || clampOffset("abc") != 0 || clampOffset("7") != 7 {
t.Fatal("clampOffset 应把负数/非法归 0、合法透传")
}
}
// A1safeCall 必须兜住 panic,不外抛(否则后台 goroutine 一 panic 崩整个进程)。
func TestSafeCallRecovers(t *testing.T) {
done := false
func() {
defer func() {
if r := recover(); r != nil {
t.Fatalf("safeCall 未兜住 panic%v", r)
}
}()
safeCall("test", func() { panic("boom") })
done = true
}()
if !done {
t.Fatal("safeCall 之后应正常继续")
}
}