It's not completely illegal, there are some costs associated with an H1B application that it is legal for the employee to pay. Just not the entire cost.
USCIS requires H1B petitions filed for an employee to be paid by the petitioner (employer). All those shady sweat shops who used to take poor Joe Schmoes' money for filing got raided big time in NJ and other areas.