Skip to content

Commit 96515c0

Browse files
authored
Fix org permission API visibility checks for hidden members and private orgs (#36798) (#36841)
backport #36798 - fix wrong parameter of HasOrgOrUserVisible in routers/api/v1/org/org.go - add integration tests covering the bug fix - merge permissions API tests --- Generated by a coding agent with Codex 5.2
1 parent 4f562da commit 96515c0

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

routers/api/v1/org/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
135135

136136
op := api.OrganizationPermissions{}
137137

138-
if !organization.HasOrgOrUserVisible(ctx, o, ctx.ContextUser) {
138+
if !organization.HasOrgOrUserVisible(ctx, o, ctx.Doer) {
139139
ctx.APIErrorNotFound("HasOrgOrUserVisible", nil)
140140
return
141141
}

tests/integration/api_user_org_perm_test.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,34 @@ import (
1515
"github.com/stretchr/testify/assert"
1616
)
1717

18+
func TestPermissionsAPI(t *testing.T) {
19+
defer tests.PrepareTestEnv(t)()
20+
21+
t.Run("TokenNeeded", testTokenNeeded)
22+
t.Run("WithOwnerUser", testWithOwnerUser)
23+
t.Run("CanWriteUser", testCanWriteUser)
24+
t.Run("AdminUser", testAdminUser)
25+
t.Run("AdminCanNotCreateRepo", testAdminCanNotCreateRepo)
26+
t.Run("CanReadUser", testCanReadUser)
27+
t.Run("UnknownUser", testUnknownUser)
28+
t.Run("UnknownOrganization", testUnknownOrganization)
29+
t.Run("HiddenMemberPermissionsForbidden", testHiddenMemberPermissionsForbidden)
30+
t.Run("PrivateOrgPermissionsNotFound", testPrivateOrgPermissionsNotFound)
31+
}
32+
1833
type apiUserOrgPermTestCase struct {
1934
LoginUser string
2035
User string
2136
Organization string
2237
ExpectedOrganizationPermissions api.OrganizationPermissions
2338
}
2439

25-
func TestTokenNeeded(t *testing.T) {
26-
defer tests.PrepareTestEnv(t)()
27-
40+
func testTokenNeeded(t *testing.T) {
2841
req := NewRequest(t, "GET", "/api/v1/users/user1/orgs/org6/permissions")
2942
MakeRequest(t, req, http.StatusUnauthorized)
3043
}
3144

3245
func sampleTest(t *testing.T, auoptc apiUserOrgPermTestCase) {
33-
defer tests.PrepareTestEnv(t)()
34-
3546
session := loginUser(t, auoptc.LoginUser)
3647
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopeReadUser)
3748

@@ -48,7 +59,7 @@ func sampleTest(t *testing.T, auoptc apiUserOrgPermTestCase) {
4859
assert.Equal(t, auoptc.ExpectedOrganizationPermissions.CanCreateRepository, apiOP.CanCreateRepository)
4960
}
5061

51-
func TestWithOwnerUser(t *testing.T) {
62+
func testWithOwnerUser(t *testing.T) {
5263
sampleTest(t, apiUserOrgPermTestCase{
5364
LoginUser: "user2",
5465
User: "user2",
@@ -63,7 +74,7 @@ func TestWithOwnerUser(t *testing.T) {
6374
})
6475
}
6576

66-
func TestCanWriteUser(t *testing.T) {
77+
func testCanWriteUser(t *testing.T) {
6778
sampleTest(t, apiUserOrgPermTestCase{
6879
LoginUser: "user4",
6980
User: "user4",
@@ -78,7 +89,7 @@ func TestCanWriteUser(t *testing.T) {
7889
})
7990
}
8091

81-
func TestAdminUser(t *testing.T) {
92+
func testAdminUser(t *testing.T) {
8293
sampleTest(t, apiUserOrgPermTestCase{
8394
LoginUser: "user1",
8495
User: "user28",
@@ -93,7 +104,7 @@ func TestAdminUser(t *testing.T) {
93104
})
94105
}
95106

96-
func TestAdminCanNotCreateRepo(t *testing.T) {
107+
func testAdminCanNotCreateRepo(t *testing.T) {
97108
sampleTest(t, apiUserOrgPermTestCase{
98109
LoginUser: "user1",
99110
User: "user28",
@@ -108,7 +119,7 @@ func TestAdminCanNotCreateRepo(t *testing.T) {
108119
})
109120
}
110121

111-
func TestCanReadUser(t *testing.T) {
122+
func testCanReadUser(t *testing.T) {
112123
sampleTest(t, apiUserOrgPermTestCase{
113124
LoginUser: "user1",
114125
User: "user24",
@@ -123,9 +134,7 @@ func TestCanReadUser(t *testing.T) {
123134
})
124135
}
125136

126-
func TestUnknowUser(t *testing.T) {
127-
defer tests.PrepareTestEnv(t)()
128-
137+
func testUnknownUser(t *testing.T) {
129138
session := loginUser(t, "user1")
130139
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadOrganization)
131140

@@ -138,9 +147,7 @@ func TestUnknowUser(t *testing.T) {
138147
assert.Equal(t, "user redirect does not exist [name: unknow]", apiError.Message)
139148
}
140149

141-
func TestUnknowOrganization(t *testing.T) {
142-
defer tests.PrepareTestEnv(t)()
143-
150+
func testUnknownOrganization(t *testing.T) {
144151
session := loginUser(t, "user1")
145152
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadOrganization)
146153

@@ -151,3 +158,38 @@ func TestUnknowOrganization(t *testing.T) {
151158
DecodeJSON(t, resp, &apiError)
152159
assert.Equal(t, "GetUserByName", apiError.Message)
153160
}
161+
162+
func testHiddenMemberPermissionsForbidden(t *testing.T) {
163+
session := loginUser(t, "user8")
164+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadOrganization)
165+
166+
req := NewRequest(t, "GET", "/api/v1/users/user5/orgs/privated_org/permissions").
167+
AddTokenAuth(token)
168+
MakeRequest(t, req, http.StatusNotFound)
169+
170+
adminSession := loginUser(t, "user1")
171+
adminToken := getTokenForLoggedInUser(t, adminSession, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadOrganization)
172+
173+
adminReq := NewRequest(t, "GET", "/api/v1/users/user5/orgs/privated_org/permissions").
174+
AddTokenAuth(adminToken)
175+
resp := MakeRequest(t, adminReq, http.StatusOK)
176+
177+
var apiOP api.OrganizationPermissions
178+
DecodeJSON(t, resp, &apiOP)
179+
assert.Equal(t, api.OrganizationPermissions{
180+
IsOwner: false,
181+
IsAdmin: false,
182+
CanWrite: true,
183+
CanRead: true,
184+
CanCreateRepository: true,
185+
}, apiOP)
186+
}
187+
188+
func testPrivateOrgPermissionsNotFound(t *testing.T) {
189+
session := loginUser(t, "user8")
190+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser, auth_model.AccessTokenScopeReadOrganization)
191+
192+
req := NewRequest(t, "GET", "/api/v1/users/user5/orgs/privated_org/permissions").
193+
AddTokenAuth(token)
194+
MakeRequest(t, req, http.StatusNotFound)
195+
}

0 commit comments

Comments
 (0)