Skip to content

Commit 8ddf817

Browse files
committed
Add public only filters when token is public only
1 parent ebd2328 commit 8ddf817

File tree

4 files changed

+60
-33
lines changed

4 files changed

+60
-33
lines changed

routers/api/v1/api.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,32 @@ func tokenRequiresScopes(requiredScopeCategories ...auth_model.AccessTokenScopeC
269269
return
270270
}
271271

272+
ctx.Data["requiredScopeCategories"] = requiredScopeCategories
273+
272274
// check if scope only applies to public resources
273275
publicOnly, err := scope.PublicOnly()
274276
if err != nil {
275277
ctx.Error(http.StatusForbidden, "tokenRequiresScope", "parsing public resource scope failed: "+err.Error())
276278
return
277279
}
278280

279-
if !publicOnly {
281+
// assign to true so that those searching should only filter public repositories/users/organizations
282+
ctx.PublicOnly = publicOnly
283+
}
284+
}
285+
286+
func checkTokenPublicOnly() func(ctx *context.APIContext) {
287+
return func(ctx *context.APIContext) {
288+
if !ctx.PublicOnly {
289+
return
290+
}
291+
292+
requiredScopeCategories, ok := ctx.Data["requiredScopeCategories"].([]auth_model.AccessTokenScopeCategory)
293+
if !ok || len(requiredScopeCategories) == 0 {
280294
return
281295
}
282296

297+
// public Only permission check
283298
switch {
284299
case auth_model.ContainsCategory(requiredScopeCategories, auth_model.AccessTokenScopeCategoryRepository),
285300
auth_model.ContainsCategory(requiredScopeCategories, auth_model.AccessTokenScopeCategoryIssue):
@@ -893,11 +908,11 @@ func Routes() *web.Router {
893908
m.Group("/user/{username}", func() {
894909
m.Get("", activitypub.Person)
895910
m.Post("/inbox", activitypub.ReqHTTPSignature(), activitypub.PersonInbox)
896-
}, context.UserAssignmentAPI())
911+
}, context.UserAssignmentAPI(), checkTokenPublicOnly())
897912
m.Group("/user-id/{user-id}", func() {
898913
m.Get("", activitypub.Person)
899914
m.Post("/inbox", activitypub.ReqHTTPSignature(), activitypub.PersonInbox)
900-
}, context.UserIDAssignmentAPI())
915+
}, context.UserIDAssignmentAPI(), checkTokenPublicOnly())
901916
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryActivityPub))
902917
}
903918

@@ -926,13 +941,13 @@ func Routes() *web.Router {
926941
// Notifications (requires 'notifications' scope)
927942
m.Group("/notifications", func() {
928943
m.Combo("").
929-
Get(reqToken(), notify.ListNotifications).
930-
Put(reqToken(), notify.ReadNotifications)
931-
m.Get("/new", reqToken(), notify.NewAvailable)
944+
Get(notify.ListNotifications).
945+
Put(notify.ReadNotifications)
946+
m.Get("/new", notify.NewAvailable)
932947
m.Combo("/threads/{id}").
933-
Get(reqToken(), notify.GetThread).
934-
Patch(reqToken(), notify.ReadThread)
935-
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryNotification))
948+
Get(notify.GetThread).
949+
Patch(notify.ReadThread)
950+
}, reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryNotification))
936951

937952
// Users (requires user scope)
938953
m.Group("/users", func() {
@@ -953,8 +968,8 @@ func Routes() *web.Router {
953968
}, reqSelfOrAdmin(), reqBasicOrRevProxyAuth())
954969

955970
m.Get("/activities/feeds", user.ListUserActivityFeeds)
956-
}, context.UserAssignmentAPI(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser), individualPermsChecker)
957-
})
971+
}, context.UserAssignmentAPI(), checkTokenPublicOnly(), individualPermsChecker)
972+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser))
958973

959974
// Users (requires user scope)
960975
m.Group("/users", func() {
@@ -971,8 +986,8 @@ func Routes() *web.Router {
971986
m.Get("/starred", user.GetStarredRepos)
972987

973988
m.Get("/subscriptions", user.GetWatchedRepos)
974-
}, reqToken(), context.UserAssignmentAPI(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser))
975-
})
989+
}, context.UserAssignmentAPI(), checkTokenPublicOnly())
990+
}, reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser))
976991

