fix(agent): list_assets 按任务范围过滤,修复任务间资产互串 (#59)

list_assets 此前把任务 id 写死为 0、裸查整个共享资产库,模型又无从写出
范围过滤条件,于是把别的任务的资产(尤其 IP)当成本任务目标,测试方向
被带偏。

- 新增 QueryDSLInScope / GetByIDsInScope:只返回落在【本任务及直接关联
  任务】task_scope 内的资产,按归属匹配而非字面值(根域范围含其全部子域/
  服务/接口,网段范围含段内主机及服务);越权 id 静默丢弃
- 新增 scopeTargetCTE(纯 scope 归属判定,不含 anchor/tested);whereBuilder
  加占位符偏移,$1 留给 taskID
- 修复 IP 直连主机(http://1.2.3.4/api)无法被网段范围命中的盲区:谓词加
  OR ts.net >>= try_inet(a.domain)
- 非任务上下文(Auto/pentest, taskID<=0)无范围可依,回退全库
- QueryDSL/CountDSL 原样未动,UI 的「测试资产」视图(按生产者过滤)不受影响
This commit is contained in:
Autumn-27 committed 2026-09-12 03:25:47 -04:00
1 parent 16e87b48c2
commit 4ccb8f4ff2
5 files changed
+267 -7

No files matched your search

+97 -2
View File
@@ -259,11 +259,12 @@ var fullTextCols = []string{
type whereBuilder struct {
args []any
base int // placeholders are numbered base+1, base+2, …; 0 = the usual $1, $2, …
}
func (b *whereBuilder) next(v any) string {
b.args = append(b.args, v)
return fmt.Sprintf("$%d", len(b.args))
return fmt.Sprintf("$%d", b.base+len(b.args))
}
func (b *whereBuilder) build(node *astNode) (string, error) {
@@ -378,10 +379,17 @@ func (b *whereBuilder) buildLeaf(e Expr) (string, error) {
}
func buildDSLWhere(node *astNode) (string, []any, error) {
return buildDSLWhereBase(node, 0)
}
// buildDSLWhereBase is buildDSLWhere with a placeholder offset: emitted args are
// numbered base+1 onward, leaving $1..$base free for the caller (e.g. a scope CTE
// that reserves $1 for the task id).
func buildDSLWhereBase(node *astNode, base int) (string, []any, error) {
if node == nil {
return "1=1", nil, nil
}
b := &whereBuilder{}
b := &whereBuilder{base: base}
clause, err := b.build(node)
if err != nil {
return "", nil, err
@@ -540,3 +548,90 @@ func (s *AssetStore) QueryDSL(dsl, typ string, taskID int64, limit, offset int)
}
return assets, nil
}
// QueryDSLInScope is QueryDSL restricted to assets that BELONG to taskID's (and its
// direct source tasks') declared scope — membership, not literal value: a
// root_domain scope returns every subdomain / service / endpoint under it. This is
// the agent-facing list_assets path, so an agent queries the task's relevant assets
// instead of the whole shared库. taskID<=0 (non-task contexts: Auto / pentest / chat)
// has no scope to honor and falls back to the plain global QueryDSL. Rows carry the
// same per-task source metadata as QueryByTask.
func (s *AssetStore) QueryDSLInScope(dsl, typ string, taskID int64, limit, offset int) ([]*Asset, error) {
if taskID <= 0 {
return s.QueryDSL(dsl, typ, 0, limit, offset)
}
if limit <= 0 {
limit = 50
}
if offset < 0 {
offset = 0
}
node, err := ParseDSL(dsl)
if err != nil {
return nil, err
}
// $1 is reserved for taskID (scopeTargetCTE); DSL placeholders start at $2.
where, dslArgs, err := buildDSLWhereBase(node, 1)
if err != nil {
return nil, err
}
args := []any{taskID}
args = append(args, dslArgs...)
if typ != "" {
args = append(args, typ)
where += fmt.Sprintf(" AND type = $%d", len(args))
}
where += " AND id IN (SELECT id FROM target)"
args = append(args, limit, offset)
q := `WITH ` + scopeTargetCTE + ` ` + assetSelectCols + " WHERE " + where +
fmt.Sprintf(" ORDER BY last_seen DESC, id DESC LIMIT $%d OFFSET $%d", len(args)-1, len(args))
rows, err := s.db.Query(q, args...)
if err != nil {
return nil, err
}
defer rows.Close()
assets, err := scanAssets(rows)
if err != nil {
return nil, err
}
if err := s.hydrateTaskAssetSources(taskID, assets); err != nil {
return nil, err
}
return assets, nil
}
// GetByIDsInScope is GetByIDs restricted to ids that BELONG to taskID's (and its
// direct source tasks') declared scope, so an agent cannot reach out-of-scope
// assets by id. taskID<=0 (non-task contexts) falls back to the global GetByIDs.
// Out-of-scope ids are silently dropped from the result (not an error).
func (s *AssetStore) GetByIDsInScope(taskID int64, ids []int64) ([]*Asset, error) {
if taskID <= 0 {
return s.GetByIDs(ids)
}
if len(ids) == 0 {
return nil, nil
}
args := make([]any, 0, len(ids)+1)
args = append(args, taskID) // $1 reserved for scopeTargetCTE
placeholders := make([]string, len(ids))
for i, id := range ids {
placeholders[i] = fmt.Sprintf("$%d", i+2)
args = append(args, id)
}
q := `WITH ` + scopeTargetCTE + ` ` + assetSelectCols +
" WHERE id IN (" + strings.Join(placeholders, ",") + ")" +
" AND id IN (SELECT id FROM target) ORDER BY last_seen DESC, id DESC"
rows, err := s.db.Query(q, args...)
if err != nil {
return nil, err
}
defer rows.Close()
assets, err := scanAssets(rows)
if err != nil {
return nil, err
}
if err := s.hydrateTaskAssetSources(taskID, assets); err != nil {
return nil, err
}
return assets, nil
}