Third-Party Scripts and Subresource Integrity: A Plain Guide
Every script you load from another site runs with the same power as your own code. Subresource Integrity lets the browser check it hasn't changed. Here is when it helps and when it can't.
๐ก The short version: a script loaded from another site runs on your page with the same power as your own code. If that other site is compromised, so is your page. Subresource Integrity (SRI) lets the browser refuse a file that no longer matches the hash you expected. It works for files that don't change, and can't work for scripts that change by design.
Think about the last time you added "just one more" snippet: analytics, a chat bubble, a font, a fancy slider. Each one is a small act of trust. ๐ค You're telling every visitor's browser to run whatever that other server sends today, and tomorrow.
โ ๏ธ What can go wrong
- The provider's server or CDN account is compromised and the file is replaced with a malicious one.
- The provider is sold, shut down or changes hands, and the address now serves something else.
- A legitimate update quietly changes behaviour, such as adding tracking.
In each case your site didn't change, but what it runs did.
๐ What SRI does
You give the browser a hash of the file you expect. If the downloaded file doesn't match, the browser refuses to run it (or to apply the stylesheet). MDN describes it as protection against exactly the compromised-host case.
<script
src="https://cdn.example.com/lib.js"
integrity="sha384-REPLACE_WITH_REAL_HASH"
crossorigin="anonymous"></script>
To make the hash from a file you've downloaded:
cat lib.js | openssl dgst -sha384 -binary | openssl base64 -A
Put sha384- in front of the output. The crossorigin="anonymous" part matters, and the other server must send the right CORS headers for the check to work.
๐ฏ When it works, and when it can't
| Script type | Can you pin it? |
|---|---|
| A versioned library file on a CDN (the same bytes every time) | Yes, and you should |
| A stylesheet or font file you load from elsewhere | Yes, if it's fixed |
| Analytics, payments, captchas, consent banners, ad and chat widgets | No. These change by design, and pinning would break them the next time they update |
| A tag manager | No, and note that it can load other scripts you can't see from the page |
Telling someone to "just add an integrity hash" to a script that changes every request is bad advice. It would simply stop loading.
๐งน A practical routine
- List every script and stylesheet loaded from another host. View source, or use the Network tab.
- Remove what you don't use. The safest script is the one that isn't there.
- Pin what can be pinned with SRI, and use exact versions in the address, not "latest".
- For the rest, trust vendors you'd be happy to name to a client, and revisit the list twice a year.
- Add a Content-Security-Policy that limits which hosts may run scripts at all. (Headers explained.)
๐ค What a scan does about it
Dr. Security reads the scripts and stylesheets your homepage loads from other hosts and notes whether each has an integrity hash. It doesn't download the scripts. A script from an unfamiliar host without a hash is a medium finding, one from a well-known CDN is low, and an unfamiliar stylesheet is low. Vendors that can't be pinned are only ever a note naming the host, never a failure, with an extra note when a tag manager is present. The report names hosts only, never paths or queries. This check is part of paid plans, not the free report.
๐ Curious what your pages load? Run a free scan to see the outside view of your site.