fix(kb): 保存 API Key 时落来源审计
- 修订历史新增 source_address/source_origin:操作人、时间、环境之外, 再记录客户端地址与控制台来源,明文录入事后可追溯。 - 来源只用于取证,不参与放行;最近一跳代理追加的 X-Forwarded-For 才可信, 直连请求不能用伪造链改地址;解析不出就留空,绝不猜值。 - 截断按 UTF-8 边界收口,避免恶意 Origin 让整条保存变成存储错误。 - 迁移 083 只追加列,回滚只删这两列。
This commit is contained in:
@@ -112,6 +112,10 @@ type SaveInput struct {
|
||||
APIKey string `json:"api_key"`
|
||||
InternalKBID string `json:"internal_kb_id"`
|
||||
ExternalKBID string `json:"external_kb_id"`
|
||||
// 审计用:由 HTTP 层按连接事实填充,不属于请求体(严格解码下客户端
|
||||
// 提交同名字段会被拒绝)。只是事后取证,不参与任何放行判定。
|
||||
SourceAddress string `json:"-"`
|
||||
SourceOrigin string `json:"-"`
|
||||
}
|
||||
|
||||
// Target is what consumers (readiness, upload, listing, monitoring) need to
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,6 +286,8 @@ func TestClearKeyKeepsNamesIDsAndFrozenIdentity(t *testing.T) {
|
||||
cleared, err := store.Save(ctx, kbbinding.EnvTest, kbbinding.SaveInput{
|
||||
BaseRevision: saved.Revision, BaseURL: server.URL, KeyAction: "clear",
|
||||
InternalKBID: saved.InternalKBID, ExternalKBID: saved.ExternalKBID,
|
||||
// 非 HTTPS 控制台保存:审计行必须能还原当时的入口与客户端地址。
|
||||
SourceAddress: "172.29.151.11", SourceOrigin: "http://172.29.151.11:8088",
|
||||
}, "admin", now.Add(time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("clear Save() error = %v", err)
|
||||
@@ -364,6 +366,17 @@ WHERE environment='test' AND revision=2`).Scan(&keyConfigured); err != nil {
|
||||
if keyConfigured {
|
||||
t.Fatal("history still says the cleared key is configured")
|
||||
}
|
||||
var sourceAddress, sourceOrigin, sourceActor string
|
||||
var sourceEnv string
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT source_address,source_origin,actor,environment FROM weknora_environment_binding_history
|
||||
WHERE environment='test' AND revision=2`).Scan(&sourceAddress, &sourceOrigin, &sourceActor, &sourceEnv); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sourceAddress != "172.29.151.11" || sourceOrigin != "http://172.29.151.11:8088" ||
|
||||
sourceActor != "admin" || sourceEnv != "test" {
|
||||
t.Fatalf("save audit = %q/%q actor=%q env=%q", sourceAddress, sourceOrigin, sourceActor, sourceEnv)
|
||||
}
|
||||
var secretColumns int
|
||||
if err := pool.QueryRow(ctx, `
|
||||
SELECT COUNT(*) FROM information_schema.columns
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
@@ -206,9 +207,11 @@ ON CONFLICT(environment) DO UPDATE SET
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO weknora_environment_binding_history(
|
||||
environment,revision,base_url,key_configured,internal_kb_id,external_kb_id,verification,actor,created_at)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9)`,
|
||||
env, revision, baseURL, len(sealed) > 0, internal, external, verification, actor, now.UTC()); err != nil {
|
||||
environment,revision,base_url,key_configured,internal_kb_id,external_kb_id,verification,actor,created_at,
|
||||
source_address,source_origin)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)`,
|
||||
env, revision, baseURL, len(sealed) > 0, internal, external, verification, actor, now.UTC(),
|
||||
auditText(input.SourceAddress, 128), auditText(input.SourceOrigin, 300)); err != nil {
|
||||
return View{}, ErrStorage
|
||||
}
|
||||
replan := postgresstore.RoutingBindingReplanResult{}
|
||||
@@ -353,3 +356,19 @@ func shortID(value string) string {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// auditText bounds one audit column. An empty value stays empty: the audit row
|
||||
// must never present a guessed address as fact. Truncation stops on a rune
|
||||
// boundary because PostgreSQL text refuses invalid UTF-8 (a hostile Origin
|
||||
// header must not turn a save into a storage error).
|
||||
func auditText(value string, limit int) string {
|
||||
value = strings.ToValidUTF8(strings.TrimSpace(value), "")
|
||||
if len(value) <= limit {
|
||||
return value
|
||||
}
|
||||
value = value[:limit]
|
||||
for len(value) > 0 && !utf8.ValidString(value) {
|
||||
value = value[:len(value)-1]
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user