fix(mcp): clarify recovery for invalid finding IDs and image paths
This commit is contained in:
parent
d7179957a6
commit
02103fac9e
|
|
@ -2,6 +2,8 @@ package app
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
|
@ -92,6 +94,9 @@ func mcpToolAuthorizer(db *database.DB) func(context.Context, string, map[string
|
|||
}
|
||||
id := mcpAuthorizationString(args, "id")
|
||||
if db == nil || id == "" || !db.UserCanAccessResource(principal.UserID, principal.ScopeFor(permission), "vulnerability", id) {
|
||||
if toolName == builtin.ToolUpdateVulnerabilityStatus {
|
||||
return fmt.Errorf("找不到可更新的漏洞。%s", vulnerabilityIDRecovery)
|
||||
}
|
||||
return fmt.Errorf("no access to vulnerability %s", id)
|
||||
}
|
||||
convID := mcpAuthorizationConversationID(ctx)
|
||||
|
|
@ -115,7 +120,11 @@ func mcpToolAuthorizer(db *database.DB) func(context.Context, string, map[string
|
|||
}
|
||||
return nil
|
||||
}
|
||||
return authorizeMCPProjectResourceBoundary(ctx, db, "vulnerability", id)
|
||||
err := authorizeMCPProjectResourceBoundary(ctx, db, "vulnerability", id)
|
||||
if toolName == builtin.ToolUpdateVulnerabilityStatus && errors.Is(err, sql.ErrNoRows) {
|
||||
return fmt.Errorf("找不到可更新的漏洞。%s", vulnerabilityIDRecovery)
|
||||
}
|
||||
return err
|
||||
case builtin.ToolQueryAssets:
|
||||
return require("asset:read")
|
||||
case builtin.ToolGetAsset:
|
||||
|
|
|
|||
|
|
@ -54,6 +54,19 @@ func TestVulnerabilityRetestMCPUpdatesOnlyAssignedFinding(t *testing.T) {
|
|||
registerVulnerabilityTools(server, db, zap.NewNop())
|
||||
principal := authctx.NewPrincipal(user.ID, user.Username, database.RBACScopeAssigned, map[string]bool{"vulnerability:read": true, "vulnerability:write": true})
|
||||
ctx := authctx.WithPrincipal(mcp.WithMCPConversationID(context.Background(), branch.ID), principal)
|
||||
sourceCtx := authctx.WithPrincipal(mcp.WithMCPConversationID(context.Background(), source.ID),
|
||||
authctx.NewPrincipal(user.ID, user.Username, database.RBACScopeAll, map[string]bool{"vulnerability:read": true, "vulnerability:write": true}))
|
||||
idPrefix := strings.SplitN(findings[0].ID, "-", 2)[0]
|
||||
_, _, err = server.CallTool(sourceCtx, builtin.ToolUpdateVulnerabilityStatus, map[string]interface{}{
|
||||
"id": idPrefix, "status": "fixed", "retest_notes": "fixture",
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), vulnerabilityIDRecovery) {
|
||||
t.Fatalf("short ID did not return recovery guidance: %v", err)
|
||||
}
|
||||
result, _, err := server.CallTool(sourceCtx, builtin.ToolListVulnerabilities, map[string]interface{}{"q": idPrefix})
|
||||
if err != nil || result == nil || result.IsError || !strings.Contains(mcp.ToolResultPlainText(result), "id="+findings[0].ID) {
|
||||
t.Fatalf("search by short ID did not return the full ID: %v %+v", err, result)
|
||||
}
|
||||
call := func(id, status, notes string) bool {
|
||||
t.Helper()
|
||||
result, _, err := server.CallTool(ctx, builtin.ToolUpdateVulnerabilityStatus, map[string]interface{}{"id": id, "status": status, "retest_notes": notes})
|
||||
|
|
@ -81,7 +94,7 @@ func TestVulnerabilityRetestMCPUpdatesOnlyAssignedFinding(t *testing.T) {
|
|||
t.Fatalf("unrelated finding changed: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
result, _, err := server.CallTool(ctx, builtin.ToolGetVulnerability, map[string]interface{}{"id": findings[0].ID})
|
||||
result, _, err = server.CallTool(ctx, builtin.ToolGetVulnerability, map[string]interface{}{"id": findings[0].ID})
|
||||
if err != nil || result.IsError {
|
||||
t.Fatalf("branch cannot read source finding: %v %+v", err, result)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package app
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
|
@ -14,6 +15,8 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const vulnerabilityIDRecovery = "请用 list_vulnerabilities 的 q 参数搜索标题或 ID 片段,再使用返回的完整 id。"
|
||||
|
||||
func conversationIDFromToolCtx(ctx context.Context) string {
|
||||
if id := agent.ConversationIDFromContext(ctx); id != "" {
|
||||
return id
|
||||
|
|
@ -388,7 +391,7 @@ func registerListVulnerabilitiesTool(mcpServer *mcp.Server, db *database.DB, log
|
|||
},
|
||||
"q": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "关键词搜索(标题、描述、类型、目标等)",
|
||||
"description": "搜索标题、描述、类型、目标或漏洞 ID(支持搜索 ID 片段,结果包含完整 ID)",
|
||||
},
|
||||
"limit": map[string]interface{}{
|
||||
"type": "integer",
|
||||
|
|
@ -513,7 +516,7 @@ func registerUpdateVulnerabilityStatusTool(server *mcp.Server, db *database.DB,
|
|||
ShortDescription: "更新漏洞状态与复测结果",
|
||||
InputSchema: map[string]interface{}{
|
||||
"type": "object", "properties": map[string]interface{}{
|
||||
"id": map[string]interface{}{"type": "string", "description": "已有漏洞 ID"},
|
||||
"id": map[string]interface{}{"type": "string", "description": "list_vulnerabilities 返回的完整漏洞 id"},
|
||||
"status": map[string]interface{}{"type": "string", "enum": []string{"open", "confirmed", "fixed", "false_positive", "ignored"}},
|
||||
"retest_notes": map[string]interface{}{"type": "string", "description": "本次复测方法、请求/响应或工具输出证据及结论,必填"},
|
||||
}, "required": []string{"id", "status", "retest_notes"},
|
||||
|
|
@ -531,7 +534,13 @@ func registerUpdateVulnerabilityStatusTool(server *mcp.Server, db *database.DB,
|
|||
}
|
||||
v, err := db.GetVulnerability(id)
|
||||
if err != nil {
|
||||
return textResult("错误: 漏洞不存在", true), nil
|
||||
if errors.Is(err, database.ErrVulnerabilityNotFound) {
|
||||
return textResult("错误: 未找到该漏洞。"+vulnerabilityIDRecovery, true), nil
|
||||
}
|
||||
if logger != nil {
|
||||
logger.Error("读取待更新漏洞失败", zap.Error(err))
|
||||
}
|
||||
return textResult("错误: 读取待更新漏洞失败", true), nil
|
||||
}
|
||||
pid, err := db.GetConversationProjectID(convID)
|
||||
if err != nil || !canAccessVulnerabilityForRetest(db, v, convID, strings.TrimSpace(pid)) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package database
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
|
|
@ -15,6 +16,8 @@ import (
|
|||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
var ErrVulnerabilityNotFound = errors.New("漏洞不存在")
|
||||
|
||||
// VulnerabilityListFilter 列表/统计/导出共用的筛选条件
|
||||
type VulnerabilityListFilter struct {
|
||||
BrowserSelection string
|
||||
|
|
@ -289,7 +292,7 @@ func (db *DB) GetVulnerability(id string) (*Vulnerability, error) {
|
|||
)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("漏洞不存在")
|
||||
return nil, ErrVulnerabilityNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("获取漏洞失败: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ func ResolveImagePath(path string, cwd string) (string, error) {
|
|||
|
||||
ext := strings.ToLower(filepath.Ext(resolved))
|
||||
if _, ok := allowedImageExt[ext]; !ok {
|
||||
return "", fmt.Errorf("unsupported image extension %q", ext)
|
||||
return "", fmt.Errorf("unsupported image extension %q, use a file with a matching image extension such as .png or .jpg", ext)
|
||||
}
|
||||
|
||||
st, err := os.Stat(resolved)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package vision
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -41,12 +42,12 @@ func TestResolveImagePath_absoluteOutsideCWD(t *testing.T) {
|
|||
|
||||
func TestResolveImagePath_rejectsNonImageExt(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
f := filepath.Join(dir, "notes.txt")
|
||||
f := filepath.Join(dir, "notes.body")
|
||||
if err := os.WriteFile(f, []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := ResolveImagePath(f, dir)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-image extension")
|
||||
if err == nil || !strings.Contains(err.Error(), "unsupported image extension \".body\"") || !strings.Contains(err.Error(), ".png or .jpg") {
|
||||
t.Fatalf("expected extension error with recovery guidance, got %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ func RegisterAnalyzeImageTool(mcpServer *mcp.Server, cfg *config.Config, logger
|
|||
"properties": map[string]interface{}{
|
||||
"path": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "图片绝对路径或相对于进程工作目录的路径",
|
||||
"description": "服务器本地图片的绝对路径或相对进程工作目录的路径,文件名须使用实际图片格式对应的扩展名,例如 .png 或 .jpg",
|
||||
},
|
||||
"question": map[string]interface{}{
|
||||
"type": "string",
|
||||
|
|
|
|||
Loading…
Reference in New Issue