mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-11-26 10:07:34 -05:00
Updating a permission or a space root permission did not check the roles an administrator enabled with GRAPH_AVAILABLE_ROLES. An invite with a disabled role was rejected, but the same role could be set by updating an existing share. We now validate updates against the allowlist, same as invites. The three "without enabling it" scenarios now expect a 400. Their removed lines are padded with blank lines so the line numbers of the scenarios stay the same. ported from `owncloud/ocis#12540`. The reva half (`owncloud/reva#653`) is https://github.com/opencloud-eu/reva/pull/993 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
247 lines
8.6 KiB
Go
247 lines
8.6 KiB
Go
package validate_test
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
libregraph "github.com/opencloud-eu/libre-graph-api-go"
|
|
|
|
"github.com/opencloud-eu/opencloud/pkg/conversions"
|
|
"github.com/opencloud-eu/opencloud/services/graph/pkg/unifiedrole"
|
|
"github.com/opencloud-eu/opencloud/services/graph/pkg/validate"
|
|
)
|
|
|
|
type validatableFactory[T any] func() (T, bool)
|
|
|
|
var _ = Describe("libregraph", func() {
|
|
var ctx context.Context
|
|
var driveItemInvite libregraph.DriveItemInvite
|
|
var driveRecipient libregraph.DriveRecipient
|
|
|
|
BeforeEach(func() {
|
|
ctx = context.Background()
|
|
driveRecipient = libregraph.DriveRecipient{
|
|
ObjectId: conversions.ToPointer("1"),
|
|
LibreGraphRecipientType: conversions.ToPointer("user"),
|
|
}
|
|
|
|
driveItemInvite = libregraph.DriveItemInvite{
|
|
Recipients: []libregraph.DriveRecipient{driveRecipient},
|
|
Roles: []string{unifiedrole.UnifiedRoleEditorID},
|
|
LibreGraphPermissionsActions: []string{unifiedrole.DriveItemVersionsUpdate},
|
|
ExpirationDateTime: libregraph.PtrTime(time.Now().Add(time.Hour)),
|
|
}
|
|
})
|
|
|
|
DescribeTable("DriveItemInvite",
|
|
func(factories ...validatableFactory[libregraph.DriveItemInvite]) {
|
|
for _, factory := range factories {
|
|
s, pass := factory()
|
|
switch err := validate.StructCtx(ctx, s); pass {
|
|
case false:
|
|
Expect(err).To(HaveOccurred())
|
|
default:
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
}
|
|
},
|
|
Entry("succeed: roles only", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.LibreGraphPermissionsActions = nil
|
|
return driveItemInvite, true
|
|
}),
|
|
Entry("succeed: actions only", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
return driveItemInvite, true
|
|
}),
|
|
Entry("succeed: no ExpirationDateTime", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.ExpirationDateTime = nil
|
|
return driveItemInvite, true
|
|
}),
|
|
Entry("fail: multiple role assignment", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = []string{
|
|
unifiedrole.UnifiedRoleEditorID,
|
|
unifiedrole.UnifiedRoleManagerID,
|
|
}
|
|
driveItemInvite.LibreGraphPermissionsActions = nil
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: unknown role", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = []string{"foo"}
|
|
driveItemInvite.LibreGraphPermissionsActions = nil
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: disabled role", func() (libregraph.DriveItemInvite, bool) {
|
|
ctx = validate.ContextWithAllowedRoleIDs(ctx, []string{unifiedrole.UnifiedRoleEditorID})
|
|
driveItemInvite.Roles = []string{
|
|
unifiedrole.UnifiedRoleSecureViewerID,
|
|
}
|
|
driveItemInvite.LibreGraphPermissionsActions = nil
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: unknown action", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.LibreGraphPermissionsActions = []string{"foo"}
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: missing roles or permission actions", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.LibreGraphPermissionsActions = nil
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: different number of roles and actions", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.LibreGraphPermissionsActions = []string{
|
|
unifiedrole.DriveItemVersionsUpdate,
|
|
unifiedrole.DriveItemChildrenCreate,
|
|
}
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: missing recipients", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.Recipients = nil
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: dive recipients", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.Recipients = []libregraph.DriveRecipient{
|
|
{ObjectId: libregraph.PtrString("1"), LibreGraphRecipientType: libregraph.PtrString("dive")},
|
|
}
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: more than 1 recipient", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.Recipients = []libregraph.DriveRecipient{
|
|
driveRecipient,
|
|
{ObjectId: libregraph.PtrString("2"), LibreGraphRecipientType: libregraph.PtrString("group")},
|
|
}
|
|
return driveItemInvite, false
|
|
}),
|
|
Entry("fail: expirationDateTime in the past", func() (libregraph.DriveItemInvite, bool) {
|
|
driveItemInvite.Roles = nil
|
|
driveItemInvite.ExpirationDateTime = libregraph.PtrTime(time.Now().Add(-time.Hour))
|
|
return driveItemInvite, false
|
|
}),
|
|
)
|
|
|
|
// Permission is validated when updating a permission (PATCH). It must honour the
|
|
// admin role allowlist just like DriveItemInvite does.
|
|
DescribeTable("Permission",
|
|
func(factory validatableFactory[libregraph.Permission]) {
|
|
s, pass := factory()
|
|
switch err := validate.StructCtx(ctx, s); pass {
|
|
case false:
|
|
Expect(err).To(HaveOccurred())
|
|
default:
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
},
|
|
Entry("fail: disabled role rejected when allowlist is set", func() (libregraph.Permission, bool) {
|
|
ctx = validate.ContextWithAllowedRoleIDs(ctx, []string{unifiedrole.UnifiedRoleEditorID})
|
|
return libregraph.Permission{Roles: []string{unifiedrole.UnifiedRoleSecureViewerID}}, false
|
|
}),
|
|
Entry("succeed: allowed role accepted when allowlist is set", func() (libregraph.Permission, bool) {
|
|
ctx = validate.ContextWithAllowedRoleIDs(ctx, []string{unifiedrole.UnifiedRoleEditorID})
|
|
return libregraph.Permission{Roles: []string{unifiedrole.UnifiedRoleEditorID}}, true
|
|
}),
|
|
)
|
|
|
|
DescribeTable("DriveRecipient",
|
|
func(factories ...validatableFactory[libregraph.DriveRecipient]) {
|
|
for _, factory := range factories {
|
|
s, pass := factory()
|
|
switch err := validate.StructCtx(context.Background(), s); pass {
|
|
case false:
|
|
Expect(err).To(HaveOccurred())
|
|
default:
|
|
Expect(err).ToNot(HaveOccurred())
|
|
}
|
|
}
|
|
},
|
|
Entry("succeed: user recipient",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.LibreGraphRecipientType = conversions.ToPointer("user")
|
|
return driveRecipient, true
|
|
},
|
|
),
|
|
Entry("succeed: group recipient",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.LibreGraphRecipientType = conversions.ToPointer("group")
|
|
return driveRecipient, true
|
|
},
|
|
),
|
|
Entry("succeed: email recipient",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = conversions.ToPointer("guest@example.com")
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, true
|
|
},
|
|
),
|
|
Entry("fail: no objectId and no email",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = nil
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: empty objectId",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = conversions.ToPointer("")
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: objectId and email both set",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.Email = conversions.ToPointer("guest@example.com")
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: objectId without recipient type",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: objectId with invalid recipient type",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.LibreGraphRecipientType = conversions.ToPointer("foo")
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: email recipient with recipient type set",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = conversions.ToPointer("guest@example.com")
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: invalid email",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = conversions.ToPointer("invalid")
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: empty email",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = conversions.ToPointer(" ")
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
Entry("fail: email with display name",
|
|
func() (libregraph.DriveRecipient, bool) {
|
|
driveRecipient.ObjectId = nil
|
|
driveRecipient.Email = conversions.ToPointer("Test User <guest@example.com>")
|
|
driveRecipient.LibreGraphRecipientType = nil
|
|
return driveRecipient, false
|
|
},
|
|
),
|
|
)
|
|
})
|