Files
github--github-mcp-server/pkg/github/feature_flags_test.go
JoannaaKL fb7cbc8b85 Annotate read tools with ifc labels (#2671)
* Annotate read tools with ifc labels

* Dont automatically enable IFCLabels in insiders mode

* ifc: don't label unpublished repo advisories as public

Repository security advisory listings can include draft/triage/closed
advisories (via the state filter), which are not world-readable even on a
public repository. Deriving confidentiality from repo visibility alone
under-classified those results as public.

LabelRepositorySecurityAdvisory now takes an allPublished flag and only
returns a public label when the repo is public AND every returned advisory
is published; otherwise it is private. list_repository_security_advisories
computes allPublished from the response state; the org-wide listing stays
private-untrusted. Adds unit + handler regression tests covering the
draft-advisory-on-public-repo case.

Addresses PR review feedback.

* ifc: fix confidentiality under-classification in releases, collaborators, get_me

Audit for the same bug class as the repo-advisory fix (confidentiality
derived from a coarse signal that misses access-restricted items) found
three more under-classifications:

- Releases (list_releases, get_latest_release, get_release_by_tag): draft
  releases are visible only to push-access users and are not world-readable
  even on a public repo. New LabelRelease(isPrivate, hasDraft) returns public
  only for a non-draft release on a public repo; handlers compute hasDraft
  from the response (Draft flag / per-item scan).
- list_repository_collaborators: a collaborator roster requires push access
  to list, so it is never world-readable, not even on a public repo. New
  LabelCollaboratorRoster() is always PrivateTrusted (mirrors LabelTeam),
  replacing the repo-visibility-derived label.
- get_me: the result includes private_gists / total_private_repos /
  owned_private_repos, which are not part of the public profile. LabelGetMe
  is now PrivateTrusted instead of PublicTrusted.

Verified the remaining public-capable labels are sound: Actions logs are
world-readable on public repos; branches/tags are public metadata; gist,
project, search, and starred-repo labels read per-item visibility and join.

Adds ifc unit tests for the new/changed labels and a get_release_by_tag
handler regression test (draft on public repo -> private); updates the
get_me handler test to assert private.

* ifc: document why list results use one joined label, not per-item

Explain on LabelSearchIssues (and cross-ref from LabelGistList) that a tool
result is delivered as one opaque payload and the IFC engine makes one
allow/deny decision per flow at egress, so the only sound bound for a list is
the meet of every item's label. Per-item labels would only be load-bearing if
the engine could partition a result and route items to different sinks; until
then they would invite unsafe declassification of a public item that arrived
alongside private data. Doc-only change.
2026-06-11 13:48:36 +02:00

219 lines
6.4 KiB
Go

package github
import (
"context"
"encoding/json"
"testing"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/github/github-mcp-server/pkg/inventory"
"github.com/github/github-mcp-server/pkg/scopes"
"github.com/github/github-mcp-server/pkg/utils"
)
// RemoteMCPEnthusiasticGreeting is a dummy test feature flag .
const RemoteMCPEnthusiasticGreeting = "remote_mcp_enthusiastic_greeting"
func featureCheckerFor(enabledFlags ...string) func(context.Context, string) (bool, error) {
enabled := make(map[string]bool, len(enabledFlags))
for _, flag := range enabledFlags {
enabled[flag] = true
}
return func(_ context.Context, flagName string) (bool, error) {
return enabled[flagName], nil
}
}
// HelloWorld returns a simple greeting tool that demonstrates feature flag conditional behavior.
// This tool is for testing and demonstration purposes only.
func HelloWorldTool(t translations.TranslationHelperFunc) inventory.ServerTool {
return NewTool(
ToolsetMetadataContext, // Use existing "context" toolset
mcp.Tool{
Name: "hello_world",
Description: t("TOOL_HELLO_WORLD_DESCRIPTION", "A simple greeting tool that demonstrates feature flag conditional behavior"),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_HELLO_WORLD_TITLE", "Hello World"),
ReadOnlyHint: true,
},
},
[]scopes.Scope{},
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, _ map[string]any) (*mcp.CallToolResult, any, error) {
// Check feature flag to determine greeting style
greeting := "Hello, world!"
if deps.IsFeatureEnabled(ctx, RemoteMCPEnthusiasticGreeting) {
greeting += " Welcome to the future of MCP! 🎉"
}
// Build response
response := map[string]any{
"greeting": greeting,
}
jsonBytes, err := json.Marshal(response)
if err != nil {
return utils.NewToolResultError("failed to marshal response"), nil, nil
}
return utils.NewToolResultText(string(jsonBytes)), nil, nil
},
)
}
func TestHelloWorld_ConditionalBehavior_Featureflag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
featureFlagEnabled bool
inputName string
expectedGreeting string
}{
{
name: "Feature flag disabled - default greeting",
featureFlagEnabled: false,
expectedGreeting: "Hello, world!",
},
{
name: "Feature flag enabled - enthusiastic greeting",
featureFlagEnabled: true,
expectedGreeting: "Hello, world! Welcome to the future of MCP! 🎉",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var enabledFlags []string
if tt.featureFlagEnabled {
enabledFlags = append(enabledFlags, RemoteMCPEnthusiasticGreeting)
}
// Create deps with the checker
deps := NewBaseDeps(
nil, nil, nil, nil,
translations.NullTranslationHelper,
FeatureFlags{},
0,
featureCheckerFor(enabledFlags...),
stubExporters(),
)
// Get the tool and its handler
tool := HelloWorldTool(translations.NullTranslationHelper)
handler := tool.Handler(deps)
// Call the handler with deps in context
ctx := ContextWithDeps(context.Background(), deps)
result, err := handler(ctx, &mcp.CallToolRequest{
Params: &mcp.CallToolParamsRaw{
Arguments: json.RawMessage(`{}`),
},
})
require.NoError(t, err)
require.NotNil(t, result)
require.Len(t, result.Content, 1)
// Parse the response - should be TextContent
textContent, ok := result.Content[0].(*mcp.TextContent)
require.True(t, ok, "expected content to be TextContent")
var response map[string]any
err = json.Unmarshal([]byte(textContent.Text), &response)
require.NoError(t, err)
// Verify the greeting matches expected based on feature flag
assert.Equal(t, tt.expectedGreeting, response["greeting"])
})
}
}
func TestResolveFeatureFlags(t *testing.T) {
t.Parallel()
tests := []struct {
name string
enabledFeatures []string
insidersMode bool
expectedFlags []string
unexpectedFlags []string
}{
{
name: "no features, no insiders",
enabledFeatures: nil,
expectedFlags: nil,
unexpectedFlags: []string{MCPAppsFeatureFlag},
},
{
name: "explicit feature enabled",
enabledFeatures: []string{MCPAppsFeatureFlag},
expectedFlags: []string{MCPAppsFeatureFlag},
},
{
name: "insiders mode enables insiders flags",
enabledFeatures: nil,
insidersMode: true,
expectedFlags: InsidersFeatureFlags,
},
{
name: "insiders mode does not auto-enable ifc labels",
enabledFeatures: nil,
insidersMode: true,
unexpectedFlags: []string{FeatureFlagIFCLabels},
},
{
name: "ifc_labels can be directly enabled",
enabledFeatures: []string{FeatureFlagIFCLabels},
expectedFlags: []string{FeatureFlagIFCLabels},
},
{
name: "unknown flags are filtered out",
enabledFeatures: []string{"unknown_flag", "another_unknown"},
unexpectedFlags: []string{"unknown_flag", "another_unknown"},
},
{
name: "mix of known and unknown flags",
enabledFeatures: []string{MCPAppsFeatureFlag, "unknown_flag"},
expectedFlags: []string{MCPAppsFeatureFlag},
unexpectedFlags: []string{"unknown_flag"},
},
{
name: "user-only flags can be enabled but are not turned on by insiders",
enabledFeatures: []string{FeatureFlagIssuesGranular},
insidersMode: false,
expectedFlags: []string{FeatureFlagIssuesGranular},
},
{
name: "insiders does not enable user-only allowed flags",
enabledFeatures: nil,
insidersMode: true,
unexpectedFlags: []string{FeatureFlagIssuesGranular, FeatureFlagPullRequestsGranular},
},
{
name: "explicit plus insiders deduplicates",
enabledFeatures: []string{MCPAppsFeatureFlag},
insidersMode: true,
expectedFlags: InsidersFeatureFlags,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := ResolveFeatureFlags(tt.enabledFeatures, tt.insidersMode)
for _, flag := range tt.expectedFlags {
assert.True(t, result[flag], "expected flag %q to be enabled", flag)
}
for _, flag := range tt.unexpectedFlags {
assert.False(t, result[flag], "expected flag %q to not be enabled", flag)
}
})
}
}