Skip to content

Commit 265f485

Browse files
authored
Do some missing checks (#28423) (#28432)
backport #28423
1 parent f144521 commit 265f485

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

routers/api/v1/api.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,24 @@ func verifyAuthWithOptions(options *common.VerifyOptions) func(ctx *context.APIC
789789
}
790790
}
791791

792+
func individualPermsChecker(ctx *context.APIContext) {
793+
// org permissions have been checked in context.OrgAssignment(), but individual permissions haven't been checked.
794+
if ctx.ContextUser.IsIndividual() {
795+
switch {
796+
case ctx.ContextUser.Visibility == api.VisibleTypePrivate:
797+
if ctx.Doer == nil || (ctx.ContextUser.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin) {
798+
ctx.NotFound("Visit Project", nil)
799+
return
800+
}
801+
case ctx.ContextUser.Visibility == api.VisibleTypeLimited:
802+
if ctx.Doer == nil {
803+
ctx.NotFound("Visit Project", nil)
804+
return
805+
}
806+
}
807+
}
808+
}
809+
792810
// check for and warn against deprecated authentication options
793811
func checkDeprecatedAuthMethods(ctx *context.APIContext) {
794812
if ctx.FormString("token") != "" || ctx.FormString("access_token") != "" {
@@ -898,7 +916,7 @@ func Routes() *web.Route {
898916
}, reqSelfOrAdmin(), reqBasicOrRevProxyAuth())
899917

900918
m.Get("/activities/feeds", user.ListUserActivityFeeds)
901-
}, context_service.UserAssignmentAPI())
919+
}, context_service.UserAssignmentAPI(), individualPermsChecker)
902920
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser))
903921

904922
// Users (requires user scope)

routers/web/web.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,24 @@ func registerRoutes(m *web.Route) {
795795
}
796796
}
797797

798+
individualPermsChecker := func(ctx *context.Context) {
799+
// org permissions have been checked in context.OrgAssignment(), but individual permissions haven't been checked.
800+
if ctx.ContextUser.IsIndividual() {
801+
switch {
802+
case ctx.ContextUser.Visibility == structs.VisibleTypePrivate:
803+
if ctx.Doer == nil || (ctx.ContextUser.ID != ctx.Doer.ID && !ctx.Doer.IsAdmin) {
804+
ctx.NotFound("Visit Project", nil)
805+
return
806+
}
807+
case ctx.ContextUser.Visibility == structs.VisibleTypeLimited:
808+
if ctx.Doer == nil {
809+
ctx.NotFound("Visit Project", nil)
810+
return
811+
}
812+
}
813+
}
814+
}
815+
798816
// ***** START: Organization *****
799817
m.Group("/org", func() {
800818
m.Group("/{org}", func() {
@@ -975,11 +993,11 @@ func registerRoutes(m *web.Route) {
975993
return
976994
}
977995
})
978-
})
996+
}, reqUnitAccess(unit.TypeProjects, perm.AccessModeRead, true), individualPermsChecker)
979997

980998
m.Group("", func() {
981999
m.Get("/code", user.CodeSearch)
982-
}, reqUnitAccess(unit.TypeCode, perm.AccessModeRead, false))
1000+
}, reqUnitAccess(unit.TypeCode, perm.AccessModeRead, false), individualPermsChecker)
9831001
}, ignSignIn, context_service.UserAssignmentWeb(), context.OrgAssignment()) // for "/{username}/-" (packages, projects, code)
9841002

9851003
m.Group("/{username}/{reponame}", func() {

tests/integration/project_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"code.gitea.io/gitea/tests"
11+
)
12+
13+
func TestPrivateRepoProject(t *testing.T) {
14+
defer tests.PrepareTestEnv(t)()
15+
16+
// not logged in user
17+
req := NewRequest(t, "GET", "/user31/-/projects")
18+
MakeRequest(t, req, http.StatusNotFound)
19+
20+
sess := loginUser(t, "user1")
21+
req = NewRequest(t, "GET", "/user31/-/projects")
22+
sess.MakeRequest(t, req, http.StatusOK)
23+
}

0 commit comments

Comments
 (0)