fix(kb): 保存 API Key 时落来源审计

- 修订历史新增 source_address/source_origin:操作人、时间、环境之外,
  再记录客户端地址与控制台来源,明文录入事后可追溯。
- 来源只用于取证,不参与放行;最近一跳代理追加的 X-Forwarded-For 才可信,
  直连请求不能用伪造链改地址;解析不出就留空,绝不猜值。
- 截断按 UTF-8 边界收口,避免恶意 Origin 让整条保存变成存储错误。
- 迁移 083 只追加列,回滚只删这两列。
This commit is contained in:
Zhero & Rin
2026-11-25 12:06:16 +08:00
committed by 姚章浩
parent 32c7074f41
commit 9ddc3291cc
11 changed files with 178 additions and 5 deletions
+12
View File
@@ -12,6 +12,7 @@ import (
"strings"
"testing"
"time"
"unicode/utf8"
"github.com/jackc/pgx/v5"
@@ -209,3 +210,14 @@ func TestUnboundSideReportsWhereItFallsBackTo(t *testing.T) {
t.Fatalf("bound view must not claim a fallback: %+v", bound)
}
}
func TestAuditTextBoundsWithoutSplittingUTF8(t *testing.T) {
// 恶意 Origin 可能带非 ASCII:截断若切开跑码点,PostgreSQL text 会拒收整条保存。
got := auditText(strings.Repeat("密钥", 100), 10)
if len(got) > 10 || !utf8.ValidString(got) || got == "" {
t.Fatalf("auditText = %q (%d bytes)", got, len(got))
}
if auditText(" ", 10) != "" {
t.Fatal("blank audit source must stay empty, never a placeholder")
}
}