In the US, it stems from some statute, such as RCW Ch. 71.05 in Washington state, which starts by stating the rationale for the law, and which are to protect the health and safety of persons suffering from behavioral health disorders and to protect public safety, and prevent inappropriate commitment of persons living with behavioral disorders and to eliminate legal disabilities that arise from involuntary commitment etc. The various laws in this chapter make it possible to commit a person to a mental institute or to undergo involuntary therapy. There is a separate chapter, RCW 71.34, applicable to minors.
In general, this law calls for professional evaluation and treatment, without legislating science. The trigger is generally evidence of a tendency towards serious harm or grave disability, with a requirement that the action be requested by a certain kind of behavioral professional. There are not many hard-coded limits on what can be ordered: while RCW 71.05.215 "has a right to refuse antipsychotic medication", that right is overridden when "it is determined that the failure to medicate may result in a likelihood of serious harm or substantial deterioration or substantially prolong the length of involuntary commitment and there is no less intrusive course of treatment than medication in the best interest of that person".
Along with the legislation cited, there are also regulations which don't require legislative action (they are "empowered" by the statutes), which could specifically forbid a treatment, but again choice of treatments are left to the professional. In some other jurisdiction, it's possible that a certain treatment would be explicitly outlawed.