Skip to content

Commit 184a7d4

Browse files
lunnyGustedKN4CK3R6543
authored
Check if project has the same repository id with issue when assign project to issue (#20133)
* Check if project has the same repository id with issue when assign project to issue * Check if issue's repository id match project's repository id * Add more permission checking * Remove invalid argument * Fix errors * Add generic check * Remove duplicated check * Return error + add check for new issues * Apply suggestions from code review Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: Gusted <[email protected]> Co-authored-by: KN4CK3R <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent db3355c commit 184a7d4

File tree

9 files changed

+68
-11
lines changed

9 files changed

+68
-11
lines changed

models/issues/issue_project.go

+11
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ func ChangeProjectAssign(issue *Issue, doer *user_model.User, newProjectID int64
124124
func addUpdateIssueProject(ctx context.Context, issue *Issue, doer *user_model.User, newProjectID int64) error {
125125
oldProjectID := issue.projectID(ctx)
126126

127+
// Only check if we add a new project and not remove it.
128+
if newProjectID > 0 {
129+
newProject, err := project_model.GetProjectByID(ctx, newProjectID)
130+
if err != nil {
131+
return err
132+
}
133+
if newProject.RepoID != issue.RepoID {
134+
return fmt.Errorf("issue's repository is not the same as project's repository")
135+
}
136+
}
137+
127138
if _, err := db.GetEngine(ctx).Where("project_issue.issue_id=?", issue.ID).Delete(&project_model.ProjectIssue{}); err != nil {
128139
return err
129140
}

models/issues/milestone.go

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func NewMilestone(m *Milestone) (err error) {
124124
return committer.Commit()
125125
}
126126

127+
// HasMilestoneByRepoID returns if the milestone exists in the repository.
128+
func HasMilestoneByRepoID(ctx context.Context, repoID, id int64) (bool, error) {
129+
return db.GetEngine(ctx).ID(id).Where("repo_id=?", repoID).Exist(new(Milestone))
130+
}
131+
127132
// GetMilestoneByRepoID returns the milestone in a repository.
128133
func GetMilestoneByRepoID(ctx context.Context, repoID, id int64) (*Milestone, error) {
129134
m := new(Milestone)

routers/api/v1/repo/pull_review.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ func dismissReview(ctx *context.APIContext, msg string, isDismiss bool) {
886886
return
887887
}
888888

889-
_, err := pull_service.DismissReview(ctx, review.ID, msg, ctx.Doer, isDismiss)
889+
_, err := pull_service.DismissReview(ctx, review.ID, ctx.Repo.Repository.ID, msg, ctx.Doer, isDismiss)
890890
if err != nil {
891891
ctx.Error(http.StatusInternalServerError, "pull_service.DismissReview", err)
892892
return

routers/web/repo/issue.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ func NewIssue(ctx *context.Context) {
803803
body := ctx.FormString("body")
804804
ctx.Data["BodyQuery"] = body
805805

806-
ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanRead(unit.TypeProjects)
806+
isProjectsEnabled := ctx.Repo.CanRead(unit.TypeProjects)
807+
ctx.Data["IsProjectsEnabled"] = isProjectsEnabled
807808
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
808809
upload.AddUploadContext(ctx, "comment")
809810

@@ -819,7 +820,7 @@ func NewIssue(ctx *context.Context) {
819820
}
820821

821822
projectID := ctx.FormInt64("project")
822-
if projectID > 0 {
823+
if projectID > 0 && isProjectsEnabled {
823824
project, err := project_model.GetProjectByID(ctx, projectID)
824825
if err != nil {
825826
log.Error("GetProjectByID: %d: %v", projectID, err)
@@ -1043,6 +1044,11 @@ func NewIssuePost(ctx *context.Context) {
10431044
}
10441045

10451046
if projectID > 0 {
1047+
if !ctx.Repo.CanRead(unit.TypeProjects) {
1048+
// User must also be able to see the project.
1049+
ctx.Error(http.StatusBadRequest, "user hasn't permissions to read projects")
1050+
return
1051+
}
10461052
if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil {
10471053
ctx.ServerError("ChangeProjectAssign", err)
10481054
return
@@ -1783,6 +1789,10 @@ func getActionIssues(ctx *context.Context) []*issues_model.Issue {
17831789
issueUnitEnabled := ctx.Repo.CanRead(unit.TypeIssues)
17841790
prUnitEnabled := ctx.Repo.CanRead(unit.TypePullRequests)
17851791
for _, issue := range issues {
1792+
if issue.RepoID != ctx.Repo.Repository.ID {
1793+
ctx.NotFound("some issue's RepoID is incorrect", errors.New("some issue's RepoID is incorrect"))
1794+
return nil
1795+
}
17861796
if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
17871797
ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
17881798
return nil

routers/web/repo/projects.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package repo
66

77
import (
8+
"errors"
89
"fmt"
910
"net/http"
1011
"net/url"
@@ -633,10 +634,17 @@ func MoveIssues(ctx *context.Context) {
633634
}
634635

635636
if len(movedIssues) != len(form.Issues) {
636-
ctx.ServerError("IssuesNotFound", err)
637+
ctx.ServerError("some issues do not exist", errors.New("some issues do not exist"))
637638
return
638639
}
639640

641+
for _, issue := range movedIssues {
642+
if issue.RepoID != project.RepoID {
643+
ctx.ServerError("Some issue's repoID is not equal to project's repoID", errors.New("Some issue's repoID is not equal to project's repoID"))
644+
return
645+
}
646+
}
647+
640648
if err = project_model.MoveIssuesOnProjectBoard(board, sortedIssueIDs); err != nil {
641649
ctx.ServerError("MoveIssuesOnProjectBoard", err)
642650
return

routers/web/repo/pull_review.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package repo
66

77
import (
8+
"errors"
89
"fmt"
910
"net/http"
1011

@@ -118,6 +119,11 @@ func UpdateResolveConversation(ctx *context.Context) {
118119
return
119120
}
120121

122+
if comment.Issue.RepoID != ctx.Repo.Repository.ID {
123+
ctx.NotFound("comment's repoID is incorrect", errors.New("comment's repoID is incorrect"))
124+
return
125+
}
126+
121127
var permResult bool
122128
if permResult, err = issues_model.CanMarkConversation(comment.Issue, ctx.Doer); err != nil {
123129
ctx.ServerError("CanMarkConversation", err)
@@ -236,7 +242,7 @@ func SubmitReview(ctx *context.Context) {
236242
// DismissReview dismissing stale review by repo admin
237243
func DismissReview(ctx *context.Context) {
238244
form := web.GetForm(ctx).(*forms.DismissReviewForm)
239-
comm, err := pull_service.DismissReview(ctx, form.ReviewID, form.Message, ctx.Doer, true)
245+
comm, err := pull_service.DismissReview(ctx, form.ReviewID, ctx.Repo.Repository.ID, form.Message, ctx.Doer, true)
240246
if err != nil {
241247
ctx.ServerError("pull_service.DismissReview", err)
242248
return

routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ func RegisterRoutes(m *web.Route) {
901901

902902
m.Post("/labels", reqRepoIssuesOrPullsWriter, repo.UpdateIssueLabel)
903903
m.Post("/milestone", reqRepoIssuesOrPullsWriter, repo.UpdateIssueMilestone)
904-
m.Post("/projects", reqRepoIssuesOrPullsWriter, repo.UpdateIssueProject)
904+
m.Post("/projects", reqRepoIssuesOrPullsWriter, reqRepoProjectsReader, repo.UpdateIssueProject)
905905
m.Post("/assignee", reqRepoIssuesOrPullsWriter, repo.UpdateIssueAssignee)
906906
m.Post("/request_review", reqRepoIssuesOrPullsReader, repo.UpdatePullReviewRequest)
907907
m.Post("/dismiss_review", reqRepoAdmin, bindIgnErr(forms.DismissReviewForm{}), repo.DismissReview)

services/issue/milestone.go

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ import (
1515
)
1616

1717
func changeMilestoneAssign(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) error {
18+
// Only check if milestone exists if we don't remove it.
19+
if issue.MilestoneID > 0 {
20+
has, err := issues_model.HasMilestoneByRepoID(ctx, issue.RepoID, issue.MilestoneID)
21+
if err != nil {
22+
return fmt.Errorf("HasMilestoneByRepoID: %v", err)
23+
}
24+
if !has {
25+
return fmt.Errorf("HasMilestoneByRepoID: issue doesn't exist")
26+
}
27+
}
28+
1829
if err := issues_model.UpdateIssueCols(ctx, issue, "milestone_id"); err != nil {
1930
return err
2031
}

services/pull/review.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos
271271
}
272272

273273
// DismissReview dismissing stale review by repo admin
274-
func DismissReview(ctx context.Context, reviewID int64, message string, doer *user_model.User, isDismiss bool) (comment *issues_model.Comment, err error) {
274+
func DismissReview(ctx context.Context, reviewID, repoID int64, message string, doer *user_model.User, isDismiss bool) (comment *issues_model.Comment, err error) {
275275
review, err := issues_model.GetReviewByID(ctx, reviewID)
276276
if err != nil {
277277
return
@@ -281,6 +281,16 @@ func DismissReview(ctx context.Context, reviewID int64, message string, doer *us
281281
return nil, fmt.Errorf("not need to dismiss this review because it's type is not Approve or change request")
282282
}
283283

284+
// load data for notify
285+
if err = review.LoadAttributes(ctx); err != nil {
286+
return nil, err
287+
}
288+
289+
// Check if the review's repoID is the one we're currently expecting.
290+
if review.Issue.RepoID != repoID {
291+
return nil, fmt.Errorf("reviews's repository is not the same as the one we expect")
292+
}
293+
284294
if err = issues_model.DismissReview(review, isDismiss); err != nil {
285295
return
286296
}
@@ -289,10 +299,6 @@ func DismissReview(ctx context.Context, reviewID int64, message string, doer *us
289299
return nil, nil
290300
}
291301

292-
// load data for notify
293-
if err = review.LoadAttributes(ctx); err != nil {
294-
return
295-
}
296302
if err = review.Issue.LoadPullRequest(); err != nil {
297303
return
298304
}

0 commit comments

Comments
 (0)