From 45d69fafa18906cd5e412f78c69d5f618d6437d7 Mon Sep 17 00:00:00 2001 From: Jon Ayers Date: Thu, 14 Dec 2023 03:54:10 +0000 Subject: [PATCH] fix: prevent data race when mutating tags --- provisionersdk/provisionertags.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/provisionersdk/provisionertags.go b/provisionersdk/provisionertags.go index 970cf2094dd74..3071d0f5356f7 100644 --- a/provisionersdk/provisionertags.go +++ b/provisionersdk/provisionertags.go @@ -16,21 +16,25 @@ const ( // own their own operations. // Otherwise, the "owner" tag is always empty. func MutateTags(userID uuid.UUID, tags map[string]string) map[string]string { - if tags == nil { - tags = map[string]string{} + // We copy the tags here to avoid overwriting the provided map. This can + // cause data races when using dbmem. + cp := map[string]string{} + for k, v := range tags { + cp[k] = v } - _, ok := tags[TagScope] + + _, ok := cp[TagScope] if !ok { - tags[TagScope] = ScopeOrganization - delete(tags, TagOwner) + cp[TagScope] = ScopeOrganization + delete(cp, TagOwner) } - switch tags[TagScope] { + switch cp[TagScope] { case ScopeUser: - tags[TagOwner] = userID.String() + cp[TagOwner] = userID.String() case ScopeOrganization: - delete(tags, TagOwner) + delete(cp, TagOwner) default: - tags[TagScope] = ScopeOrganization + cp[TagScope] = ScopeOrganization } - return tags + return cp }