From f37ad6f0bdeb74edb672a1f4113a07d970b925a6 Mon Sep 17 00:00:00 2001 From: Neil Twigg Date: Tue, 26 Mar 2024 10:55:07 +0000 Subject: [PATCH] Tweak `clone` behaviour of `User` and `NkeyUser` In this case, an empty-but-non-nil map would not be copied, which could result in the clone referring to the original map. Also implement a deep copy of `AllowedConnectionTypes` for `NkeyUser`, like `User` already has. Signed-off-by: Neil Twigg --- server/auth.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/auth.go b/server/auth.go index b37d245ec40..d3da05d1701 100644 --- a/server/auth.go +++ b/server/auth.go @@ -83,9 +83,10 @@ func (u *User) clone() *User { } clone := &User{} *clone = *u + // Account is not cloned because it is always by reference to an existing struct. clone.Permissions = u.Permissions.clone() - if len(u.AllowedConnectionTypes) > 0 { + if u.AllowedConnectionTypes != nil { clone.AllowedConnectionTypes = make(map[string]struct{}) for k, v := range u.AllowedConnectionTypes { clone.AllowedConnectionTypes[k] = v @@ -103,7 +104,16 @@ func (n *NkeyUser) clone() *NkeyUser { } clone := &NkeyUser{} *clone = *n + // Account is not cloned because it is always by reference to an existing struct. clone.Permissions = n.Permissions.clone() + + if n.AllowedConnectionTypes != nil { + clone.AllowedConnectionTypes = make(map[string]struct{}) + for k, v := range n.AllowedConnectionTypes { + clone.AllowedConnectionTypes[k] = v + } + } + return clone }