Question: What is the path to safety from tribulation? For we see many of us repeatedly entering into tribulation, so we fear for ourselves?
Answer: The path to safety from being tempted is caution against committing the disobedience of Allah—whatever is small of it and whatever is great—and eagerness to fulfill what Allah commanded His servants. Then, humbling oneself to Allah—Exalted is He—by supplication for protection, success, and rightness, and maintaining constant seeking of forgiveness.
That is because tribulations are a recompense from Allah—Exalted is He—for the disobedient due to their disobedience, as evidenced by His—Exalted is He—saying regarding the people of the Sabbath: "Thus We test them because they were defiantly disobedient" [Al-A‘raf], and His—Exalted is He—saying: "Then the outcome of those who did evil was the worst, because they denied the signs of Allah and used to mock them" [Ar-Rum].
Source: Min Thimār al-ʿIlm wa al-Ḥikmah vol.3