mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-18 00:36:35 +00:00
This PR is part of a series (#11311). If the user authenticating to an API call is a Forgejo site administrator, or a Forgejo repo administrator, a wide variety of permission and ownership checks in the API are either bypassed, or are bypassable. If a user has created an access token with restricted resources, I understand the intent of the user is to create a token which has a layer of risk reduction in the event that the token is lost/leaked to an attacker. For this reason, it makes sense to me that restricted scope access tokens shouldn't inherit the owner's administrator access. My intent is that repo-specific access tokens [will only be able to access specific authorization scopes](https://codeberg.org/forgejo/design/issues/50#issuecomment-11093951), probably: `repository:read`, `repository:write`, `issue:read`, `issue:write`, (`organization:read` / `user:read` maybe). This means that *most* admin access is not intended to be affected by this because repo-specific access tokens won't have, for example, `admin:write` scope. However, administrative access still grants elevated permissions in some areas that are relevant to these scopes, and need to be restricted: - The `?sudo=otheruser` query parameter allows site administrators to impersonate other users in the API. - Repository management rules are different for a site administrator, allowing them to create repos for another user, create repos in another organization, migrate a repository to an arbitrary owner, and transfer a repository to a prviate organization. - Administrators have access to extra data through some APIs which would be in scope: the detailed configuration of branch protection rules, the some details of repository deploy keys (which repo, and which scope -- seems odd), (user:read -- user SSH keys, activity feeds of private users, user profiles of private users, user webhook configurations). - Pull request reviews have additional perms for repo administrators, including the ability to dismiss PR reviews, delete PR reviews, and view draft PR reviews. - Repo admins and site admins can comment on locked issues, and related to comments can edit or delete other user's comments and attachments. - Repo admins can manage and view logged time on behalf of other users. A handful of these permissions may make sense for repo-specific access tokens, but most of them clearly exceed the risk that would be expected from creating a limited scope access token. I'd generally prefer to take a restrictive approach, and we can relax it if real-world use-cases come in -- users will have a workaround of creating an access token without repo-specific restrictions if they are blocked from needed access. **Breaking:** The administration restrictions introduced in this PR affect both repo-specific access tokens, and existing public-only access tokens. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - Although repo-specific access tokens are not yet exposed to end users, the breaking changes to public-only tokens will be visible to users and require release notes. - [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11468 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
329 lines
8.5 KiB
Go
329 lines
8.5 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package org
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"forgejo.org/models"
|
|
"forgejo.org/models/organization"
|
|
"forgejo.org/modules/setting"
|
|
api "forgejo.org/modules/structs"
|
|
"forgejo.org/routers/api/v1/user"
|
|
"forgejo.org/routers/api/v1/utils"
|
|
"forgejo.org/services/context"
|
|
"forgejo.org/services/convert"
|
|
)
|
|
|
|
// listMembers list an organization's members
|
|
func listMembers(ctx *context.APIContext, isMember bool) {
|
|
opts := &organization.FindOrgMembersOpts{
|
|
Doer: ctx.Doer,
|
|
IsDoerMember: isMember,
|
|
OrgID: ctx.Org.Organization.ID,
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
}
|
|
|
|
count, err := organization.CountOrgMembers(ctx, opts)
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
members, _, err := organization.FindOrgMembers(ctx, opts)
|
|
if err != nil {
|
|
ctx.InternalServerError(err)
|
|
return
|
|
}
|
|
|
|
apiMembers := make([]*api.User, len(members))
|
|
for i, member := range members {
|
|
apiMembers[i] = convert.ToUser(ctx, member, ctx.Doer)
|
|
}
|
|
|
|
ctx.SetTotalCountHeader(count)
|
|
ctx.JSON(http.StatusOK, apiMembers)
|
|
}
|
|
|
|
// ListMembers list an organization's members
|
|
func ListMembers(ctx *context.APIContext) {
|
|
// swagger:operation GET /orgs/{org}/members organization orgListMembers
|
|
// ---
|
|
// summary: List an organization's members
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/UserList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
var (
|
|
isMember bool
|
|
err error
|
|
)
|
|
|
|
if ctx.Doer != nil {
|
|
isMember, err = ctx.Org.Organization.IsOrgMember(ctx, ctx.Doer.ID)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
|
return
|
|
}
|
|
}
|
|
listMembers(ctx, isMember)
|
|
}
|
|
|
|
// ListPublicMembers list an organization's public members
|
|
func ListPublicMembers(ctx *context.APIContext) {
|
|
// swagger:operation GET /orgs/{org}/public_members organization orgListPublicMembers
|
|
// ---
|
|
// summary: List an organization's public members
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: page
|
|
// in: query
|
|
// description: page number of results to return (1-based)
|
|
// type: integer
|
|
// - name: limit
|
|
// in: query
|
|
// description: page size of results
|
|
// type: integer
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/UserList"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
listMembers(ctx, false)
|
|
}
|
|
|
|
// IsMember check if a user is a member of an organization
|
|
func IsMember(ctx *context.APIContext) {
|
|
// swagger:operation GET /orgs/{org}/members/{username} organization orgIsMember
|
|
// ---
|
|
// summary: Check if a user is a member of an organization
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: username
|
|
// in: path
|
|
// description: username of the user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// description: user is a member
|
|
// "303":
|
|
// description: redirection to /orgs/{org}/public_members/{username}
|
|
// "404":
|
|
// description: user is not a member
|
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if ctx.Doer != nil {
|
|
userIsMember, err := ctx.Org.Organization.IsOrgMember(ctx, ctx.Doer.ID)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
|
return
|
|
} else if userIsMember || ctx.IsUserSiteAdmin() {
|
|
userToCheckIsMember, err := ctx.Org.Organization.IsOrgMember(ctx, userToCheck.ID)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "IsOrgMember", err)
|
|
} else if userToCheckIsMember {
|
|
ctx.Status(http.StatusNoContent)
|
|
} else {
|
|
ctx.NotFound()
|
|
}
|
|
return
|
|
} else if ctx.Doer.ID == userToCheck.ID {
|
|
ctx.NotFound()
|
|
return
|
|
}
|
|
}
|
|
|
|
redirectURL := setting.AppSubURL + "/api/v1/orgs/" + url.PathEscape(ctx.Org.Organization.Name) + "/public_members/" + url.PathEscape(userToCheck.Name)
|
|
ctx.Redirect(redirectURL)
|
|
}
|
|
|
|
// IsPublicMember check if a user is a public member of an organization
|
|
func IsPublicMember(ctx *context.APIContext) {
|
|
// swagger:operation GET /orgs/{org}/public_members/{username} organization orgIsPublicMember
|
|
// ---
|
|
// summary: Check if a user is a public member of an organization
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: username
|
|
// in: path
|
|
// description: username of the user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// description: user is a public member
|
|
// "404":
|
|
// description: user is not a public member
|
|
|
|
userToCheck := user.GetUserByParams(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
is, err := organization.IsPublicMembership(ctx, ctx.Org.Organization.ID, userToCheck.ID)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "IsPublicMembership", err)
|
|
return
|
|
}
|
|
if is {
|
|
ctx.Status(http.StatusNoContent)
|
|
} else {
|
|
ctx.NotFound()
|
|
}
|
|
}
|
|
|
|
// PublicizeMember make a member's membership public
|
|
func PublicizeMember(ctx *context.APIContext) {
|
|
// swagger:operation PUT /orgs/{org}/public_members/{username} organization orgPublicizeMember
|
|
// ---
|
|
// summary: Publicize a user's membership
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: username
|
|
// in: path
|
|
// description: username of the user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// description: membership publicized
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
userToPublicize := user.GetUserByParams(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if userToPublicize.ID != ctx.Doer.ID {
|
|
ctx.Error(http.StatusForbidden, "", "Cannot publicize another member")
|
|
return
|
|
}
|
|
err := organization.ChangeOrgUserStatus(ctx, ctx.Org.Organization.ID, userToPublicize.ID, true)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// ConcealMember make a member's membership not public
|
|
func ConcealMember(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /orgs/{org}/public_members/{username} organization orgConcealMember
|
|
// ---
|
|
// summary: Conceal a user's membership
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: username
|
|
// in: path
|
|
// description: username of the user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// "$ref": "#/responses/empty"
|
|
// "403":
|
|
// "$ref": "#/responses/forbidden"
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
userToConceal := user.GetUserByParams(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if userToConceal.ID != ctx.Doer.ID {
|
|
ctx.Error(http.StatusForbidden, "", "Cannot conceal another member")
|
|
return
|
|
}
|
|
err := organization.ChangeOrgUserStatus(ctx, ctx.Org.Organization.ID, userToConceal.ID, false)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "ChangeOrgUserStatus", err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// DeleteMember remove a member from an organization
|
|
func DeleteMember(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /orgs/{org}/members/{username} organization orgDeleteMember
|
|
// ---
|
|
// summary: Remove a member from an organization
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: org
|
|
// in: path
|
|
// description: name of the organization
|
|
// type: string
|
|
// required: true
|
|
// - name: username
|
|
// in: path
|
|
// description: username of the user
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "204":
|
|
// description: member removed
|
|
// "404":
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
member := user.GetUserByParams(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if err := models.RemoveOrgUser(ctx, ctx.Org.Organization.ID, member.ID); err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "RemoveOrgUser", err)
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|