Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict permission check on repositories and fix some problems #5314

Merged
merged 32 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0bf41c2
fix units permission problems
lunny Nov 10, 2018
15f80b9
fix some bugs and merge LoadUnits to repoAssignment
lunny Nov 11, 2018
40d552c
refactor permission struct and add some copyright heads
lunny Nov 11, 2018
d80fea2
remove unused codes
lunny Nov 11, 2018
fb4a2cb
fix routes units check
lunny Nov 11, 2018
a21bfde
improve permission check
lunny Nov 11, 2018
422ba40
add unit tests for permission
lunny Nov 12, 2018
dae595b
fix typo
lunny Nov 12, 2018
6bed0d4
fix tests
lunny Nov 12, 2018
d5ba3a0
fix some routes
lunny Nov 12, 2018
426980d
fix api permission check
lunny Nov 12, 2018
50d1287
improve permission check
lunny Nov 12, 2018
95d9a58
fix some permission check
lunny Nov 13, 2018
5df61b6
fix tests
lunny Nov 13, 2018
4ee6e1f
fix tests
lunny Nov 13, 2018
de04377
improve some permission check
lunny Nov 13, 2018
66fd8f3
fix some permission check
lunny Nov 14, 2018
11bde94
refactor AccessLevel
lunny Nov 17, 2018
ba60cc8
fix bug
lunny Nov 17, 2018
a978acc
fix tests
lunny Nov 17, 2018
d161315
fix tests
lunny Nov 17, 2018
e5e165c
fix tests
lunny Nov 17, 2018
9253015
fix AccessLevel
lunny Nov 18, 2018
2f65f7a
rename CanAccess
lunny Nov 18, 2018
962be78
fix tests
lunny Nov 18, 2018
9742d63
fix comment
lunny Nov 18, 2018
2db05db
fix bug
lunny Nov 18, 2018
3620109
add missing unit for test repos
lunny Nov 18, 2018
861b3b2
fix bug
lunny Nov 18, 2018
b677d04
rename some functions
lunny Nov 18, 2018
79365d8
fix routes check
lunny Nov 18, 2018
d54bc51
Merge branch 'master' into lunny/fix_units_permissions
lunny Nov 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improve some permission check
  • Loading branch information
lunny committed Nov 28, 2018
commit de0437737aba991709b88c9e5c9b737ff78e1277
11 changes: 10 additions & 1 deletion models/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
return p.CanWrite(UnitTypeIssues)
}

// CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
// returns true if isPull is false and user could read to issues
func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
if isPull {
return p.CanAccess(UnitTypePullRequests)
}
return p.CanAccess(UnitTypeIssues)
}

// GetUserRepoPermission returns the user permissions to the repository
func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
return getUserRepoPermission(x, repo, user)
Expand All @@ -96,7 +105,7 @@ func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permiss
return
}

// Admin has super access or user is the owner of the repository
// Admin or the owner has super access to the repository
if user.IsAdmin || user.ID == repo.OwnerID {
perm.AccessMode = AccessModeOwner
return
Expand Down
2 changes: 1 addition & 1 deletion modules/context/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// RequireRepoAdmin returns a macaron middleware for requiring repository admin permission
func RequireRepoAdmin() macaron.Handler {
return func(ctx *Context) {
if !ctx.IsSigned || (!ctx.Repo.IsAdmin() && !ctx.User.IsAdmin) {
if !ctx.IsSigned || !ctx.Repo.IsAdmin() {
ctx.NotFound(ctx.Req.RequestURI, nil)
return
}
Expand Down
44 changes: 33 additions & 11 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,11 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
return
}

if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
ctx.Error(403)
return
}

var attachments []string
if setting.AttachmentEnabled {
attachments = form.Files
Expand Down Expand Up @@ -1142,7 +1147,12 @@ func UpdateCommentContent(ctx *context.Context) {
return
}

if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
if err := comment.LoadIssue(); err != nil {
ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
return
}

if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
ctx.Error(403)
return
} else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
Expand Down Expand Up @@ -1176,7 +1186,12 @@ func DeleteComment(ctx *context.Context) {
return
}

if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) {
if err := comment.LoadIssue(); err != nil {
ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
return
}

if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
ctx.Error(403)
return
} else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
Expand Down Expand Up @@ -1419,6 +1434,11 @@ func ChangeIssueReaction(ctx *context.Context, form auth.ReactionForm) {
return
}

if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
ctx.Error(403)
return
}

