feat(findings): 发现页新增「按资产」视图

左侧资产树 + 右侧该子树下的发现,与另外两个视图共用同一张表和同一批筛选。

后端
- db/finding_assets.go:按当前筛选构建资产树。只收有发现的资产,祖先链逐层
  补齐(endpoint→service→subdomain/ip→root_domain),父子优先级与
  BuildCoverageGraph 同源;计数沿祖先链去重累加,一条发现挂多个资产不会让
  父节点重复计数
- FindingFilter 加 AssetScope:选中节点=选中整棵子树,解析成资产 id 集合后
  下推 SQL,列表 / 分组 / 导出三个入口共用,导出「当前筛选」自动带上
- 「未关联资产」同时收 asset_ids 为空与指向已删资产的发现,与树上桶的口径
  一致(只判空数组会让桶上的数字大于点开后的条数)
- findingFilterFromQuery 统一四处的筛选解析;新增 GIN 索引
  idx_findings_asset_ids,按资产反查发现不再全表扫

前端
- 树默认展开一层,支持过滤(匹配项自动展开)、面包屑下钻、手动刷新
- 每层只显示相对上一层的增量:子域名去根域名后缀、服务显示 `https :443`、
  接口只留路径。完整值在 title 与面包屑,搜索仍按完整值匹配
- 不轮询:进入视图 / 改筛选 / 页内改动发现 / 点刷新时才查
- 节点超上限时整层丢弃接口/服务并提示,计数已在上层不会丢
This commit is contained in:
Autumn-27 committed 2026-08-31 10:58:32 -04:00
1 parent 796dbf5600
commit 23cfb9ed4e
11 files changed
+1582 -82

No files matched your search

+40 -1
View File
@@ -148,6 +148,14 @@ type FindingFilter struct {
TaskID string // 任务 id(字符串形式;空/非法 = 不按任务筛选)
Query string // 名称/类型/摘要/证据/报告正文的模糊检索关键词
Sort string // "severity" | "time"
// AssetScope 是资产树的节点 key(a:<id> / c:<id> / r:<domain> / __none__),
// 选中一个节点等于选中它的整棵子树。空 = 不按资产筛选。
AssetScope string
// 下面三个由 applyAssetScope 从 AssetScope 解析而来,调用方不用设置。
assetIDs []int64 // 子树里所有资产 id
assetNone bool // 只要「未关联资产」的发现
assetMiss bool // 选中的节点在当前筛选下不存在 → 结果恒空
}
// FindingUnassignedTask is the task filter sentinel for findings whose task is
@@ -178,6 +186,25 @@ func (f FindingFilter) where() (string, []any) {
args = append(args, tid)
conds = append(conds, fmt.Sprintf("f.task_id = $%d", len(args)))
}
// 资产筛选:asset_ids 是 jsonb 数组,@> ANY(...) 能走 idx_findings_asset_ids。
switch {
case f.assetMiss:
conds = append(conds, "FALSE")
case f.assetNone:
// 「未关联资产」= asset_ids 为空,或者里面的 id 一个都不在 assets 表里
// (资产已被删除)。两类都进资产树的未关联桶,这里必须同样收下,否则桶上
// 的计数会大于点开后能查到的条数。
conds = append(conds, `(
jsonb_array_length(COALESCE(f.asset_ids, '[]'::jsonb)) = 0
OR NOT EXISTS (
SELECT 1 FROM jsonb_array_elements_text(f.asset_ids) e(v)
JOIN assets a ON a.id = e.v::bigint
)
)`)
case len(f.assetIDs) > 0:
args = append(args, assetIDContainments(f.assetIDs))
conds = append(conds, fmt.Sprintf("f.asset_ids @> ANY($%d::jsonb[])", len(args)))
}
if query := strings.TrimSpace(f.Query); query != "" {
escaped := strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(query)
args = append(args, "%"+escaped+"%")
@@ -205,6 +232,10 @@ func (d *DB) ListFindingsPage(f FindingFilter, page, pageSize int) ([]*DBFinding
if pageSize <= 0 {
pageSize = 20
}
f, err := d.applyAssetScope(f)
if err != nil {
return nil, 0, err
}
where, args := f.where()
var total int
@@ -264,6 +295,10 @@ func (d *DB) ListFindingGroups(f FindingFilter, page, pageSize int) ([]FindingGr
if pageSize <= 0 {
pageSize = 10
}
f, err := d.applyAssetScope(f)
if err != nil {
return nil, 0, 0, err
}
where, args := f.where()
grouped := ` FROM findings f LEFT JOIN tasks t ON f.task_id=t.id` + where +
` GROUP BY t.id, t.name, t.description, t.status, t.paused, t.queued`
@@ -444,7 +479,11 @@ func (d *DB) ListFindingsForExport(f FindingFilter, ids []int64) ([]*DBFinding,
WHERE f.id IN (` + strings.Join(ph, ",") + `)
` + order
} else {
where, wargs := f.where()
scoped, err := d.applyAssetScope(f)
if err != nil {
return nil, err
}
where, wargs := scoped.where()
q = `SELECT ` + cols + `
FROM findings f
LEFT JOIN tasks t ON f.task_id = t.id` + where + `