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.
https://www.cnn.com/2022/03/01/success/state-income-tax-ramifications-remote-work/index.html