Safety concerns during the operation of legged robots must be addressed to enable their widespread use. Machine learning-based control methods that use model-based constraints provide promising means to improve robot safety. This study presents a modular safety filter to improve the safety of a legged robot, i.e., reduce the chance of a fall. The prerequisite is the availability of a robot that is capable of locomotion, i.e., a nominal controller exists. During locomotion, terrain properties around the robot are estimated through machine learning which uses a minimal set of proprioceptive signals. A novel deep-learning model utilizing an efficient transformer architecture is used for the terrain estimation. A quadratic program combines the terrain estimations with inverse dynamics and a novel exponential control barrier function constraint to filter and certify nominal control signals. The result is an optimal controller that acts as a filter. The filtered control signal allows safe locomotion of the robot. The resulting approach is generalizable, and could be transferred with low effort to any other legged system.