Skip to content

chore: Return copied templates to prevent reference issues #6679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions coderd/database/dbfake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,7 @@ func (q *fakeQuerier) GetTemplateByID(ctx context.Context, id uuid.UUID) (databa
func (q *fakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
for _, template := range q.templates {
if template.ID == id {
return template, nil
return template.DeepCopy(), nil
}
}
return database.Template{}, sql.ErrNoRows
Expand All @@ -1785,7 +1785,7 @@ func (q *fakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
if template.Deleted != arg.Deleted {
continue
}
return template, nil
return template.DeepCopy(), nil
}
return database.Template{}, sql.ErrNoRows
}
Expand All @@ -1808,7 +1808,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
tpl.Description = arg.Description
tpl.Icon = arg.Icon
q.templates[idx] = tpl
return tpl, nil
return tpl.DeepCopy(), nil
}

return database.Template{}, sql.ErrNoRows
Expand All @@ -1830,7 +1830,7 @@ func (q *fakeQuerier) UpdateTemplateScheduleByID(_ context.Context, arg database
tpl.DefaultTTL = arg.DefaultTTL
tpl.MaxTTL = arg.MaxTTL
q.templates[idx] = tpl
return tpl, nil
return tpl.DeepCopy(), nil
}

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

templates := slices.Clone(q.templates)
for i := range templates {
templates[i] = templates[i].DeepCopy()
}
slices.SortFunc(templates, func(i, j database.Template) bool {
if i.Name != j.Name {
return i.Name < j.Name
Expand Down Expand Up @@ -2775,7 +2778,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
AllowUserCancelWorkspaceJobs: arg.AllowUserCancelWorkspaceJobs,
}
q.templates = append(q.templates, template)
return template, nil
return template.DeepCopy(), nil
}

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

q.templates[i] = template
return template, nil
return template.DeepCopy(), nil
}
}

Expand Down
9 changes: 9 additions & 0 deletions coderd/database/modelmethods.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"sort"
"strconv"

"golang.org/x/exp/maps"

"github.com/coder/coder/coderd/rbac"
)

Expand Down Expand Up @@ -86,6 +88,13 @@ func (t Template) RBACObject() rbac.Object {
WithGroupACL(t.GroupACL)
}

func (t Template) DeepCopy() Template {
cpy := t
cpy.UserACL = maps.Clone(t.UserACL)
cpy.GroupACL = maps.Clone(t.GroupACL)
return cpy
}

func (TemplateVersion) RBACObject(template Template) rbac.Object {
// Just use the parent template resource for controlling versions
return template.RBACObject()
Expand Down