977992
// Users (requires user scope)
978993
m.Group("/user", func() {
@@ -1058,8 +1073,8 @@ func Routes() *web.Router {
10581073
m.Get("", user.IsStarring)
10591074
m.Put("", user.Star)
10601075
m.Delete("", user.Unstar)
1061-
}, repoAssignment(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
1062-
})
1076+
}, repoAssignment(), checkTokenPublicOnly())
1077+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
10631078
m.Get("/times", repo.ListMyTrackedTimes)
10641079
m.Get("/stopwatches", repo.GetStopwatches)
10651080
m.Get("/subscriptions", user.GetMyWatchedRepos)
@@ -1083,7 +1098,7 @@ func Routes() *web.Router {
10831098
m.Get("", user.CheckUserBlock)
10841099
m.Put("", user.BlockUser)
10851100
m.Delete("", user.UnblockUser)
1086-
}, context.UserAssignmentAPI())
1101+
}, context.UserAssignmentAPI(), checkTokenPublicOnly())
10871102
})
10881103
}, reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser))
10891104

@@ -1101,10 +1116,10 @@ func Routes() *web.Router {
11011116

11021117
// Repos (requires repo scope)
11031118
m.Group("/repos", func() {
1104-
m.Get("/search", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), repo.Search)
1119+
m.Get("/search", repo.Search)
11051120

11061121
// (repo scope)
1107-
m.Post("/migrate", reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository), bind(api.MigrateRepoOptions{}), repo.Migrate)
1122+
m.Post("/migrate", reqToken(), bind(api.MigrateRepoOptions{}), repo.Migrate)
11081123

11091124
m.Group("/{username}/{reponame}", func() {
11101125
m.Get("/compare/*", reqRepoReader(unit.TypeCode), repo.CompareDiff)
@@ -1350,21 +1365,21 @@ func Routes() *web.Router {
13501365
m.Post("", bind(api.UpdateRepoAvatarOption{}), repo.UpdateAvatar)
13511366
m.Delete("", repo.DeleteAvatar)
13521367
}, reqAdmin(), reqToken())
1353-
}, repoAssignment(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
1354-
})
1368+
}, repoAssignment(), checkTokenPublicOnly())
1369+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryRepository))
13551370

13561371
// Notifications (requires notifications scope)
13571372
m.Group("/repos", func() {
13581373
m.Group("/{username}/{reponame}", func() {
13591374
m.Combo("/notifications", reqToken()).
13601375
Get(notify.ListRepoNotifications).
13611376
Put(notify.ReadRepoNotifications)
1362-
}, repoAssignment(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryNotification))
1363-
})
1377+
}, repoAssignment(), checkTokenPublicOnly())
1378+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryNotification))
13641379

13651380
// Issue (requires issue scope)
13661381
m.Group("/repos", func() {
1367-
m.Get("/issues/search", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryIssue), repo.SearchIssues)
1382+
m.Get("/issues/search", repo.SearchIssues)
13681383

13691384
m.Group("/{username}/{reponame}", func() {
13701385
m.Group("/issues", func() {
@@ -1473,8 +1488,8 @@ func Routes() *web.Router {
14731488
Patch(reqToken(), reqRepoWriter(unit.TypeIssues, unit.TypePullRequests), bind(api.EditMilestoneOption{}), repo.EditMilestone).
14741489
Delete(reqToken(), reqRepoWriter(unit.TypeIssues, unit.TypePullRequests), repo.DeleteMilestone)
14751490
})
1476-
}, repoAssignment(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryIssue))
1477-
})
1491+
}, repoAssignment(), checkTokenPublicOnly())
1492+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryIssue))
14781493

14791494
// NOTE: these are Gitea package management API - see packages.CommonRoutes and packages.DockerContainerRoutes for endpoints that implement package manager APIs
14801495
m.Group("/packages/{username}", func() {
@@ -1484,14 +1499,14 @@ func Routes() *web.Router {
14841499
m.Get("/files", reqToken(), packages.ListPackageFiles)
14851500
})
14861501
m.Get("/", reqToken(), packages.ListPackages)
1487-
}, context.UserAssignmentAPI(), context.PackageAssignmentAPI(), reqPackageAccess(perm.AccessModeRead), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryPackage))
1502+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryPackage), context.UserAssignmentAPI(), context.PackageAssignmentAPI(), reqPackageAccess(perm.AccessModeRead), checkTokenPublicOnly())
14881503

