Skip to content

Commit

Permalink
enforce the publiclink.create permission
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Feb 28, 2023
1 parent 1efcdab commit f63cdab
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/public-link-permission.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Enforce the publiclink.create permission

Added checks for the "publiclink.create" permission when creating or updating public links.

https://github.com/cs3org/reva/pull/3693
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"strconv"

permissionsv1beta1 "github.com/cs3org/go-cs3apis/cs3/permissions/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
Expand Down Expand Up @@ -52,6 +53,30 @@ func (h *Handler) createPublicLinkShare(w http.ResponseWriter, r *http.Request,
}
}

user := ctxpkg.ContextMustGetUser(ctx)
resp, err := c.CheckPermission(ctx, &permissionsv1beta1.CheckPermissionRequest{
SubjectRef: &permissionsv1beta1.SubjectReference{
Spec: &permissionsv1beta1.SubjectReference_UserId{
UserId: user.Id,
},
},
Permission: "publiclink.create",
})
if err != nil {
return nil, &ocsError{
Code: response.MetaServerError.StatusCode,
Message: "failed to check user permission",
Error: err,
}
}

if resp.Status.Code != rpc.Code_CODE_OK {
return nil, &ocsError{
Code: response.MetaForbidden.StatusCode,
Message: "user is not allowed to create a public link",
}
}

err = r.ParseForm()
if err != nil {
return nil, &ocsError{
Expand Down Expand Up @@ -270,6 +295,27 @@ func (h *Handler) updatePublicShare(w http.ResponseWriter, r *http.Request, shar
return
}

ctx := r.Context()

user := ctxpkg.ContextMustGetUser(ctx)
resp, err := gwC.CheckPermission(ctx, &permissionsv1beta1.CheckPermissionRequest{
SubjectRef: &permissionsv1beta1.SubjectReference{
Spec: &permissionsv1beta1.SubjectReference_UserId{
UserId: user.Id,
},
},
Permission: "publiclink.create",
})
if err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "failed to check user permission", err)
return
}

if resp.Status.Code != rpc.Code_CODE_OK {
response.WriteOCSError(w, r, response.MetaForbidden.StatusCode, "user is not allowed to create a public link", nil)
return
}

before, err := gwC.GetPublicShare(r.Context(), &link.GetPublicShareRequest{
Ref: &link.PublicShareReference{
Spec: &link.PublicShareReference_Id{
Expand Down
2 changes: 2 additions & 0 deletions pkg/permission/manager/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (m manager) CheckPermission(perm string, subject string, ref *provider.Refe
// TODO Users can only create their own personal space
// TODO guest accounts cannot create spaces
return true
case permission.CreatePublicLink:
return true
case permission.ListAllSpaces:
// TODO introduce an admin role to allow listing all spaces
return false
Expand Down
2 changes: 2 additions & 0 deletions pkg/permission/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
ListAllSpaces string = "list-all-spaces"
// CreateSpace is the hardcoded name for the create space permission
CreateSpace string = "create-space"
// CreatePublicLink is the hardcoded name for the publiclink.create permission
CreatePublicLink string = "publiclink.create"
)

// Manager defines the interface for the permission service driver
Expand Down

0 comments on commit f63cdab

Please sign in to comment.