|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/github/github-mcp-server/pkg/inventory" |
| 7 | + "github.com/github/github-mcp-server/pkg/scopes" |
| 8 | + "github.com/github/github-mcp-server/pkg/translations" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// TestPublicRepoWriteToolsDeclareLeastPrivilegeScopes asserts that the public |
| 14 | +// repository write tools advertise `public_repo` as their required scope while |
| 15 | +// still accepting a full `repo` token (via the scope hierarchy). This enables |
| 16 | +// least-privilege, public-only OAuth deployments instead of forcing the broad |
| 17 | +// `repo` scope, which also grants private-repository access. |
| 18 | +// See https://github.com/github/github-mcp-server/issues/3136 |
| 19 | +func TestPublicRepoWriteToolsDeclareLeastPrivilegeScopes(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + builders := map[string]func(translations.TranslationHelperFunc) *inventory.ServerTool{ |
| 23 | + "add_issue_comment": wrapToolBuilder(AddIssueComment), |
| 24 | + "issue_write": wrapToolBuilder(IssueWrite), |
| 25 | + "create_branch": wrapToolBuilder(CreateBranch), |
| 26 | + "push_files": wrapToolBuilder(PushFiles), |
| 27 | + "create_pull_request": wrapToolBuilder(CreatePullRequest), |
| 28 | + "fork_repository": wrapToolBuilder(ForkRepository), |
| 29 | + } |
| 30 | + |
| 31 | + for name, build := range builders { |
| 32 | + t.Run(name, func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + |
| 35 | + st := build(translations.NullTranslationHelper) |
| 36 | + |
| 37 | + require.NotNil(t, st.ScopeAccess.Challenge) |
| 38 | + args := map[string]any(nil) |
| 39 | + wantScopes := []string{string(scopes.PublicRepo)} |
| 40 | + if name == "push_files" { |
| 41 | + args = map[string]any{"files": []any{map[string]any{"path": "README.md"}}} |
| 42 | + wantScopes = append(wantScopes, string(scopes.Workflow)) |
| 43 | + } |
| 44 | + assert.Equal(t, wantScopes, st.ScopeAccess.Scopes, |
| 45 | + "%s should expose the least-privilege scope upper bound", name) |
| 46 | + assert.Empty(t, st.ScopeAccess.Challenge(args, []string{string(scopes.PublicRepo)}), |
| 47 | + "%s should accept a public_repo token", name) |
| 48 | + assert.Empty(t, st.ScopeAccess.Challenge(args, []string{string(scopes.Repo)}), |
| 49 | + "%s should accept a parent repo token", name) |
| 50 | + if name == "push_files" { |
| 51 | + workflowArgs := map[string]any{"files": []any{map[string]any{"path": ".github/workflows/ci.yml"}}} |
| 52 | + assert.Equal(t, []string{string(scopes.PublicRepo), string(scopes.Workflow)}, |
| 53 | + st.ScopeAccess.Challenge(workflowArgs, []string{string(scopes.PublicRepo)}), |
| 54 | + "push_files should additionally challenge for workflow when needed") |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestPublicRepoWriteToolsVisibleToPublicRepoToken asserts the PAT tool filter |
| 61 | +// shows these tools to a public_repo-only token and keeps them visible for a |
| 62 | +// full repo token. |
| 63 | +func TestPublicRepoWriteToolsVisibleToPublicRepoToken(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + publicRepoToken := []string{string(scopes.PublicRepo)} |
| 67 | + repoToken := []string{string(scopes.Repo)} |
| 68 | + |
| 69 | + filterForPublicToken := CreateToolScopeFilter(publicRepoToken) |
| 70 | + filterForRepoToken := CreateToolScopeFilter(repoToken) |
| 71 | + |
| 72 | + builders := map[string]func(translations.TranslationHelperFunc) *inventory.ServerTool{ |
| 73 | + "add_issue_comment": wrapToolBuilder(AddIssueComment), |
| 74 | + "issue_write": wrapToolBuilder(IssueWrite), |
| 75 | + "create_branch": wrapToolBuilder(CreateBranch), |
| 76 | + "push_files": wrapToolBuilder(PushFiles), |
| 77 | + "create_pull_request": wrapToolBuilder(CreatePullRequest), |
| 78 | + "fork_repository": wrapToolBuilder(ForkRepository), |
| 79 | + } |
| 80 | + |
| 81 | + for name, build := range builders { |
| 82 | + t.Run(name, func(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + st := build(translations.NullTranslationHelper) |
| 86 | + |
| 87 | + allowed, err := filterForPublicToken(t.Context(), st) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.True(t, allowed, "%s should be visible with a public_repo-only token", name) |
| 90 | + |
| 91 | + allowed, err = filterForRepoToken(t.Context(), st) |
| 92 | + require.NoError(t, err) |
| 93 | + assert.True(t, allowed, "%s should remain visible with a full repo token", name) |
| 94 | + }) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func wrapToolBuilder(fn func(translations.TranslationHelperFunc) inventory.ServerTool) func(translations.TranslationHelperFunc) *inventory.ServerTool { |
| 99 | + return func(t translations.TranslationHelperFunc) *inventory.ServerTool { |
| 100 | + st := fn(t) |
| 101 | + return &st |
| 102 | + } |
| 103 | +} |
0 commit comments