The Messenger of Allah ﷺ said, "Allah has pardoned my community for whatever crosses their minds, so long as they do not act upon it or speak of it."
حدثنا أبو بكر بن أبي شيبة حدثنا علي بن مسهر وعبدة بن سليمان ح و حدثنا حميد بن مسعدة حدثنا خالد بن الحارث جميعا عن سعيد بن أبي عروبة عن قتادة عن زرارة بن أوفى
قال رسول الله ﷺ إن الله تجاوز لأمتي عما حدثت به أنفسها ما لم تعمل به أو تكلم به
Bukhari, Emancipation, chapter on mistake and forgetfulness in emancipation and divorce and the like, hadith no. 2528, 5269, 6664, and Muslim, Faith, chapter on Allah's overlooking of stray thoughts and passing notions in the heart when they do not settle, hadith no. 127, through various routes from Qatada.