diff --git a/server/service/apple_mdm.go b/server/service/apple_mdm.go index 42ebd4a9e4..9765398478 100644 --- a/server/service/apple_mdm.go +++ b/server/service/apple_mdm.go @@ -397,6 +397,17 @@ func (svc *Service) NewMDMAppleConfigProfile(ctx context.Context, teamID uint, r return nil, ctxerr.Wrap(ctx, fleet.NewInvalidArgumentError("profile", err.Error())) } + // We validate Fleet variables before we unmarshal the profile because bad variables can break unmarshal. + // For example: $FLEET_VAR_BOZO + appConfig, err := svc.ds.AppConfig(ctx) + if err != nil { + return nil, ctxerr.Wrap(ctx, err) + } + err = validateConfigProfileFleetVariables(appConfig, expanded) + if err != nil { + return nil, ctxerr.Wrap(ctx, err, "validating fleet variables") + } + cp, err := fleet.NewMDMAppleConfigProfile([]byte(expanded), &teamID) if err != nil { return nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{ @@ -410,14 +421,6 @@ func (svc *Service) NewMDMAppleConfigProfile(ctx context.Context, teamID uint, r } return nil, ctxerr.Wrap(ctx, &fleet.BadRequestError{Message: err.Error()}) } - appConfig, err := svc.ds.AppConfig(ctx) - if err != nil { - return nil, ctxerr.Wrap(ctx, err) - } - err = validateConfigProfileFleetVariables(appConfig, string(cp.Mobileconfig)) - if err != nil { - return nil, ctxerr.Wrap(ctx, err, "validating fleet variables") - } // Save the original unexpanded profile cp.Mobileconfig = b