Skip to content

Commit c58fba9

Browse files
guillep2kzeripath
authored andcommitted
Fix permission checks for close/reopen from commit (#8875)
* Fix checks for close/reopen from commit * Fix permission order
1 parent 7719009 commit c58fba9

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

models/action.go

+29-16
Original file line numberDiff line numberDiff line change
@@ -491,32 +491,45 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
491491
}
492492
refMarked[key] = true
493493

494-
// only create comments for issues if user has permission for it
495-
if perm.IsAdmin() || perm.IsOwner() || perm.CanWrite(UnitTypeIssues) {
496-
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, html.EscapeString(c.Message))
497-
if err = CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
498-
return err
499-
}
494+
// FIXME: this kind of condition is all over the code, it should be consolidated in a single place
495+
canclose := perm.IsAdmin() || perm.IsOwner() || perm.CanWrite(UnitTypeIssues) || refIssue.PosterID == doer.ID
496+
cancomment := canclose || perm.CanRead(UnitTypeIssues)
497+
498+
// Don't proceed if the user can't comment
499+
if !cancomment {
500+
continue
500501
}
501502

502-
// Process closing/reopening keywords
503-
if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
503+
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, repo.Link(), c.Sha1, html.EscapeString(c.Message))
504+
if err = CreateRefComment(doer, refRepo, refIssue, message, c.Sha1); err != nil {
505+
return err
506+
}
507+
508+
// Only issues can be closed/reopened this way, and user needs the correct permissions
509+
if refIssue.IsPull || !canclose {
504510
continue
505511
}
506512

507-
// Change issue status only if the commit has been pushed to the default branch.
508-
// and if the repo is configured to allow only that
509-
// FIXME: we should be using Issue.ref if set instead of repo.DefaultBranch
510-
if repo.DefaultBranch != branchName && !repo.CloseIssuesViaCommitInAnyBranch {
513+
// Only process closing/reopening keywords
514+
if ref.Action != references.XRefActionCloses && ref.Action != references.XRefActionReopens {
511515
continue
512516
}
513517

514-
// only close issues in another repo if user has push access
515-
if perm.IsAdmin() || perm.IsOwner() || perm.CanWrite(UnitTypeCode) {
516-
if err := changeIssueStatus(refRepo, refIssue, doer, ref.Action == references.XRefActionCloses); err != nil {
517-
return err
518+
if !repo.CloseIssuesViaCommitInAnyBranch {
519+
// If the issue was specified to be in a particular branch, don't allow commits in other branches to close it
520+
if refIssue.Ref != "" {
521+
if branchName != refIssue.Ref {
522+
continue
523+
}
524+
// Otherwise, only process commits to the default branch
525+
} else if branchName != repo.DefaultBranch {
526+
continue
518527
}
519528
}
529+
530+
if err := changeIssueStatus(refRepo, refIssue, doer, ref.Action == references.XRefActionCloses); err != nil {
531+
return err
532+
}
520533
}
521534
}
522535
return nil

models/action_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestUpdateIssuesCommit(t *testing.T) {
166166
PosterID: user.ID,
167167
IssueID: 1,
168168
}
169-
issueBean := &Issue{RepoID: repo.ID, Index: 2}
169+
issueBean := &Issue{RepoID: repo.ID, Index: 4}
170170

171171
AssertNotExistsBean(t, commentBean)
172172
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
@@ -220,7 +220,7 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) {
220220
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
221221
repo.Owner = user
222222

223-
issueBean := &Issue{RepoID: repo.ID, Index: 2}
223+
issueBean := &Issue{RepoID: repo.ID, Index: 4}
224224

225225
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
226226
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))

0 commit comments

Comments
 (0)