14891504
// Organizations
14901505
m.Get("/user/orgs", reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser, auth_model.AccessTokenScopeCategoryOrganization), org.ListMyOrgs)
14911506
m.Group("/users/{username}/orgs", func() {
14921507
m.Get("", reqToken(), org.ListUserOrgs)
14931508
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
1494-
}, context.UserAssignmentAPI(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser, auth_model.AccessTokenScopeCategoryOrganization))
1509+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser, auth_model.AccessTokenScopeCategoryOrganization), context.UserAssignmentAPI(), checkTokenPublicOnly())
14951510
m.Post("/orgs", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), reqToken(), bind(api.CreateOrgOption{}), org.Create)
14961511
m.Get("/orgs", org.GetAll, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization))
14971512
m.Group("/orgs/{org}", func() {
@@ -1549,7 +1564,7 @@ func Routes() *web.Router {
15491564
m.Delete("", org.UnblockUser)
15501565
})
15511566
}, reqToken(), reqOrgOwnership())
1552-
}, orgAssignment(true), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization))
1567+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), orgAssignment(true), checkTokenPublicOnly())
15531568
m.Group("/teams/{teamid}", func() {
15541569
m.Combo("").Get(reqToken(), org.GetTeam).
15551570
Patch(reqToken(), reqOrgOwnership(), bind(api.EditTeamOption{}), org.EditTeam).
@@ -1569,7 +1584,7 @@ func Routes() *web.Router {
15691584
Get(reqToken(), org.GetTeamRepo)
15701585
})
15711586
m.Get("/activities/feeds", org.ListTeamActivityFeeds)
1572-
}, orgAssignment(false, true), reqToken(), tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), reqTeamMembership())
1587+
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), orgAssignment(false, true), reqToken(), reqTeamMembership(), checkTokenPublicOnly())
15731588

15741589
m.Group("/admin", func() {
15751590
m.Group("/cron", func() {

routers/api/v1/repo/repo.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func Search(ctx *context.APIContext) {
129129
// "422":
130130
// "$ref": "#/responses/validationError"
131131

132+
private := ctx.IsSigned && (ctx.FormString("private") == "" || ctx.FormBool("private"))
133+
if ctx.PublicOnly {
134+
private = false
135+
}
136+
132137
opts := &repo_model.SearchRepoOptions{
133138
ListOptions: utils.GetListOptions(ctx),
134139
Actor: ctx.Doer,
@@ -138,7 +143,7 @@ func Search(ctx *context.APIContext) {
138143
TeamID: ctx.FormInt64("team_id"),
139144
TopicOnly: ctx.FormBool("topic"),
140145
Collaborate: optional.None[bool](),
141-
Private: ctx.IsSigned && (ctx.FormString("private") == "" || ctx.FormBool("private")),
146+
Private: private,
142147
Template: optional.None[bool](),
143148
StarredByID: ctx.FormInt64("starredBy"),
144149
IncludeDescription: ctx.FormBool("includeDesc"),

routers/api/v1/user/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
activities_model "code.gitea.io/gitea/models/activities"
1111
user_model "code.gitea.io/gitea/models/user"
12+
"code.gitea.io/gitea/modules/structs"
1213
"code.gitea.io/gitea/routers/api/v1/utils"
1314
"code.gitea.io/gitea/services/context"
1415
"code.gitea.io/gitea/services/convert"
@@ -67,12 +68,17 @@ func Search(ctx *context.APIContext) {
6768
maxResults = 1
6869
users = []*user_model.User{user_model.NewActionsUser()}
6970
default:
71+
var visible []structs.VisibleType
72+
if ctx.PublicOnly {
73+
visible = []structs.VisibleType{structs.VisibleTypePublic}
74+
}
7075
users, maxResults, err = user_model.SearchUsers(ctx, &user_model.SearchUserOptions{
7176
Actor: ctx.Doer,
7277
Keyword: ctx.FormTrim("q"),
7378
UID: uid,
7479
Type: user_model.UserTypeIndividual,
7580
SearchByEmail: true,
81+
Visible: visible,
7682
ListOptions: listOptions,
7783
})
7884
if err != nil {

services/context/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ type APIContext struct {
3535

3636
ContextUser *user_model.User // the user which is being visited, in most cases it differs from Doer
3737

38-
Repo *Repository
39-
Org *APIOrganization
40-
Package *Package
38+
Repo *Repository
39+
Org *APIOrganization
40+
Package *Package
41+
PublicOnly bool // Whether the request is for a public endpoint
4142
}
4243

4344
func init() {

0 commit comments

Comments
 (0)