The Prophet ﷺ said, "Allah has pardoned my nation for whatever crosses their minds, as long as they do not speak of it or act upon it."
حدثنا أبو نعيم حدثنا هشام عن قتادة عن زرارة بن أوفى
عن النبي ﷺ قال إن الله تجاوز لأمتي عما حدثت به أنفسها ما لم تكلم به أو تعمل به
Its isnad is sahih, previously mentioned at 6466. It was also related by Bukhari 3/190, in the chapter on emancipation, on mistake and forgetfulness in manumission, and Muslim 1/116 no. 127, in the chapter on faith, on Allah overlooking fleeting thoughts, and Abu Dawud 2/264 no. 2209, in the chapter on divorce, on intrusive thoughts about divorce, and Nasai 6/157 no. 3435, and Ibn Majah 1/658 no. 2040.