Yes, ghost production is legal when it is structured through a work for hire agreement or a copyright assignment. In both cases, the ghost producer agrees to transfer authorship and economic rights to the buyer in exchange for a fixed fee.
Music industry explanations referenced by Tracklib clearly outline how ghost production works in EDM and why copyright ownership can be contractually reassigned, making the public facing artist the legal rights holder even if they did not create the track.

