Two men came to visit the Prophet ﷺ, and he spoke harshly to them and rebuked them.She said, I asked, "Messenger of Allah, no one else who has been in your presence has come away with what these two came away with."He replied, "Don't you know what I have promised my Lord ? I said, 'O Allah, any believer whom I rebuke, strike, or curse, make that for him a means of forgiveness, well-being, and such and such.'"
حدثنا أبو معاوية وابن نمير المعنى قالا حدثنا الأعمش عن مسلم عن مسروق
دخل على النبي ﷺ رجلان فأغلظ لهما وسبهما قالت فقلت يا رسول الله لمن أصاب منك خيرا ما أصاب هذان منك خيرا قالت فقال أوما علمت ما عاهدت عليه ربي عز وجل قال قلت اللهم أيما مؤمن سببته أو جلدته أو لعنته فاجعلها له مغفرة وعافية وكذا وكذا
Its isnad is sahih, and it is in Muslim with a similar wording, 4/2007, no. 2600, in the chapter on righteousness, about those cursed by the Prophet ﷺ.