193 lines
6.5 KiB
Go
193 lines
6.5 KiB
Go
package project
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"strings"
|
||
|
||
"cyberstrike-ai/internal/config"
|
||
"cyberstrike-ai/internal/database"
|
||
)
|
||
|
||
// projectScopePayload 解析 projects.scope_json(约定字段,可扩展)。
|
||
type projectScopePayload struct {
|
||
Targets []string `json:"targets"`
|
||
Exclude []string `json:"exclude"`
|
||
Notes string `json:"notes"`
|
||
}
|
||
|
||
// BuildScopeBlock 将项目 scope_json 格式化为 Agent 可读的授权范围块。
|
||
func BuildScopeBlock(proj *database.Project) string {
|
||
if proj == nil {
|
||
return ""
|
||
}
|
||
raw := strings.TrimSpace(proj.ScopeJSON)
|
||
if raw == "" {
|
||
return ""
|
||
}
|
||
|
||
var payload projectScopePayload
|
||
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
|
||
return fmt.Sprintf("## 项目测试范围(project: %s)\n(scope_json 非合法 JSON,请人工核对配置)\n```\n%s\n```\n"+
|
||
"仅对明确授权目标执行测试;超出范围须停止并说明。\n", proj.Name, truncateRunes(raw, 800))
|
||
}
|
||
|
||
var b strings.Builder
|
||
b.WriteString(fmt.Sprintf("## 项目测试范围(project: %s, id: %s)\n", proj.Name, proj.ID))
|
||
b.WriteString("以下为授权边界,**必须遵守**:仅测试列出的 targets,避开 exclude,不得擅自扩大范围。\n")
|
||
|
||
if len(payload.Targets) > 0 {
|
||
b.WriteString("\n**允许测试(targets)**:\n")
|
||
for _, t := range payload.Targets {
|
||
t = strings.TrimSpace(t)
|
||
if t != "" {
|
||
b.WriteString("- " + t + "\n")
|
||
}
|
||
}
|
||
}
|
||
if len(payload.Exclude) > 0 {
|
||
b.WriteString("\n**明确排除(exclude)**:\n")
|
||
for _, t := range payload.Exclude {
|
||
t = strings.TrimSpace(t)
|
||
if t != "" {
|
||
b.WriteString("- " + t + "\n")
|
||
}
|
||
}
|
||
}
|
||
if n := strings.TrimSpace(payload.Notes); n != "" {
|
||
b.WriteString("\n**说明(notes)**:\n" + n + "\n")
|
||
}
|
||
if len(payload.Targets) == 0 && len(payload.Exclude) == 0 && strings.TrimSpace(payload.Notes) == "" {
|
||
b.WriteString("\n(scope_json 已配置但未识别 targets/exclude/notes 字段,原始内容供参考)\n```json\n")
|
||
b.WriteString(truncateRunes(raw, 1200))
|
||
b.WriteString("\n```\n")
|
||
}
|
||
b.WriteString("\n若目标不在 targets 内或命中 exclude,不得主动扫描/利用;需用户明确扩大授权后再继续。\n")
|
||
return b.String()
|
||
}
|
||
|
||
func truncateRunes(s string, max int) string {
|
||
r := []rune(s)
|
||
if len(r) <= max {
|
||
return s
|
||
}
|
||
return string(r[:max]) + "…"
|
||
}
|
||
|
||
// BuildProjectBlackboardBlock 组合测试范围 + 事实黑板索引。
|
||
func BuildProjectBlackboardBlock(db *database.DB, projectID string, cfg config.ProjectConfig, conversationIDs ...string) (string, error) {
|
||
projectID = strings.TrimSpace(projectID)
|
||
if projectID == "" {
|
||
return "", nil
|
||
}
|
||
proj, err := db.GetProject(projectID)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
parts := []string{}
|
||
if scope := strings.TrimSpace(BuildScopeBlock(proj)); scope != "" {
|
||
parts = append(parts, scope)
|
||
}
|
||
index, err := BuildFactIndexBlock(db, projectID, cfg, conversationIDs...)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
if strings.TrimSpace(index) != "" {
|
||
parts = append(parts, index)
|
||
}
|
||
vis := database.FactVisibility{CanReadProjects: true, ProjectScope: database.RBACScopeAll, CanReadConversations: true, ConversationScope: database.RBACScopeAll}
|
||
if len(conversationIDs) > 0 {
|
||
var owner string
|
||
_ = db.QueryRow(`SELECT COALESCE(owner_user_id,'') FROM conversations WHERE id=?`, conversationIDs[0]).Scan(&owner)
|
||
if owner != "" {
|
||
if resolved, err := db.FactVisibilityFor(owner); err == nil {
|
||
vis = resolved
|
||
}
|
||
}
|
||
}
|
||
access := database.RBACListAccess{Scope: database.RBACScopeAll}
|
||
if vis.UserID != "" {
|
||
scope := vis.ProjectScope
|
||
if scope == "" || scope == "none" {
|
||
scope = database.RBACScopeOwn
|
||
}
|
||
access = database.RBACListAccess{UserID: vis.UserID, Scope: scope}
|
||
}
|
||
if library, err := BuildAssetLibraryBlock(db, projectID, access); err != nil {
|
||
return "", err
|
||
} else if strings.TrimSpace(library) != "" {
|
||
parts = append(parts, library)
|
||
}
|
||
if collections, err := BuildCollectionBlock(db, projectID, vis); err != nil {
|
||
return "", err
|
||
} else if strings.TrimSpace(collections) != "" {
|
||
parts = append(parts, collections)
|
||
}
|
||
return strings.Join(parts, "\n\n"), nil
|
||
}
|
||
|
||
// BuildCollectionBlock lists named asset collections visible to the caller.
|
||
func BuildCollectionBlock(db *database.DB, projectID string, vis database.FactVisibility) (string, error) {
|
||
items, err := db.ListAssetCollections(projectID)
|
||
if err != nil || len(items) == 0 {
|
||
return "", err
|
||
}
|
||
if len(items) > 20 {
|
||
items = items[:20]
|
||
}
|
||
var b strings.Builder
|
||
b.WriteString("## 已保存的检索结果\n")
|
||
b.WriteString("这些只是资产库的命名子集,来自信息收集。未指定子集时,对话、批量任务和深度测评的范围仍是上面的项目资产库。已确认观察只统计当前账号能读取、且有成功回执的项目事实,不等于已经扫描。\n")
|
||
for _, item := range items {
|
||
total, observed, err := db.ObservationCoverageVisible(database.RBACListAccess{UserID: vis.UserID, Scope: vis.ProjectScope}, vis, item.ID)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
fmt.Fprintf(&b, "- %s(%s):可见资产 %d 个,已确认观察 %d 个\n", item.Name, item.ID, total, observed)
|
||
}
|
||
return b.String(), nil
|
||
}
|
||
|
||
// BuildAssetLibraryBlock describes the project assets shared by chat, batch
|
||
// tasks and deep assessment. A named collection is not required.
|
||
func BuildAssetLibraryBlock(db *database.DB, projectID string, access database.RBACListAccess) (string, error) {
|
||
projectID = strings.TrimSpace(projectID)
|
||
if db == nil || projectID == "" {
|
||
return "", nil
|
||
}
|
||
assets, total, err := db.ListAssets(12, 0, database.AssetListFilter{ProjectID: projectID}, access)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
var b strings.Builder
|
||
b.WriteString("## 项目资产库\n")
|
||
b.WriteString("对话、批量任务和深度测评使用这里的同一批资产。不要因为没有资产集合就认为没有目标。\n")
|
||
if total == 0 {
|
||
b.WriteString("该项目目前没有可读资产。新发现的目标用 create_asset 写入这个项目。\n")
|
||
return b.String(), nil
|
||
}
|
||
fmt.Fprintf(&b, "当前可读资产 %d 个。下面最多列出 12 个,其余用 query_assets 按该项目分页读取。\n", total)
|
||
for _, asset := range assets {
|
||
fmt.Fprintf(&b, "- %s\n", assetLibraryLine(asset))
|
||
}
|
||
return b.String(), nil
|
||
}
|
||
|
||
func assetLibraryLine(asset *database.Asset) string {
|
||
name := asset.Domain
|
||
if name == "" {
|
||
name = asset.IP
|
||
}
|
||
if name == "" {
|
||
name = asset.Host
|
||
}
|
||
target := name
|
||
if asset.Port > 0 {
|
||
target = fmt.Sprintf("%s:%d", name, asset.Port)
|
||
}
|
||
if asset.Protocol != "" {
|
||
target += " " + asset.Protocol
|
||
}
|
||
return target + "(" + asset.ID + ")"
|
||
}
|