feat(tasks): 删除任务可选连带删除资产/漏洞/流量/文件(默认不选)
删除弹窗新增四个默认不勾的复选框:
- 删除关联的资产:仅删本任务独有资产,与其他任务共享的仅解除关联(保留)。
- 删除发现的漏洞:删本任务全部 findings。
- 删除测试的流量:按本任务资产的 host 清录制流量(host 无任务标签,会连带清
其他任务命中同一 host 的流量)。
- 删除测试过程中写的文件:删 <workDir>/tasks/<id> 目录(worker 产物 + 上传)。
- db: AssetStore.DeleteByTaskID(独有删+共享解关联)/HostsByTask;DeleteFindingsByTask。
- server: pgDeleteTask 读可选 JSON body {assets,findings,traffic,files},先按 host
清流量(需在删资产前取 host)→删漏洞→删资产→删任务→删文件目录;空 body=只删任务。
- web: api.deleteTask(id,opts);删除弹窗四复选框(默认不选)+说明。
go build/vet、tsc 通过;新增 TestDeleteByTaskID(独有删/共享解关联/host 反查)。
This commit is contained in:
1 parent
2aeb2e49da
commit
4f7c1a199d
6 files changed
+215
-14
No files matched your search
@@ -1053,6 +1053,45 @@ func (s *AssetStore) GetByIDs(ids []int64) ([]*Asset, error) {
|
||||
return scanAssets(rows)
|
||||
}
|
||||
|
||||
// DeleteByTaskID removes a task's assets: hard-deletes assets owned solely by this
|
||||
// task (task_ids = {taskID}) and de-associates the rest (drops this task from their
|
||||
// task_ids, keeping assets shared with other tasks intact). Returns rows deleted.
|
||||
func (s *AssetStore) DeleteByTaskID(taskID int64) (int64, error) {
|
||||
res, err := s.db.Exec(`DELETE FROM assets WHERE task_ids = ARRAY[$1]::bigint[]`, taskID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if _, err := s.db.Exec(`UPDATE assets SET task_ids = array_remove(task_ids, $1) WHERE $1 = ANY(task_ids)`, taskID); err != nil {
|
||||
return n, err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// HostsByTask returns the distinct hostnames/IPs the task's assets carry (domain,
|
||||
// root_domain, ip) — used to purge host-keyed traffic recorded for that task.
|
||||
func (s *AssetStore) HostsByTask(taskID int64) ([]string, error) {
|
||||
rows, err := s.db.Query(`
|
||||
SELECT DISTINCT h FROM (
|
||||
SELECT NULLIF(domain,'') AS h FROM assets WHERE $1 = ANY(task_ids)
|
||||
UNION SELECT NULLIF(root_domain,'') FROM assets WHERE $1 = ANY(task_ids)
|
||||
UNION SELECT NULLIF(ip,'') FROM assets WHERE $1 = ANY(task_ids)
|
||||
) q WHERE h IS NOT NULL AND h <> ''`, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var h string
|
||||
if err := rows.Scan(&h); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, h)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// DeleteByCompanyID hard-deletes all assets belonging to a company. Returns rows deleted.
|
||||
func (s *AssetStore) DeleteByCompanyID(companyID int64) (int64, error) {
|
||||
res, err := s.db.Exec(`DELETE FROM assets WHERE company_id = $1`, companyID)
|
||||
|
||||
@@ -2,6 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -535,6 +536,59 @@ func TestQueryByType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeleteByTaskID: 独有资产被删,与其他任务共享的资产仅解除关联(保留),host 反查正确。
|
||||
func TestDeleteByTaskID(t *testing.T) {
|
||||
d, av2, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
|
||||
const taskA = int64(90001)
|
||||
const taskB = int64(90002)
|
||||
// solo:仅属 taskA
|
||||
solo, err := av2.UpsertRootDomain(UpsertRootDomainReq{Domain: "solo-del.test", TaskID: taskA})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deleteAsset(d, solo)
|
||||
// shared:先 taskA 再 taskB → task_ids={A,B}
|
||||
shared, err := av2.UpsertRootDomain(UpsertRootDomainReq{Domain: "shared-del.test", TaskID: taskA})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer deleteAsset(d, shared)
|
||||
if _, err := av2.UpsertRootDomain(UpsertRootDomainReq{Domain: "shared-del.test", TaskID: taskB}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// host 反查(删资产前):应含两个域名
|
||||
hosts, err := av2.HostsByTask(taskA)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !slices.Contains(hosts, "solo-del.test") || !slices.Contains(hosts, "shared-del.test") {
|
||||
t.Fatalf("HostsByTask 缺 host: %v", hosts)
|
||||
}
|
||||
|
||||
n, err := av2.DeleteByTaskID(taskA)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Fatalf("DeleteByTaskID: 应删 1 个独有资产,实删 %d", n)
|
||||
}
|
||||
// solo 已删
|
||||
if a, _ := av2.GetByIDs([]int64{solo}); len(a) != 0 {
|
||||
t.Fatalf("solo 资产应被删除")
|
||||
}
|
||||
// shared 保留,且 task_ids 只剩 taskB
|
||||
sa, _ := av2.GetByIDs([]int64{shared})
|
||||
if len(sa) != 1 {
|
||||
t.Fatalf("shared 资产应保留")
|
||||
}
|
||||
if slices.Contains(sa[0].TaskIDs, taskA) || !slices.Contains(sa[0].TaskIDs, taskB) {
|
||||
t.Fatalf("shared task_ids 应解除 A 保留 B,得 %v", sa[0].TaskIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryByTask(t *testing.T) {
|
||||
d, av2, _ := testSetup(t)
|
||||
defer d.Close()
|
||||
|
||||
@@ -329,6 +329,17 @@ func (d *DB) DeleteFinding(id int64) (int64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
// DeleteFindingsByTask removes all findings rows of a task. The originating
|
||||
// exploration finding nodes are cascade-deleted separately when the task's
|
||||
// exploration subgraph is dropped. Returns rows deleted.
|
||||
func (d *DB) DeleteFindingsByTask(taskID int64) (int64, error) {
|
||||
res, err := d.Exec(`DELETE FROM findings WHERE task_id=$1`, taskID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// SetFindingStatus updates one finding's triage state. Returns rows affected.
|
||||
func (d *DB) SetFindingStatus(id int64, status string) (int64, error) {
|
||||
res, err := d.Exec(`UPDATE findings SET status=$1 WHERE id=$2`, status, id)
|
||||
|
||||
Reference in new issue
Block a user