I did some research on this issue before. Generally, it is a "blur" area and
no one can tell you exactly if it is legal or not. In reality, no H4 holder
had ever been "punished" for working for a foreign company.
But it does not mean you won't have problems. You need consider 2 things:
1. whether or not the company has a physical existence in US soil. If yes,
the company will be subject to US laws and thus it may not be legal to hire
you if you don't have EAD;
2. whether or not the job requires the employee's physical existence in US.
If yes, the job is considered a "US job". Note the law's purpose (or merit,
or intention, I fogot the term) is to protect US labors. So you taking the
job will be against its purpose.