if ctx.HasError() {
ctx.ServerError("ChangeIssueReaction", errors.New(ctx.GetErrMsg()))
return
Expand Down Expand Up @@ -1488,20 +1508,22 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
return
}

issue, err := models.GetIssueByID(comment.IssueID)
checkIssueRights(ctx, issue)
if ctx.Written() {
if err := comment.LoadIssue(); err != nil {
ctx.NotFoundOrServerError("LoadIssue", models.IsErrIssueNotExist, err)
return
}

if ctx.HasError() {
ctx.ServerError("ChangeCommentReaction", errors.New(ctx.GetErrMsg()))
if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(comment.Issue.IsPull)) {
ctx.Error(403)
return
} else if comment.Type != models.CommentTypeComment && comment.Type != models.CommentTypeCode {
ctx.Error(204)
return
}

switch ctx.Params(":action") {
case "react":
reaction, err := models.CreateCommentReaction(ctx.User, issue, comment, form.Content)
reaction, err := models.CreateCommentReaction(ctx.User, comment.Issue, comment, form.Content)
if err != nil {
log.Info("CreateCommentReaction: %s", err)
break
Expand All @@ -1513,9 +1535,9 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
break
}

log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID, reaction.ID)
log.Trace("Reaction for comment created: %d/%d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID, reaction.ID)
case "unreact":
if err := models.DeleteCommentReaction(ctx.User, issue, comment, form.Content); err != nil {
if err := models.DeleteCommentReaction(ctx.User, comment.Issue, comment, form.Content); err != nil {
ctx.ServerError("DeleteCommentReaction", err)
return
}
Expand All @@ -1527,7 +1549,7 @@ func ChangeCommentReaction(ctx *context.Context, form auth.ReactionForm) {
break
}

log.Trace("Reaction for comment removed: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
log.Trace("Reaction for comment removed: %d/%d/%d", ctx.Repo.Repository.ID, comment.Issue.ID, comment.ID)
default:
ctx.NotFound(fmt.Sprintf("Unknown action %s", ctx.Params(":action")), nil)
return
Expand Down
25 changes: 15 additions & 10 deletions routers/repo/issue_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,28 @@ import (
)

// IssueWatch sets issue watching
func IssueWatch(c *context.Context) {
watch, err := strconv.ParseBool(c.Req.PostForm.Get("watch"))
if err != nil {
c.ServerError("watch is not bool", err)
func IssueWatch(ctx *context.Context) {
issue := GetActionIssue(ctx)
if ctx.Written() {
return
}

issue := GetActionIssue(c)
if c.Written() {
if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
ctx.Error(403)
return
}

watch, err := strconv.ParseBool(ctx.Req.PostForm.Get("watch"))
if err != nil {
ctx.ServerError("watch is not bool", err)
return
}

if err := models.CreateOrUpdateIssueWatch(c.User.ID, issue.ID, watch); err != nil {
c.ServerError("CreateOrUpdateIssueWatch", err)
if err := models.CreateOrUpdateIssueWatch(ctx.User.ID, issue.ID, watch); err != nil {
ctx.ServerError("CreateOrUpdateIssueWatch", err)
return
}

url := fmt.Sprintf("%s/issues/%d", c.Repo.RepoLink, issue.Index)
c.Redirect(url, http.StatusSeeOther)
url := fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index)
ctx.Redirect(url, http.StatusSeeOther)
}
4 changes: 2 additions & 2 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("", repo.UpdateCommentContent)
m.Post("/delete", repo.DeleteComment)
m.Post("/reactions/:action", bindIgnErr(auth.ReactionForm{}), repo.ChangeCommentReaction)
}, reqRepoIssuesOrPullsReader)
})
m.Group("/labels", func() {
m.Post("/new", bindIgnErr(auth.CreateLabelForm{}), repo.NewLabel)
m.Post("/edit", bindIgnErr(auth.CreateLabelForm{}), repo.UpdateLabel)
Expand All @@ -578,7 +578,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/delete", repo.DeleteMilestone)
}, reqRepoIssuesOrPullsWriter, context.RepoRef())

m.Combo("/compare/*", repo.MustAllowPulls, repo.SetEditorconfigIfExists).
m.Combo("/compare/*", reqRepoCodeReader, repo.MustAllowPulls, repo.SetEditorconfigIfExists).
Get(repo.SetDiffViewStyle, repo.CompareAndPullRequest).
Post(bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost)

Expand Down