From 4c20fe661d8c325733d286aed34ad2f2dd1eb9be Mon Sep 17 00:00:00 2001 From: Vladislav Fursov Date: Wed, 9 Jan 2019 22:45:08 +0900 Subject: [PATCH] fix panic if permission is nil --- rbac_test.go | 4 ++++ role.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/rbac_test.go b/rbac_test.go index 9e89e37..69d507a 100644 --- a/rbac_test.go +++ b/rbac_test.go @@ -126,6 +126,10 @@ func TestRbacPermission(t *testing.T) { if rbac.IsGranted("role-c", pB, nil) { t.Fatalf("role-c should not have %s because of the unbinding with role-b", pB) } + + if rbac.IsGranted("role-a", nil, nil) { + t.Fatal("role-a should not have nil permission") + } } func BenchmarkRbacGranted(b *testing.B) { diff --git a/role.go b/role.go index 4fe652f..1862a28 100644 --- a/role.go +++ b/role.go @@ -48,6 +48,10 @@ func (role *StdRole) Assign(p Permission) error { // Permit returns true if the role has specific permission. func (role *StdRole) Permit(p Permission) (rslt bool) { + if p == nil { + return false + } + role.RLock() for _, rp := range role.permissions { if rp.Match(p) {