It’s illegal if the employer has a local entity and could hire you as an employee and they’re your only gig, but if it’s a foreign company with no way to employ you in your country, what else are you supposed to do? Decline the job because of that? I don’t think that situation would be illegal because there is no legal alternative.