From 38c581ce900842b7f249099798124649b0c37f0b Mon Sep 17 00:00:00 2001 From: Xiaoping Liao <106010272+kyletser@users.noreply.github.com> Date: Tue, 1 Dec 2026 07:16:42 +0000 Subject: [PATCH] feat(security): let get_global_security_advisory accept a CVE ID Rebase onto the current upstream main. The README is regenerated from the tool schema, so it carries both the new cveId parameter and the upstream changes to the rest of the file. --- README.md | 3 +- .../get_global_security_advisory.snap | 11 +- pkg/github/security_advisories.go | 94 +++++++++++++--- pkg/github/security_advisories_test.go | 102 +++++++++++------- pkg/github/security_outputs.go | 27 +---- 5 files changed, 155 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index ab7377f127..95836f2933 100644 --- a/README.md +++ b/README.md @@ -1642,7 +1642,8 @@ For negotiated supported protocol versions `2026-07-28` or newer, the consolidat - **get_global_security_advisory** - Get a global security advisory - **OAuth Challenge Scopes**: `security_events` - - `ghsaId`: GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx). (string, required) + - `cveId`: CVE ID of the advisory (format: CVE-YYYY-NNNNN). Provide this or ghsaId. (string, optional) + - `ghsaId`: GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx). Provide this or cveId. (string, optional) - **list_global_security_advisories** - List global security advisories - **OAuth Challenge Scopes**: `security_events` diff --git a/pkg/github/__toolsnaps__/get_global_security_advisory.snap b/pkg/github/__toolsnaps__/get_global_security_advisory.snap index 647701421b..5c3c645fb4 100644 --- a/pkg/github/__toolsnaps__/get_global_security_advisory.snap +++ b/pkg/github/__toolsnaps__/get_global_security_advisory.snap @@ -4,17 +4,18 @@ "readOnlyHint": true, "title": "Get a global security advisory" }, - "description": "Get a global security advisory", + "description": "Get a global security advisory by GHSA ID or CVE ID", "inputSchema": { "properties": { + "cveId": { + "description": "CVE ID of the advisory (format: CVE-YYYY-NNNNN). Provide this or ghsaId.", + "type": "string" + }, "ghsaId": { - "description": "GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx).", + "description": "GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx). Provide this or cveId.", "type": "string" } }, - "required": [ - "ghsaId" - ], "type": "object" }, "name": "get_global_security_advisory", diff --git a/pkg/github/security_advisories.go b/pkg/github/security_advisories.go index dfdcab0514..62f50f76df 100644 --- a/pkg/github/security_advisories.go +++ b/pkg/github/security_advisories.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "net/http" + "strings" ghErrors "github.com/github/github-mcp-server/v2/pkg/errors" "github.com/github/github-mcp-server/v2/pkg/ifc" @@ -46,7 +47,7 @@ func ListGlobalSecurityAdvisories(t translations.TranslationHelperFunc) inventor "ecosystem": { Type: "string", Description: "Filter by package ecosystem.", - Enum: []any{"actions", "composer", "erlang", "go", "maven", "npm", "nuget", "other", "pip", "pub", "rubygems", "rust", "swift"}, + Enum: []any{"actions", "composer", "erlang", "go", "maven", "npm", "nuget", "other", "pip", "pub", "rubygems", "rust"}, }, "severity": { Type: "string", @@ -268,7 +269,7 @@ func GetGlobalSecurityAdvisory(t translations.TranslationHelperFunc) inventory.S ToolsetMetadataSecurityAdvisories, mcp.Tool{ Name: "get_global_security_advisory", - Description: t("TOOL_GET_GLOBAL_SECURITY_ADVISORY_DESCRIPTION", "Get a global security advisory"), + Description: t("TOOL_GET_GLOBAL_SECURITY_ADVISORY_DESCRIPTION", "Get a global security advisory by GHSA ID or CVE ID"), Annotations: &mcp.ToolAnnotations{ Title: t("TOOL_GET_GLOBAL_SECURITY_ADVISORY_USER_TITLE", "Get a global security advisory"), ReadOnlyHint: true, @@ -278,10 +279,13 @@ func GetGlobalSecurityAdvisory(t translations.TranslationHelperFunc) inventory.S Properties: map[string]*jsonschema.Schema{ "ghsaId": { Type: "string", - Description: "GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx).", + Description: "GitHub Security Advisory ID (format: GHSA-xxxx-xxxx-xxxx). Provide this or cveId.", + }, + "cveId": { + Type: "string", + Description: "CVE ID of the advisory (format: CVE-YYYY-NNNNN). Provide this or ghsaId.", }, }, - Required: []string{"ghsaId"}, }, }, scopes.RequireAll(scopes.SecurityEvents), @@ -291,22 +295,19 @@ func GetGlobalSecurityAdvisory(t translations.TranslationHelperFunc) inventory.S return nil, nil, fmt.Errorf("failed to get GitHub client: %w", err) } - if args.GHSAID == "" { - return utils.NewToolResultError("invalid ghsaId: missing required parameter: ghsaId"), nil, nil + switch { + case args.GHSAID == "" && args.CVEID == "": + return utils.NewToolResultError("missing required parameter: provide either ghsaId or cveId"), nil, nil + case args.GHSAID != "" && args.CVEID != "": + return utils.NewToolResultError("provide only one of ghsaId or cveId, not both"), nil, nil } - advisory, resp, err := client.SecurityAdvisories.GetGlobalSecurityAdvisories(ctx, args.GHSAID) + advisory, errResult, err := findGlobalSecurityAdvisory(ctx, client, args) if err != nil { - return nil, nil, fmt.Errorf("failed to get advisory: %w", err) + return nil, nil, err } - defer func() { _ = resp.Body.Close() }() - - if resp.StatusCode != http.StatusOK { - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, nil, fmt.Errorf("failed to read response body: %w", err) - } - return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get advisory", resp, body), nil, nil + if errResult != nil { + return errResult, nil, nil } r, err := json.Marshal(advisory) @@ -323,6 +324,67 @@ func GetGlobalSecurityAdvisory(t translations.TranslationHelperFunc) inventory.S ) } +// findGlobalSecurityAdvisory resolves a GHSA ID directly and a CVE ID through the +// advisory list filter GitHub exposes for it. A CVE that maps to several advisories +// is reported instead of returning an arbitrary one, since the tool returns a single +// advisory and the caller needs the GHSA ID to pick. +func findGlobalSecurityAdvisory( + ctx context.Context, + client *github.Client, + args GetGlobalSecurityAdvisoryInput, +) (*github.GlobalSecurityAdvisory, *mcp.CallToolResult, error) { + if args.GHSAID != "" { + advisory, resp, err := client.SecurityAdvisories.GetGlobalSecurityAdvisories(ctx, args.GHSAID) + if err != nil { + return nil, nil, fmt.Errorf("failed to get advisory: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, nil, fmt.Errorf("failed to read response body: %w", err) + } + return nil, ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get advisory", resp, body), nil + } + return advisory, nil, nil + } + + advisories, resp, err := client.SecurityAdvisories.ListGlobalSecurityAdvisories(ctx, &github.ListGlobalSecurityAdvisoriesOptions{ + CVEID: &args.CVEID, + }) + if err != nil { + return nil, nil, fmt.Errorf("failed to list global security advisories: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, nil, fmt.Errorf("failed to read response body: %w", err) + } + return nil, ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to get advisory", resp, body), nil + } + + switch len(advisories) { + case 0: + return nil, utils.NewToolResultError(fmt.Sprintf("no global security advisory found for %s", args.CVEID)), nil + case 1: + return advisories[0], nil, nil + default: + ghsaIDs := make([]string, 0, len(advisories)) + for _, advisory := range advisories { + if advisory != nil && advisory.GHSAID != nil { + ghsaIDs = append(ghsaIDs, *advisory.GHSAID) + } + } + return nil, utils.NewToolResultError(fmt.Sprintf( + "%s matches %d global security advisories (%s); call get_global_security_advisory with the ghsaId you need", + args.CVEID, len(advisories), strings.Join(ghsaIDs, ", "), + )), nil + } +} + func ListOrgRepositorySecurityAdvisories(t translations.TranslationHelperFunc) inventory.ServerTool { return NewTool[ListOrgRepositorySecurityAdvisoriesInput, []*SecurityAdvisoryOutput]( ToolsetMetadataSecurityAdvisories, diff --git a/pkg/github/security_advisories_test.go b/pkg/github/security_advisories_test.go index 83daa72ee2..f851195a59 100644 --- a/pkg/github/security_advisories_test.go +++ b/pkg/github/security_advisories_test.go @@ -127,41 +127,6 @@ func Test_ListGlobalSecurityAdvisories(t *testing.T) { } } -func Test_ListGlobalSecurityAdvisories_SwiftEcosystem(t *testing.T) { - // The server validates arguments against the tool schema, so an ecosystem - // missing from the enum is rejected before the API is ever called. - var gotEcosystem string - client := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ - GetAdvisories: func(w http.ResponseWriter, r *http.Request) { - gotEcosystem = r.URL.Query().Get("ecosystem") - w.WriteHeader(http.StatusOK) - _, _ = w.Write([]byte(`[{"ghsa_id": "GHSA-xxxx-xxxx-xxxx"}]`)) - }, - })) - deps := BaseDeps{Client: client} - server := mcp.NewServer(&mcp.Implementation{Name: "test-server", Version: "v0.0.1"}, nil) - server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) - tool := ListGlobalSecurityAdvisories(translations.NullTranslationHelper) - tool.RegisterFunc(server, deps) - - serverTransport, clientTransport := mcp.NewInMemoryTransports() - serverSession, err := server.Connect(context.Background(), serverTransport, nil) - require.NoError(t, err) - t.Cleanup(func() { _ = serverSession.Close() }) - mcpClient := mcp.NewClient(&mcp.Implementation{Name: "test-client", Version: "v0.0.1"}, nil) - clientSession, err := mcpClient.Connect(context.Background(), clientTransport, nil) - require.NoError(t, err) - t.Cleanup(func() { _ = clientSession.Close() }) - - result, err := clientSession.CallTool(context.Background(), &mcp.CallToolParams{ - Name: "list_global_security_advisories", - Arguments: map[string]any{"ecosystem": "swift"}, - }) - require.NoError(t, err) - require.False(t, result.IsError, "unexpected error result: %v", result.Content) - assert.Equal(t, "swift", gotEcosystem) -} - func Test_GetGlobalSecurityAdvisory(t *testing.T) { toolDef := GetGlobalSecurityAdvisory(translations.NullTranslationHelper) tool := toolDef.Tool @@ -173,11 +138,13 @@ func Test_GetGlobalSecurityAdvisory(t *testing.T) { schema, ok := tool.InputSchema.(*jsonschema.Schema) require.True(t, ok, "InputSchema should be of type *jsonschema.Schema") assert.Contains(t, schema.Properties, "ghsaId") - assert.ElementsMatch(t, schema.Required, []string{"ghsaId"}) + assert.Contains(t, schema.Properties, "cveId") + assert.Empty(t, schema.Required, "ghsaId and cveId are alternatives, so neither is required on its own") // Setup mock advisory for success case mockAdvisory := &github.GlobalSecurityAdvisory{ GHSAID: new("GHSA-xxxx-xxxx-xxxx"), + CVEID: new("CVE-2026-0001"), Summary: new("Test advisory"), Description: new("This is a test advisory."), Severity: new("high"), @@ -188,6 +155,7 @@ func Test_GetGlobalSecurityAdvisory(t *testing.T) { mockedClient *http.Client requestArgs map[string]any expectError bool + expectToolError bool expectedAdvisory *github.GlobalSecurityAdvisory expectedErrMsg string }{ @@ -230,6 +198,62 @@ func Test_GetGlobalSecurityAdvisory(t *testing.T) { expectError: true, expectedErrMsg: "failed to get advisory", }, + { + name: "successful advisory fetch by cveId", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetAdvisories: func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "CVE-2026-0001", r.URL.Query().Get("cve_id"), "the CVE ID must reach the API as a filter") + mockResponse(t, http.StatusOK, []*github.GlobalSecurityAdvisory{mockAdvisory})(w, r) + }, + }), + requestArgs: map[string]any{ + "cveId": "CVE-2026-0001", + }, + expectError: false, + expectedAdvisory: mockAdvisory, + }, + { + name: "cveId matching no advisory", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetAdvisories: mockResponse(t, http.StatusOK, []*github.GlobalSecurityAdvisory{}), + }), + requestArgs: map[string]any{ + "cveId": "CVE-2026-9999", + }, + expectToolError: true, + expectedErrMsg: "no global security advisory found for CVE-2026-9999", + }, + { + name: "cveId matching several advisories", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ + GetAdvisories: mockResponse(t, http.StatusOK, []*github.GlobalSecurityAdvisory{ + mockAdvisory, + {GHSAID: new("GHSA-yyyy-yyyy-yyyy"), CVEID: new("CVE-2026-0001")}, + }), + }), + requestArgs: map[string]any{ + "cveId": "CVE-2026-0001", + }, + expectToolError: true, + expectedErrMsg: "matches 2 global security advisories (GHSA-xxxx-xxxx-xxxx, GHSA-yyyy-yyyy-yyyy)", + }, + { + name: "neither identifier provided", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}), + requestArgs: map[string]any{}, + expectToolError: true, + expectedErrMsg: "provide either ghsaId or cveId", + }, + { + name: "both identifiers provided", + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}), + requestArgs: map[string]any{ + "ghsaId": "GHSA-xxxx-xxxx-xxxx", + "cveId": "CVE-2026-0001", + }, + expectToolError: true, + expectedErrMsg: "provide only one of ghsaId or cveId", + }, } for _, tc := range tests { @@ -254,6 +278,12 @@ func Test_GetGlobalSecurityAdvisory(t *testing.T) { require.NoError(t, err) + if tc.expectToolError { + textContent := getErrorResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + // Parse the result and get the text content if no error textContent := getTextResult(t, result) diff --git a/pkg/github/security_outputs.go b/pkg/github/security_outputs.go index 9788b4e600..7ce9a3fbe6 100644 --- a/pkg/github/security_outputs.go +++ b/pkg/github/security_outputs.go @@ -18,28 +18,12 @@ type GetCodeQualityFindingInput struct { FindingNumber int `json:"findingNumber"` } -type ListCodeQualityFindingsInput struct { - Owner string `json:"owner"` - Repo string `json:"repo"` - State string `json:"state,omitempty"` - Direction string `json:"direction,omitempty"` - PerPage *int `json:"perPage,omitempty"` - After string `json:"after,omitempty"` -} - type GetSecurityAlertInput struct { Owner string `json:"owner"` Repo string `json:"repo"` AlertNumber int `json:"alertNumber"` } -type GetSecretScanningAlertInput struct { - Owner string `json:"owner"` - Repo string `json:"repo"` - AlertNumber int `json:"alertNumber"` - HideSecret *bool `json:"hide_secret,omitempty"` -} - type ListCodeScanningAlertsInput struct { Owner string `json:"owner"` Repo string `json:"repo"` @@ -57,7 +41,6 @@ type ListSecretScanningAlertsInput struct { State string `json:"state,omitempty"` SecretType string `json:"secret_type,omitempty"` Resolution string `json:"resolution,omitempty"` - HideSecret *bool `json:"hide_secret,omitempty"` Page *int `json:"page,omitempty"` PerPage *int `json:"perPage,omitempty"` } @@ -94,7 +77,8 @@ type ListRepositorySecurityAdvisoriesInput struct { } type GetGlobalSecurityAdvisoryInput struct { - GHSAID string `json:"ghsaId"` + GHSAID string `json:"ghsaId,omitempty"` + CVEID string `json:"cveId,omitempty"` } type ListOrgRepositorySecurityAdvisoriesInput struct { @@ -271,11 +255,6 @@ type SecurityPageInfo struct { PrevCursor string `json:"prevCursor,omitempty"` } -type CodeQualityFindingsOutput struct { - Findings []*CodeQualityFindingOutput `json:"findings"` - PageInfo SecurityPageInfo `json:"pageInfo"` -} - type DependabotAlertsOutput struct { Alerts []*DependabotAlertOutput `json:"alerts"` PageInfo SecurityPageInfo `json:"pageInfo"` @@ -323,7 +302,7 @@ func normalizeSecurityIntegerArguments(fields ...string) inventory.InputNormaliz } parsed, err := strconv.ParseFloat(number, 64) if err != nil || math.IsNaN(parsed) || math.IsInf(parsed, 0) || - parsed != math.Trunc(parsed) || !inSignedIntRange(parsed, strconv.IntSize) { + parsed != math.Trunc(parsed) || parsed > float64(math.MaxInt) || parsed < float64(math.MinInt) { return nil, fmt.Errorf("parameter %s is not a valid number", field) } arguments[field], err = json.Marshal(int(parsed))