Skip to content

Commit 7685eb4

Browse files
committed
Require custom: prefix for permissions defined at app level
1 parent d99c5e5 commit 7685eb4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

internal/server/rbac.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
const RBAC_AUTH_PREFIX = "rbac:"
1616
const RBAC_GROUP_PREFIX = "group:"
1717
const RBAC_ROLE_PREFIX = "role:"
18+
const RBAC_CUSTOM_PREFIX = "custom:" // used for app level custom permissions
1819

1920
type RBACManager struct {
2021
*types.Logger
@@ -65,13 +66,13 @@ func (h *RBACManager) Authorize(user string, appPathDomain types.AppPathDomain,
6566
appPathDomain.Path = strings.TrimSuffix(appPathDomain.Path, types.STAGE_SUFFIX)
6667
appPathDomain.Path = strings.TrimSuffix(appPathDomain.Path, types.PREVIEW_SUFFIX)
6768

68-
return h.checkGrants(user, appPathDomain, permission, groups)
69+
return h.checkGrants(user, appPathDomain, permission, groups, isAppLevelPermission)
6970
}
7071

7172
func (h *RBACManager) checkGrants(inputUser string, appPathDomain types.AppPathDomain,
72-
inputPermission types.RBACPermission, groups []string) (bool, error) {
73+
inputPermission types.RBACPermission, groups []string, isAppLevelPermission bool) (bool, error) {
7374
for _, grant := range h.rbacConfig.Grants {
74-
match, err := h.checkGrant(grant, inputUser, appPathDomain, inputPermission, groups)
75+
match, err := h.checkGrant(grant, inputUser, appPathDomain, inputPermission, groups, isAppLevelPermission)
7576
if err != nil {
7677
return false, err
7778
}
@@ -82,12 +83,13 @@ func (h *RBACManager) checkGrants(inputUser string, appPathDomain types.AppPathD
8283
return true, nil
8384
}
8485
}
85-
h.Debug().Msgf("Denied user %s access to app %s with permission %s", inputUser, appPathDomain.String(), inputPermission)
86+
h.Debug().Msgf("Denied user %s access to app %s with permission %s app level %t",
87+
inputUser, appPathDomain.String(), inputPermission, isAppLevelPermission)
8688
return false, nil
8789
}
8890

8991
func (h *RBACManager) checkGrant(grant types.RBACGrant, inputUser string, appPathDomain types.AppPathDomain,
90-
inputPermission types.RBACPermission, groups []string) (bool, error) {
92+
inputPermission types.RBACPermission, groups []string, isAppLevelPermission bool) (bool, error) {
9193
userMatched := false
9294
for _, user := range grant.Users {
9395
if strings.HasPrefix(user, RBAC_GROUP_PREFIX) {
@@ -113,6 +115,10 @@ func (h *RBACManager) checkGrant(grant types.RBACGrant, inputUser string, appPat
113115
}
114116

115117
// user matched, check if role matches
118+
if isAppLevelPermission {
119+
// app level permission, look for grant with custom: prefix
120+
inputPermission = types.RBACPermission(RBAC_CUSTOM_PREFIX + string(inputPermission))
121+
}
116122
roleMatched := false
117123
for _, role := range grant.Roles {
118124
if slices.Contains(h.roles[role], inputPermission) {

internal/server/rbac_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ func TestAuthorizeAppLevelPermissions(t *testing.T) {
10701070
}
10711071

10721072
allowed, err := rbacManager.Authorize("user1", types.AppPathDomain{Path: "/test", Domain: ""}, "rbac:test",
1073-
types.RBACPermission("custom"), []string{}, true)
1073+
types.RBACPermission("action_run"), []string{}, true)
10741074
if err != nil {
10751075
t.Fatalf("unexpected error: %v", err)
10761076
}
@@ -1086,7 +1086,7 @@ func TestAuthorizeAppLevelPermissions(t *testing.T) {
10861086
Enabled: true,
10871087
Groups: map[string][]string{},
10881088
Roles: map[string][]types.RBACPermission{
1089-
"actor": {types.RBACPermission("action_run"), types.RBACPermission("custom")},
1089+
"actor": {types.RBACPermission("custom:action_run")},
10901090
},
10911091
Grants: []types.RBACGrant{
10921092
{

0 commit comments

Comments
 (0)