You need to look this up specifically for the states involved. It varies. Some neighboring states have reciprocity agreements if they have a major city near a shared border. Frequently what happens is you’ll have to file in both states, but one will give you a non-refundable credit for the tax amount paid in the other.
I suspect that in your situation you’ll end up having to pay the full amount of income tax of the state your employer is in.
Whether that’s legal: states get to do whatever they want to collect taxes.
I guarantee I will not have to file in my home state because I deliberately chose that state for residency because it has no state or personal property taxes and no annual vehicle inspections.