Skip to content

Commit 65945ae

Browse files
authored
chore: Return copied templates to prevent reference issues (#6679)
1 parent cb846ba commit 65945ae

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

coderd/database/dbfake/databasefake.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ func (q *fakeQuerier) GetTemplateByID(ctx context.Context, id uuid.UUID) (databa
17611761
func (q *fakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
17621762
for _, template := range q.templates {
17631763
if template.ID == id {
1764-
return template, nil
1764+
return template.DeepCopy(), nil
17651765
}
17661766
}
17671767
return database.Template{}, sql.ErrNoRows
@@ -1785,7 +1785,7 @@ func (q *fakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
17851785
if template.Deleted != arg.Deleted {
17861786
continue
17871787
}
1788-
return template, nil
1788+
return template.DeepCopy(), nil
17891789
}
17901790
return database.Template{}, sql.ErrNoRows
17911791
}
@@ -1808,7 +1808,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
18081808
tpl.Description = arg.Description
18091809
tpl.Icon = arg.Icon
18101810
q.templates[idx] = tpl
1811-
return tpl, nil
1811+
return tpl.DeepCopy(), nil
18121812
}
18131813

18141814
return database.Template{}, sql.ErrNoRows
@@ -1830,7 +1830,7 @@ func (q *fakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
18301830
tpl.DefaultTTL = arg.DefaultTTL
18311831
tpl.MaxTTL = arg.MaxTTL
18321832
q.templates[idx] = tpl
1833-
return tpl, nil
1833+
return tpl.DeepCopy(), nil
18341834
}
18351835

18361836
return database.Template{}, sql.ErrNoRows
@@ -1889,7 +1889,7 @@ func (q *fakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
18891889
continue
18901890
}
18911891
}
1892-
templates = append(templates, template)
1892+
templates = append(templates, template.DeepCopy())
18931893
}
18941894
if len(templates) > 0 {
18951895
slices.SortFunc(templates, func(i, j database.Template) bool {
@@ -2190,6 +2190,9 @@ func (q *fakeQuerier) GetTemplates(_ context.Context) ([]database.Template, erro
21902190
defer q.mutex.RUnlock()
21912191

21922192
templates := slices.Clone(q.templates)
2193+
for i := range templates {
2194+
templates[i] = templates[i].DeepCopy()
2195+
}
21932196
slices.SortFunc(templates, func(i, j database.Template) bool {
21942197
if i.Name != j.Name {
21952198
return i.Name < j.Name
@@ -2775,7 +2778,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
27752778
AllowUserCancelWorkspaceJobs: arg.AllowUserCancelWorkspaceJobs,
27762779
}
27772780
q.templates = append(q.templates, template)
2778-
return template, nil
2781+
return template.DeepCopy(), nil
27792782
}
27802783

27812784
func (q *fakeQuerier) InsertTemplateVersion(_ context.Context, arg database.InsertTemplateVersionParams) (database.TemplateVersion, error) {
@@ -3403,7 +3406,7 @@ func (q *fakeQuerier) UpdateTemplateACLByID(_ context.Context, arg database.Upda
34033406
template.UserACL = arg.UserACL
34043407

34053408
q.templates[i] = template
3406-
return template, nil
3409+
return template.DeepCopy(), nil
34073410
}
34083411
}
34093412

coderd/database/modelmethods.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"sort"
55
"strconv"
66

7+
"golang.org/x/exp/maps"
8+
79
"github.com/coder/coder/coderd/rbac"
810
)
911

@@ -86,6 +88,13 @@ func (t Template) RBACObject() rbac.Object {
8688
WithGroupACL(t.GroupACL)
8789
}
8890

91+
func (t Template) DeepCopy() Template {
92+
cpy := t
93+
cpy.UserACL = maps.Clone(t.UserACL)
94+
cpy.GroupACL = maps.Clone(t.GroupACL)
95+
return cpy
96+
}
97+
8998
func (TemplateVersion) RBACObject(template Template) rbac.Object {
9099
// Just use the parent template resource for controlling versions
91100
return template.RBACObject()

0 commit comments

Comments
 (0)