However, that does not mean that it is allowed to do whatever it wants. In the 50s, Italy surrounded it with tanks and forced a regime change when San Marino had started leaning dangerously towards the Soviet block.
Probably because of this precedent, recently San Marino has made a diplomatic U-turn with Russia: after decades of very good relationships (I personally saw Lavrov there once, shops have labels in Russian, etc) after the invasion of Ukraine it sided with the west and ended on the list of countries hostile to Russia.
Also, it was a black-listed tax haven which was tolerated by Italy until it went too far; then the Finance minister of Italy surrounded the republic with Guardia di Finanza (finance police)... San Marino has no access to the sea (despite being very near to it), no airport (besides a tiny airfield), and only borders with Italy, so it has very limited leeway.
And yes, it is a member of the UN (not of the EU though).