And even if the leading car was non-malicious and merely incompetent, it's your responsibility to not hit anything in front of you. For liability, you worry about what's in front, not what's behind.
The only way you get out of being liable for rear-ending someone is if the leading car maliciously caused you to rear-end them, or if something unpredictable happened that would have made it impossible for you to avoid hitting the other car.
This is literally what happened in this situation. If you have a dashcam and something like this happens, you won't be found at fault.
If you went to court for having rear-ended someone in a similar set of circumstances, that's basically what the judge would tell you. Would that be frustrating to hear? Yes, but it's how it works.