mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-10-28 09:13:49 +08:00
fix(assets): ip 字段拒收主机名,并告诉调用方该怎么改
上一次修复让非法 ip 不再打挂企业归属重算,但脏数据仍在持续产生。这次从源头
拦住:assets.ip 会被转成 inet 参与网段归属,存主机名的资产对所有 IP/CIDR
范围规则都是隐形的。
- db 层新增 ValidateAssetIP + ErrAssetIPInvalid,在 UpsertIP /
UpsertHTTPService / UpsertOtherService / UpsertEndpoint 四个入口校验。
放在 db 层是因为 Agent 工具和资产 API 都经由它们,不必写两遍;内部调用方
(enrich 的解析结果、task_assets 的 ParseCIDR 输出、RecordValue 已 ParseIP
过滤的分支)传的本就是合法地址,不受影响。
- 错误信息给的是改法而不只是「非法」:带上收到的原值,并指出主机名应改用
type=subdomain 填 domain,或先解析 A/AAAA 记录再填 ip。insert_assets 的
错误是逐条返回的({index, error}),同批其余资产照常入库,Agent 拿到后能
定位到具体那条并在下一轮自行修正。
- 同步收紧 insert_assets 的工具描述与 ip 字段说明,在提示层先挡一道。
- 空 ip 仍然合法:service / endpoint 的 ip 是可选字段。
验证: 新增 db 层四类型拒收测试与 agent 层「一批混入一条非法、其余照常入库」
测试,均连 PG 实跑通过;干净库 go test -p 1 ./... 无新增失败(仅剩既有的
TestCompanyScopeInvalid)。
This commit is contained in:
1 parent
9e98407528
commit
dfbbb33069
5 files changed
+147
-2
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -946,3 +948,54 @@ func TestAssetPaginationUsesStableIDTieBreaker(t *testing.T) {
|
||||
t.Fatalf("DSL pagination order=%v want=%v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAssetIPRejectsHostname pins the write-side guard added after a hostname in
|
||||
// assets.ip broke company attribution: every asset type that persists an ip must
|
||||
// refuse a non-address, and the message must tell the caller how to fix it.
|
||||
func TestAssetIPRejectsHostname(t *testing.T) {
|
||||
d, av2, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
|
||||
hostname := "d63cd476.cdn.ucloud.com.cn"
|
||||
cases := []struct {
|
||||
name string
|
||||
call func() (int64, error)
|
||||
}{
|
||||
{"ip", func() (int64, error) {
|
||||
return av2.UpsertIP(UpsertIPReq{IP: hostname})
|
||||
}},
|
||||
{"http service", func() (int64, error) {
|
||||
return av2.UpsertHTTPService(UpsertHTTPServiceReq{URL: "https://example.com/", IP: hostname})
|
||||
}},
|
||||
{"other service", func() (int64, error) {
|
||||
return av2.UpsertOtherService(UpsertOtherServiceReq{IP: hostname, Port: 22, ServiceName: "ssh"})
|
||||
}},
|
||||
{"endpoint", func() (int64, error) {
|
||||
return av2.UpsertEndpoint(UpsertEndpointReq{URL: "https://example.com/a", Method: "GET", IP: hostname})
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
id, err := tc.call()
|
||||
if !errors.Is(err, ErrAssetIPInvalid) {
|
||||
deleteAsset(d, id)
|
||||
t.Fatalf("%s: err=%v, want %v", tc.name, err, ErrAssetIPInvalid)
|
||||
}
|
||||
// The caller has to learn the remedy, not just that something was wrong.
|
||||
if !strings.Contains(err.Error(), hostname) || !strings.Contains(err.Error(), "type=subdomain") {
|
||||
t.Fatalf("%s: message is not actionable: %v", tc.name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty ip stays legal for the types where it is optional, and a real
|
||||
// address is still accepted.
|
||||
id, err := av2.UpsertHTTPService(UpsertHTTPServiceReq{URL: "https://ip-guard.example.com/"})
|
||||
if err != nil {
|
||||
t.Fatalf("empty ip rejected: %v", err)
|
||||
}
|
||||
deleteAsset(d, id)
|
||||
id, err = av2.UpsertIP(UpsertIPReq{IP: "203.0.113.7"})
|
||||
if err != nil {
|
||||
t.Fatalf("valid ip rejected: %v", err)
|
||||
}
|
||||
deleteAsset(d, id)
|
||||
}
|
||||
Reference in new issue
Block a user