It means to injure yourself in order to harm an adversary (or variations close to that) and carries the idea that revenge or anger motivates the self-inflicted damage. And the phrase apparently comes from England in the 1500s. Where most of the really interesting and useful phrases do.
Which also means that I’ve never used this phrase correctly. I generally use it in the sense that a person is too stubborn to willingly do something, despite surmounting evidence of said thing’s benefit. The word “spite” really should have clued me into the violent elements of the phrase, but there you have it.
So if you’ve ever wondered, like me, about the phrase, now you know. What other words or phrases make you curious? I think I’ll have to look into more because words are fantastically fun.