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
rename CanAccess
  • Loading branch information
lunny committed Nov 28, 2018
commit 2f65f7a8bc03b83d9a4f53b3572cd28d7e8be08b
2 changes: 1 addition & 1 deletion models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
}
valid, err := canBeAssigned(e, user, opts.Repo)
if err != nil {
return fmt.Errorf("hasAccess [user_id: %d, repo_id: %d]: %v", assigneeID, opts.Repo.ID, err)
return fmt.Errorf("canBeAssigned [user_id: %d, repo_id: %d]: %v", assigneeID, opts.Repo.ID, err)
}
if !valid {
return ErrUserDoesNotHaveAccessToRepo{UserID: assigneeID, RepoName: opts.Repo.Name}
Expand Down
2 changes: 1 addition & 1 deletion models/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
if err != nil {
return err
}
if !perm.CanWrite(UnitTypeCode) {
if !perm.CanAccess(mode, UnitTypeCode) {
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
}
return nil
Expand Down
20 changes: 4 additions & 16 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ func (repo *Repository) CheckUnitUser(userID int64, isAdmin bool, unitType UnitT
}

func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unitType UnitType) bool {
if isAdmin {
return true
}
user, err := getUserByID(e, userID)
if err != nil {
return false
Expand All @@ -334,7 +337,7 @@ func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unit
return false
}

return perm.CanAccess(unitType)
return perm.CanRead(unitType)
}

// UnitEnabled if this repository has the given unit enabled
Expand All @@ -350,21 +353,6 @@ func (repo *Repository) UnitEnabled(tp UnitType) bool {
return false
}

// AnyUnitEnabled if this repository has the any of the given units enabled
func (repo *Repository) AnyUnitEnabled(tps ...UnitType) bool {
if err := repo.getUnits(x); err != nil {
log.Warn("Error loading repository (ID: %d) units: %s", repo.ID, err.Error())
}
for _, unit := range repo.Units {
for _, tp := range tps {
if unit.Type == tp {
return true
}
}
}
return false
}

var (
// ErrUnitNotExist organization does not exist
ErrUnitNotExist = errors.New("Unit does not exist")
Expand Down
19 changes: 12 additions & 7 deletions models/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,28 @@ func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
}

// CanAccess returns true if user has read access to the unit of the repository
func (p *Permission) CanAccess(unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= AccessModeRead
func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= mode
}

// CanAccessAny returns true if user has read access to any of the units of the repository
func (p *Permission) CanAccessAny(unitTypes ...UnitType) bool {
func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
for _, u := range unitTypes {
if p.CanAccess(u) {
if p.CanAccess(mode, u) {
return true
}
}
return false
}

// CanWrite returns true if user could write to this unit
func (p *Permission) CanRead(unitType UnitType) bool {
return p.CanAccess(AccessModeRead, unitType)
}

// CanWrite returns true if user could write to this unit
func (p *Permission) CanWrite(unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= AccessModeWrite
return p.CanAccess(AccessModeWrite, unitType)
}

// CanWriteIssuesOrPulls returns true if isPull is true and user could write to pull requests and
Expand All @@ -75,9 +80,9 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
// 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.CanRead(UnitTypePullRequests)
}
return p.CanAccess(UnitTypeIssues)
return p.CanRead(UnitTypeIssues)
}

