Anything careless enough to attack a spiny creature in melee will take damage from the defender's spines. Ranged or reaching attacks ignore this counterattack, as do spells.
Each time anything attacks a spiny monster in melee, there is a one in three chance that the attacker will take 5d4 damage. This damage ignores EV, but is reduced by AC. Spiny enemies are also immune to constriction both from the melee attacks of nagas and octopodes and the zombie hands of Borgnjor's Vile Clutch.
You can acquire the spiny trait yourself by gaining the demonspawn spiny mutation. Spiny players have a one in two chance to hurt attackers, doing damage depending on mutation rank and experience level:
damage = mutation rank + 1d(XL + 1) - 1
As with monster spines, this ignores EV. However, you can only affect the first attack of any given monster per round.
Prior to 0.19, the player version did damage based only on the mutation rank.