← Back to context

Comment by duxup

11 days ago

Let’s say I use Optimizely (I don’t), is there a good best practice to say?:

“yeah load this JS m, but it can’t load / random other JS referencing sketchy places”

Besides specifying scripts by hashes, you can also whitelist various domains: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP (and https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/...)

Feels nice knowing several years ago I worked on forcing CSP (including removing unsafe-eval) on some customers for a site builder product, as they have to manually whitelist domains which limits the impact of these sorts of attacks...

This writeup https://blog.report-uri.com/worlds-largest-brands-infected-w... has a section on how CSP helped (or didn't) in this attack.

  • Some details that may have been missed in the Report URI analysis:

    CSP script-src was only helpful here because the attacker used the Optimizely ProjectJS feature to delegate to a third-party script. It wouldn't do a thing if the dirty work had been done in e.g. ProjectJS itself.

    CSP connect-src/form-action are somewhat helpful for stopping a script from exfiltrating data to, or redirecting the end-user to, a third-party domain, but an attacker still has plenty of other options (https://github.com/w3c/webappsec/issues/656).

    And apparently CSP frame-src would have blocked the iOS exploit, but there's nothing to stop other malicious content (scam propositions, pump-and-dumps, psy-ops, other “truths”...) from being rendered.

    (disclaimer: former Optimizely employee)

  • Yeah, honestly I wish they'd move that section higher up - it's probably he most genuinely useful tidbit in the article.

    As an aside - I'm rabidly against "analytics" companies that come in and suggest that the right "getting started" choice is to drop untrusted JS they own (and can update at any time) onto your site.

    It's "easy", but it completely breaks your security model.

    If I were a lawyer... I'd be taking a serious look at putting optimizely into the meat grinder for recommending 'unsafe-eval' as a script source... it literally has "unsafe" in the fucking name for a reason. This alone should end the company. It won't, because this space is absolutely FILLED with scummy behavior, but it should.

    ---

    A strict CSP is one of the most meaningful ways you have right now as a web developer to reduce attack surface. People should take advantage of it.

    There's a reason browser vendors now force basically all extensions into a strict CSP by default... https://developer.chrome.com/docs/extensions/reference/manif... They won't even let you manually relax it anymore.

    • I think the kinds of people with "security models" are not the kinds of people with third-party analytics. They're marketing to people who want it to Just Work.

      2 replies →

    • Ironically, eval & co. don't actually increase the power of Optimizely Web Experimentation scripts, which are designed to support arbitrary customer-specific JavaScript and which primarily do so by compiling custom code into the syntax tree. (I left Optimizely in 2020, but I would be quite confident that the “only” inputs that get eval'd are intended code inputs.)

      Softer questions might be: why is eval (still) being used for any inputs at all; and to the extent that it is used, why did¹ the docs not concede the breadth of CSP unsafe-eval (which allows eval in all scripts, not just Optimizely's), and why do the docs actively discourage attempts to enable Trusted Types (which includes the much narrower CSP trusted-types-eval²) instead of actively facilitating its use?

      And needless to say, a more direct question would be: why should customers use a class of product that is so powerful that unsafe-eval doesn't increase its capabilities at all?

      ¹ cf. https://web.archive.org/web/20260810141143/https://support.o... vs. https://docs.optimizely.com/web-experimentation/docs/update-... (updated yesterday).

      ² ~84% browser support, per https://caniuse.com/mdn-http_headers_content-security-policy...

Yes: https://developer.mozilla.org/en-US/docs/Web/Security/Defens...

  • SRI helps you make sure the script you're loading is what you initially thought, but doesn't ensure that script doesn't load other scripts, no? You need CSP for that.

    • Specifying scripts by hashes is always a solid defense that platforms like this don't change their script after compromise - but in practice the whole point of optimizely and similar tools it that it's expected the script will change.

      Common examples like analytics tools happily change their script bundles whenever they feel like it without just tagging a new version and letting you upgrade.

      I wish SRI could be used more,

      4 replies →

  • There's no reason to use SRI any more since caches became partitioned. It won't share the cache between different websites for privacy reasons so you may as well just host the file yourself, and then there's not much reason to use SRI.

  • this will prevent an unexpected version but doesn't stop a legit but compromised include from loading nefarious resources

Yes. You install a CSP that says "my site cannot load any scripts from third parties". And then any script you run, you audit thoroughly and then host on your own website, ensuring it doesn't try to load anything third-party.

Never load third-party JavaScript on your site. Zero exceptions.

In theory yes, in practice no since every SaaS wants to ship 5 times a day and add 15 new dependencies on other SaaS providers each release, and marketing wants to load gobs of 3rd party tracking and analytics code from their favorite SaaS of the day without having to get approval from those pesky security people. Let's not even mention ad networks.