diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 619ad3a5615..397c6636b3c 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -377,26 +377,12 @@ func ListIssues(ctx *context.APIContext) { listOptions := utils.GetListOptions(ctx) - isPull := common.ParseIssueFilterTypeIsPull(ctx.FormString("type")) - - if isPull.Has() && !ctx.Repo.Permission.CanReadIssuesOrPulls(isPull.Value()) { - ctx.APIErrorNotFound() + isPull, ok := common.ParseIssueFilterTypeIsPullWithPermission(ctx.Repo.Permission, ctx.FormString("type")) + if !ok { + ctx.APIError(http.StatusForbidden, "no permission to read issues or pulls") return } - if !isPull.Has() { - canReadIssues := ctx.Repo.Permission.CanRead(unit.TypeIssues) - canReadPulls := ctx.Repo.Permission.CanRead(unit.TypePullRequests) - if !canReadIssues && !canReadPulls { - ctx.APIErrorNotFound() - return - } else if !canReadIssues { - isPull = optional.Some(true) - } else if !canReadPulls { - isPull = optional.Some(false) - } - } - // FIXME: we should be more efficient here createdByID := getUserIDForFilter(ctx, "created_by") if ctx.Written() { diff --git a/routers/common/issue_filter.go b/routers/common/issue_filter.go index b11d5bc9b16..6af542874bd 100644 --- a/routers/common/issue_filter.go +++ b/routers/common/issue_filter.go @@ -7,7 +7,9 @@ import ( "context" "gitea.dev/models/organization" + access_model "gitea.dev/models/perm/access" repo_model "gitea.dev/models/repo" + "gitea.dev/models/unit" user_model "gitea.dev/models/user" "gitea.dev/modules/optional" "gitea.dev/modules/util" @@ -30,6 +32,32 @@ func ParseIssueFilterTypeIsPull(typ string) optional.Option[bool] { return optional.FromMapLookup(map[string]bool{"pulls": true, "issues": false}, typ) } +func ParseIssueFilterTypeIsPullWithPermission(userPerm access_model.Permission, typ string) (optional.Option[bool], bool) { + typeIssues, typePulls := "issues", "pulls" + canReadIssues := userPerm.CanRead(unit.TypeIssues) + canReadPulls := userPerm.CanRead(unit.TypePullRequests) + switch { + case canReadIssues && canReadPulls: + return ParseIssueFilterTypeIsPull(typ), true + case typ == typeIssues && canReadIssues: + return optional.Some(false), true + case typ == typePulls && canReadPulls: + return optional.Some(true), true + default: + isPull := ParseIssueFilterTypeIsPull(typ) + if isPull.Has() { + if (isPull.Value() && canReadPulls) || (!isPull.Value() && canReadIssues) { + // explicitly specified a type that the user can read, so return it + return isPull, true + } + } else if canReadIssues || canReadPulls { + // no type specified, so return the type the user can read + return optional.Some(canReadPulls), true + } + return optional.None[bool](), false + } +} + type SearchIssuesRepoIDsOptions struct { Doer *user_model.User PublicOnly bool diff --git a/routers/web/repo/issue_list.go b/routers/web/repo/issue_list.go index 3bda048a1d6..d055b536566 100644 --- a/routers/web/repo/issue_list.go +++ b/routers/web/repo/issue_list.go @@ -43,8 +43,8 @@ func parseProjectIDsFromQuery(ctx *context.Context) []int64 { return ctx.FormStringInt64s("project") } -// SearchIssues searches for issues across the repositories that the user has access to -func SearchIssues(ctx *context.Context) { +// SearchGlobalIssues searches for issues across the repositories that the user has access to +func SearchGlobalIssues(ctx *context.Context) { before, since, err := context.GetQueryBeforeSince(ctx.Base) if err != nil { ctx.HTTPError(http.StatusUnprocessableEntity, err.Error()) @@ -233,7 +233,11 @@ func SearchRepoIssuesJSON(ctx *context.Context) { } } - isPull := common.ParseIssueFilterTypeIsPull(ctx.FormString("type")) + isPull, ok := common.ParseIssueFilterTypeIsPullWithPermission(ctx.Repo.Permission, ctx.FormString("type")) + if !ok { + ctx.HTTPError(http.StatusForbidden, "no permission to read issues or pulls") + return + } // FIXME: we should be more efficient here createdByID := getUserIDForFilter(ctx, "created_by") diff --git a/routers/web/repo/issue_suggestions.go b/routers/web/repo/issue_suggestions.go index f5c3042faff..c136bc776a4 100644 --- a/routers/web/repo/issue_suggestions.go +++ b/routers/web/repo/issue_suggestions.go @@ -6,8 +6,7 @@ package repo import ( "net/http" - "gitea.dev/models/unit" - "gitea.dev/modules/optional" + "gitea.dev/routers/common" "gitea.dev/services/context" issue_service "gitea.dev/services/issue" ) @@ -16,14 +15,10 @@ import ( func IssueSuggestions(ctx *context.Context) { keyword := ctx.Req.FormValue("q") - canReadIssues := ctx.Repo.Permission.CanRead(unit.TypeIssues) - canReadPulls := ctx.Repo.Permission.CanRead(unit.TypePullRequests) - - var isPull optional.Option[bool] - if canReadPulls && !canReadIssues { - isPull = optional.Some(true) - } else if canReadIssues && !canReadPulls { - isPull = optional.Some(false) + isPull, ok := common.ParseIssueFilterTypeIsPullWithPermission(ctx.Repo.Permission, "") + if !ok { + ctx.HTTPError(http.StatusForbidden, "no permission to read issues or pulls") + return } suggestions, err := issue_service.GetSuggestion(ctx, ctx.Repo.Repository, isPull, keyword) diff --git a/routers/web/repo/issue_test.go b/routers/web/repo/issue_test.go index 9db2a73ca78..c68f80756cd 100644 --- a/routers/web/repo/issue_test.go +++ b/routers/web/repo/issue_test.go @@ -4,12 +4,24 @@ package repo import ( + "net/http" + "net/http/httptest" "testing" issues_model "gitea.dev/models/issues" + "gitea.dev/models/perm" + access_model "gitea.dev/models/perm/access" + repo_model "gitea.dev/models/repo" + "gitea.dev/models/unit" + "gitea.dev/models/unittest" + "gitea.dev/modules/json" "gitea.dev/modules/references" + api "gitea.dev/modules/structs" + "gitea.dev/services/context" + "gitea.dev/services/contexttest" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCombineXRefComments(t *testing.T) { @@ -397,3 +409,59 @@ func TestCombineLabelComments(t *testing.T) { }) } } + +func TestIssueListsRespectUnitReadPermission(t *testing.T) { + unittest.PrepareTestEnv(t) + + // user2/repo1 with read access to issues only, so pull requests must stay hidden + issuesOnlyContext := func(path string) (*context.Context, *httptest.ResponseRecorder) { + ctx, resp := contexttest.MockContext(t, path) + contexttest.LoadUser(t, ctx, 2) + contexttest.LoadRepo(t, ctx, 1) + ctx.Repo.Permission = access_model.Permission{} + ctx.Repo.Permission.SetUnitsWithDefaultAccessMode([]*repo_model.RepoUnit{{Type: unit.TypeIssues}}, perm.AccessModeRead) + return ctx, resp + } + + t.Run("SearchRepoIssuesJSON", func(t *testing.T) { + search := func(typ string) (int, []*api.Issue) { + ctx, resp := issuesOnlyContext("user2/repo1/issues/search") + ctx.Req.Form.Set("state", "all") + ctx.Req.Form.Set("type", typ) + SearchRepoIssuesJSON(ctx) + var issues []*api.Issue + if resp.Code == http.StatusOK { + require.NoError(t, json.Unmarshal(resp.Body.Bytes(), &issues)) + } + return resp.Code, issues + } + code, _ := search("pulls") + assert.Equal(t, http.StatusForbidden, code) + + code, issues := search("") + assert.Equal(t, http.StatusOK, code) + assert.Len(t, issues, 2) + for _, issue := range issues { + assert.Nil(t, issue.PullRequest, "pull request #%d listed", issue.Index) + } + }) + + t.Run("MilestoneIssuesAndPulls", func(t *testing.T) { + // milestone1 of repo1 only holds pull request #2 + view := func(issuesOnly bool) issues_model.IssueList { + ctx, resp := issuesOnlyContext("user2/repo1/milestone/1") + if !issuesOnly { + contexttest.LoadRepo(t, ctx, 1) // restores the owner permission of user2 + } + contexttest.LoadGitRepo(t, ctx) + defer ctx.Repo.GitRepo.Close() + ctx.SetPathParam("id", "1") + MilestoneIssuesAndPulls(ctx) + assert.Equal(t, http.StatusOK, resp.Code) + issues, _ := ctx.Data["Issues"].(issues_model.IssueList) + return issues + } + assert.Len(t, view(false), 1) + assert.Empty(t, view(true)) + }) +} diff --git a/routers/web/repo/milestone.go b/routers/web/repo/milestone.go index 3c16f1272a0..5a8bfb6cb33 100644 --- a/routers/web/repo/milestone.go +++ b/routers/web/repo/milestone.go @@ -259,7 +259,12 @@ func MilestoneIssuesAndPulls(ctx *context.Context) { ctx.Data["Title"] = milestone.Name ctx.Data["Milestone"] = milestone - prepareIssueFilterAndList(ctx, milestoneID, projectIDs, optional.None[bool]()) + isPull, ok := common.ParseIssueFilterTypeIsPullWithPermission(ctx.Repo.Permission, ctx.FormString("type")) + if !ok { + ctx.HTTPError(http.StatusForbidden, "no permission to read issues or pulls") + return + } + prepareIssueFilterAndList(ctx, milestoneID, projectIDs, isPull) ret := issue.ParseTemplatesFromDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo) ctx.Data["NewIssueChooseTemplate"] = len(ret.IssueTemplates) > 0 diff --git a/routers/web/web.go b/routers/web/web.go index 694726f773d..67b3caa8516 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -558,7 +558,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) { m.Group("/issues", func() { m.Get("", user.Issues) - m.Get("/search", repo.SearchIssues) + m.Get("/search", repo.SearchGlobalIssues) }, reqSignIn) m.Get("/pulls", reqSignIn, user.Pulls)