Skip to content

Ensure that feeds are appropriately restricted (#10018) #10019

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

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
}

cond = cond.And(builder.In("repo_id", repoIDs))
} else {
cond = cond.And(builder.In("repo_id", AccessibleRepoIDsQuery(opts.RequestingUserID)))
}

cond = cond.And(builder.Eq{"user_id": opts.RequestedUser.ID})
Expand Down
17 changes: 17 additions & 0 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ func SearchRepository(opts *SearchRepoOptions) (RepositoryList, int64, error) {

// accessibleRepositoryCondition takes a user a returns a condition for checking if a repository is accessible
func accessibleRepositoryCondition(userID int64) builder.Cond {
if userID <= 0 {
return builder.And(
builder.Eq{"`repository`.is_private": false},
builder.Or(
// A. Aren't in organisations __OR__
builder.NotIn("`repository`.owner_id", builder.Select("id").From("`user`").Where(builder.Eq{"type": UserTypeOrganization})),
// B. Is a public organisation.
builder.In("`repository`.owner_id", builder.Select("id").From("`user`").Where(builder.Eq{"visibility": structs.VisibleTypePublic}))),
)
}

return builder.Or(
// 1. Be able to see all non-private repositories that either:
builder.And(
Expand Down Expand Up @@ -349,6 +360,12 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
return SearchRepository(opts)
}

// AccessibleRepoIDsQuery queries accessible repository ids. Usable as a subquery wherever repo ids need to be filtered.
func AccessibleRepoIDsQuery(userID int64) *builder.Builder {
// NB: Please note this code needs to still work if user is nil
return builder.Select("id").From("repository").Where(accessibleRepositoryCondition(userID))
}

// FindUserAccessibleRepoIDs find all accessible repositories' ID by user's id
func FindUserAccessibleRepoIDs(userID int64) ([]int64, error) {
var accessCond builder.Cond = builder.Eq{"is_private": false}
Expand Down
14 changes: 10 additions & 4 deletions routers/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ func Dashboard(ctx *context.Context) {
ctx.Data["MirrorCount"] = len(mirrors)
ctx.Data["Mirrors"] = mirrors

requestingUserID := int64(0)
if ctx.User != nil {
requestingUserID = ctx.User.ID
}

retrieveFeeds(ctx, models.GetFeedsOptions{
RequestedUser: ctxUser,
IncludePrivate: true,
OnlyPerformedBy: false,
IncludeDeleted: false,
RequestedUser: ctxUser,
RequestingUserID: requestingUserID,
IncludePrivate: true,
OnlyPerformedBy: false,
IncludeDeleted: false,
})

if ctx.Written() {
Expand Down
12 changes: 9 additions & 3 deletions routers/user/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,20 @@ func Profile(ctx *context.Context) {
orderBy = models.SearchOrderByRecentUpdated
}

requestingUserID := int64(0)
if ctx.User != nil {
requestingUserID = ctx.User.ID
}

keyword := strings.Trim(ctx.Query("q"), " ")
ctx.Data["Keyword"] = keyword
switch tab {
case "activity":
retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
IncludePrivate: showPrivate,
OnlyPerformedBy: true,
IncludeDeleted: false,
RequestingUserID: requestingUserID,
IncludePrivate: showPrivate,
OnlyPerformedBy: true,
IncludeDeleted: false,
})
if ctx.Written() {
return
Expand Down