Skip to content

Commit 99e11e7

Browse files
committed
defensively handle nil maps and slices in marshaling
1 parent dc5f69e commit 99e11e7

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

coderd/idpsync/group.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,17 @@ func (s *GroupSyncSettings) String() string {
274274
return runtimeconfig.JSONString(s)
275275
}
276276

277+
func (s *GroupSyncSettings) MarshalJSON() ([]byte, error) {
278+
if s.Mapping == nil {
279+
s.Mapping = make(map[string][]uuid.UUID)
280+
}
281+
282+
// Aliasing the struct to avoid infinite recursion when calling json.Marshal
283+
// on the struct itself.
284+
type Alias GroupSyncSettings
285+
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
286+
}
287+
277288
type ExpectedGroup struct {
278289
OrganizationID uuid.UUID
279290
GroupID *uuid.UUID

coderd/idpsync/organization.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ func (s *OrganizationSyncSettings) String() string {
234234
return runtimeconfig.JSONString(s)
235235
}
236236

237+
func (s *OrganizationSyncSettings) MarshalJSON() ([]byte, error) {
238+
if s.Mapping == nil {
239+
s.Mapping = make(map[string][]uuid.UUID)
240+
}
241+
242+
// Aliasing the struct to avoid infinite recursion when calling json.Marshal
243+
// on the struct itself.
244+
type Alias OrganizationSyncSettings
245+
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
246+
}
247+
237248
// ParseClaims will parse the claims and return the list of organizations the user
238249
// should sync to.
239250
func (s *OrganizationSyncSettings) ParseClaims(ctx context.Context, db database.Store, mergedClaims jwt.MapClaims) ([]uuid.UUID, error) {

coderd/idpsync/role.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,14 @@ func (s *RoleSyncSettings) String() string {
291291
}
292292
return runtimeconfig.JSONString(s)
293293
}
294+
295+
func (s *RoleSyncSettings) MarshalJSON() ([]byte, error) {
296+
if s.Mapping == nil {
297+
s.Mapping = make(map[string][]string)
298+
}
299+
300+
// Aliasing the struct to avoid infinite recursion when calling json.Marshal
301+
// on the struct itself.
302+
type Alias RoleSyncSettings
303+
return json.Marshal(&struct{ *Alias }{Alias: (*Alias)(s)})
304+
}

enterprise/coderd/idpsync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ func (api *API) idpSyncClaimFieldValues(orgID uuid.UUID, rw http.ResponseWriter,
836836
httpapi.InternalServerError(rw, err)
837837
return
838838
}
839+
if fieldValues == nil {
840+
fieldValues = []string{}
841+
}
839842

840843
httpapi.Write(ctx, rw, http.StatusOK, fieldValues)
841844
}

0 commit comments

Comments
 (0)