Fiqh-us-Sunnah Volume 2, Supererogatory Prayer, Fiqh 2.058B.
Al-Bukhari records that Ibn ‘Umar prayed behind al-Hajjaj. Muslim records that Abu Sa’id al-Khudri prayed salatul ‘id behind Marwan.
Ibn Mas’ud once prayed four rak’at of fajr, behind al-Walid ibn ‘Uqbah ibn Abu Ma’it who used to drink wine, and ‘Uthman ibn ‘Affan had him flogged. The companions and their successors prayed behind Ibn ‘Ubaid who was accused of propagating heresies. According to the scholars, anyone whose prayer is valid on an individual basis, his imamate is also valid for others. However, they dislike to pray behind an evildoer or innovator.
As-Sa’ib ibn Khilad relates that a man was leading the people in salah and he spat in the direction of the qiblah. The Messenger of Allah saw this and said: “Do not let him lead you in salah.” After this, the man wanted to lead the people in salah but they prevented him and told him what the Prophet had said. The man went to the Prophet sallallahu alehi wasallam, to ask him about that, and the Prophet said: “Yes, [it is true] for you have offended Allah and His Messenger.” This is related by Abu Dawud and Ibn Hibban. Abu Dawud and al-Munzhiri are silent about it.