It needs to come within a few meters of a ship's metal hull. Ouch. But it should also have a fuse that won't arm until it has traveled some distance from the vessel.
I assume torpedoes have a number of failsafes on them, for example I heard of one that can disarm the torpedo if it does a 180, to keep ships from torpedoing themselves (it happens). One Kursk theory was that a torpedo ran hot in it's rack, and the captain attempted a 180 to disarm it (but it blew up anyway).