Skip to content

Commit

Permalink
fix: Prevent possible out-of-bounds access when loading policies (#8186)
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <jann@mistrust.net>
  • Loading branch information
jannfis authored Jan 15, 2022
1 parent 3c5033c commit d33caac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions util/rbac/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ func loadPolicyLine(line string, model model.Model) error {
return err
}

if len(tokens) < 2 || len(tokens[0]) < 1 {
return fmt.Errorf("invalid RBAC policy: %s", line)
}

key := tokens[0]
sec := key[:1]
if _, ok := model[sec]; !ok {
Expand Down
40 changes: 40 additions & 0 deletions util/rbac/rbac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
Expand Down Expand Up @@ -399,3 +400,42 @@ func TestGlobMatchFunc(t *testing.T) {
ok, _ = globMatchFunc("arg/123", "arg/*")
assert.True(t, ok.(bool))
}

func TestLoadPolicyLine(t *testing.T) {
t.Run("Valid policy line", func(t *testing.T) {
policy := `p, foo, bar, baz`
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.NoError(t, err)
})
t.Run("Empty policy line", func(t *testing.T) {
policy := ""
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.NoError(t, err)
})
t.Run("Comment policy line", func(t *testing.T) {
policy := "# Some comment"
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.NoError(t, err)
})
t.Run("Invalid policy line: single token", func(t *testing.T) {
policy := "p"
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.Error(t, err)
})
t.Run("Invalid policy line: plain text", func(t *testing.T) {
policy := "Some comment"
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.Error(t, err)
})
t.Run("Invalid policy line", func(t *testing.T) {
policy := "agh, foo, bar"
model := newBuiltInModel()
err := loadPolicyLine(policy, model)
require.Error(t, err)
})
}

0 comments on commit d33caac

Please sign in to comment.