// GetUserRepoPermission returns the user permissions to the repository
Expand Down
4 changes: 2 additions & 2 deletions modules/context/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func RequireRepoWriterOr(unitTypes ...models.UnitType) macaron.Handler {
// RequireRepoReader returns a macaron middleware for requiring repository read to the specify unitType
func RequireRepoReader(unitType models.UnitType) macaron.Handler {
return func(ctx *Context) {
if !ctx.Repo.CanAccess(unitType) {
if !ctx.Repo.CanRead(unitType) {
ctx.NotFound(ctx.Req.RequestURI, nil)
return
}
Expand All @@ -55,7 +55,7 @@ func RequireRepoReader(unitType models.UnitType) macaron.Handler {
func RequireRepoReaderOr(unitTypes ...models.UnitType) macaron.Handler {
return func(ctx *Context) {
for _, unitType := range unitTypes {
if ctx.Repo.CanAccess(unitType) {
if ctx.Repo.CanRead(unitType) {
return
}
}
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func reqAdmin() macaron.Handler {

func reqRepoReader(unitType models.UnitType) macaron.Handler {
return func(ctx *context.Context) {
if !ctx.Repo.CanAccess(unitType) {
if !ctx.Repo.CanRead(unitType) {
ctx.Error(403)
return
}
Expand Down Expand Up @@ -343,22 +343,22 @@ func orgAssignment(args ...bool) macaron.Handler {
}

func mustEnableIssues(ctx *context.APIContext) {
if !ctx.Repo.CanAccess(models.UnitTypeIssues) {
if !ctx.Repo.CanRead(models.UnitTypeIssues) {
ctx.Status(404)
return
}
}

func mustAllowPulls(ctx *context.Context) {
if !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanAccess(models.UnitTypePullRequests)) {
if !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
ctx.Status(404)
return
}
}

func mustEnableIssuesOrPulls(ctx *context.Context) {
if !ctx.Repo.CanAccess(models.UnitTypeIssues) &&
!(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanAccess(models.UnitTypePullRequests)) {
if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
!(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
ctx.Status(404)
return
}
Expand Down
6 changes: 3 additions & 3 deletions routers/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func Activity(ctx *context.Context) {

var err error
if ctx.Data["Activity"], err = models.GetActivityStats(ctx.Repo.Repository.ID, timeFrom,
ctx.Repo.CanAccess(models.UnitTypeReleases),
ctx.Repo.CanAccess(models.UnitTypeIssues),
ctx.Repo.CanAccess(models.UnitTypePullRequests)); err != nil {
ctx.Repo.CanRead(models.UnitTypeReleases),
ctx.Repo.CanRead(models.UnitTypeIssues),
ctx.Repo.CanRead(models.UnitTypePullRequests)); err != nil {
ctx.ServerError("GetActivityStats", err)
return
}
Expand Down
14 changes: 7 additions & 7 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ var (

// MustEnableIssues check if repository enable internal issues
func MustEnableIssues(ctx *context.Context) {
if !ctx.Repo.CanAccess(models.UnitTypeIssues) &&
!ctx.Repo.CanAccess(models.UnitTypeExternalTracker) {
if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
!ctx.Repo.CanRead(models.UnitTypeExternalTracker) {
ctx.NotFound("MustEnableIssues", nil)
return
}
Expand All @@ -79,7 +79,7 @@ func MustEnableIssues(ctx *context.Context) {

// MustAllowPulls check if repository enable pull requests and user have right to do that
func MustAllowPulls(ctx *context.Context) {
if !ctx.Repo.Repository.CanEnablePulls() || !ctx.Repo.CanAccess(models.UnitTypePullRequests) {
if !ctx.Repo.Repository.CanEnablePulls() || !ctx.Repo.CanRead(models.UnitTypePullRequests) {
ctx.NotFound("MustAllowPulls", nil)
return
}
Expand Down Expand Up @@ -859,8 +859,8 @@ func GetActionIssue(ctx *context.Context) *models.Issue {
}

func checkIssueRights(ctx *context.Context, issue *models.Issue) {
if issue.IsPull && !ctx.Repo.CanAccess(models.UnitTypePullRequests) ||
!issue.IsPull && !ctx.Repo.CanAccess(models.UnitTypeIssues) {
if issue.IsPull && !ctx.Repo.CanRead(models.UnitTypePullRequests) ||
!issue.IsPull && !ctx.Repo.CanRead(models.UnitTypeIssues) {
ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
}
}
Expand All @@ -885,8 +885,8 @@ func getActionIssues(ctx *context.Context) []*models.Issue {
return nil
}
// Check access rights for all issues
issueUnitEnabled := ctx.Repo.CanAccess(models.UnitTypeIssues)
prUnitEnabled := ctx.Repo.CanAccess(models.UnitTypePullRequests)
issueUnitEnabled := ctx.Repo.CanRead(models.UnitTypeIssues)
prUnitEnabled := ctx.Repo.CanRead(models.UnitTypePullRequests)
for _, issue := range issues {
if issue.IsPull && !prUnitEnabled || !issue.IsPull && !issueUnitEnabled {
ctx.NotFound("IssueOrPullRequestUnitNotAllowed", nil)
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func getForkRepository(ctx *context.Context) *models.Repository {
return nil
}

if forkRepo.IsBare || !perm.CanAccess(models.UnitTypeCode) {
if forkRepo.IsBare || !perm.CanRead(models.UnitTypeCode) {
ctx.NotFound("getForkRepository", nil)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions routers/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const (

// MustEnableWiki check if wiki is enabled, if external then redirect
func MustEnableWiki(ctx *context.Context) {
if !ctx.Repo.CanAccess(models.UnitTypeWiki) &&
!ctx.Repo.CanAccess(models.UnitTypeExternalWiki) {
if !ctx.Repo.CanRead(models.UnitTypeWiki) &&
!ctx.Repo.CanRead(models.UnitTypeExternalWiki) {
ctx.NotFound("MustEnableWiki", nil)
return
}
Expand Down
2 changes: 1 addition & 1 deletion routers/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func Issues(ctx *context.Context) {
ctx.ServerError("GetUserRepoPermission", fmt.Errorf("[%d]%v", repoID, err))
return
}
if !perm.CanAccess(models.UnitTypeIssues) {
if !perm.CanRead(models.UnitTypeIssues) {
ctx.Status(404)
return
}
Expand Down