I don't see why it's hard. You screen admission of an ad to the auction "floor". Shady javascript/links? You don't get to compete.
Admittedly, this means you need an army of ad moderators, but that's not a hard problem. Social media giants already use an army of underpaid moderators for moderating their platforms, so seems like it's just table stakes for running a platform. Screening ads should be a cakewalk compared to moderating social media.
That's not how it works. It's hierarchical. Someone with an ad to show doesn't send the ad to the web site that wants to show the ad. Instead it just tells that web site "I'll pay $.005 if you show my ad", then if it wins it serves the ad it wants to show. There's no time at that realtime auction to do analysis. The ad doesn't even need to exist as a fixed thing. It can be dynamically generated tailored to the specific user (think of "Come back and shop with us" ads where they show you things you've looked at).
There is a lot more middlemen involved... and at any point they could make a rule that you can only use a certain set of HTML tags and image formats for your ads (none of which include scripts of course).
That would prevent not only most exploits (especially once you re-encode the images), but also simple badly written ads that drive up CPU usage. But it's easier, and allows more middlemen, to simply allow the next party to hand you arbitrary code that may or may not be put into an iframe that may or may not be sandboxed.
> Someone with an ad to show doesn't send the ad to the web site that wants to show the ad
In fact, they do. Creative review is part of most ad platforms. Contextual categorization isn't possible without knowing what the ad is about (and the content it's going to), to various degree.
Admittedly, this means you need an army of ad moderators, but that's not a hard problem. Social media giants already use an army of underpaid moderators for moderating their platforms, so seems like it's just table stakes for running a platform. Screening ads should be a cakewalk compared to moderating social media.