mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-26 05:33:35 +00:00
fix(api): enforce organization listing token scope (#39041)
Enforce organization token scope before listing organizations and retain public-only filtering. --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
@@ -1781,7 +1781,7 @@ func Routes() *web.Router {
|
|||||||
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
|
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
|
||||||
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser, auth_model.AccessTokenScopeCategoryOrganization), context.UserAssignmentAPI(), checkTokenPublicOnly(), individualPermsChecker)
|
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser, auth_model.AccessTokenScopeCategoryOrganization), context.UserAssignmentAPI(), checkTokenPublicOnly(), individualPermsChecker)
|
||||||
m.Post("/orgs", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), reqToken(), bind(api.CreateOrgOption{}), org.Create)
|
m.Post("/orgs", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), reqToken(), bind(api.CreateOrgOption{}), org.Create)
|
||||||
m.Get("/orgs", org.GetAll, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization))
|
m.Get("/orgs", tokenRequiresScopes(auth_model.AccessTokenScopeCategoryOrganization), org.GetAll)
|
||||||
m.Group("/orgs/{org}", func() {
|
m.Group("/orgs/{org}", func() {
|
||||||
m.Combo("").Get(org.Get).
|
m.Combo("").Get(org.Get).
|
||||||
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
|
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
|
||||||
|
|||||||
@@ -121,6 +121,9 @@ func testAPIOrgGeneral(t *testing.T) {
|
|||||||
user1Token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteOrganization)
|
user1Token := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeWriteOrganization)
|
||||||
|
|
||||||
t.Run("OrgGetAll", func(t *testing.T) {
|
t.Run("OrgGetAll", func(t *testing.T) {
|
||||||
|
miscToken := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeReadMisc)
|
||||||
|
MakeRequest(t, NewRequest(t, "GET", "/api/v1/orgs").AddTokenAuth(miscToken), http.StatusForbidden)
|
||||||
|
|
||||||
// accessing with a token will return all orgs
|
// accessing with a token will return all orgs
|
||||||
req := NewRequest(t, "GET", "/api/v1/orgs").AddTokenAuth(user1Token)
|
req := NewRequest(t, "GET", "/api/v1/orgs").AddTokenAuth(user1Token)
|
||||||
resp := MakeRequest(t, req, http.StatusOK)
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
@@ -130,6 +133,14 @@ func testAPIOrgGeneral(t *testing.T) {
|
|||||||
assert.Equal(t, "Limited Org 36", apiOrgList[1].FullName)
|
assert.Equal(t, "Limited Org 36", apiOrgList[1].FullName)
|
||||||
assert.Equal(t, api.VisibilityStringLimited, apiOrgList[1].Visibility)
|
assert.Equal(t, api.VisibilityStringLimited, apiOrgList[1].Visibility)
|
||||||
|
|
||||||
|
publicOnlyToken := getTokenForLoggedInUser(t, user1Session, auth_model.AccessTokenScopeReadOrganization, auth_model.AccessTokenScopePublicOnly)
|
||||||
|
resp = MakeRequest(t, NewRequest(t, "GET", "/api/v1/orgs").AddTokenAuth(publicOnlyToken), http.StatusOK)
|
||||||
|
apiOrgList = DecodeJSON(t, resp, []*api.Organization{})
|
||||||
|
assert.Len(t, apiOrgList, 9)
|
||||||
|
for _, org := range apiOrgList {
|
||||||
|
assert.Equal(t, api.VisibilityStringPublic, org.Visibility)
|
||||||
|
}
|
||||||
|
|
||||||
// accessing without a token will return only public orgs
|
// accessing without a token will return only public orgs
|
||||||
req = NewRequest(t, "GET", "/api/v1/orgs")
|
req = NewRequest(t, "GET", "/api/v1/orgs")
|
||||||
resp = MakeRequest(t, req, http.StatusOK)
|
resp = MakeRequest(t, req, http.StatusOK)
|
||||||
|
|||||||
Reference in New Issue
Block a user