Subresource Integrity: blocking a hijacked third-party script

Subresource Integrity (SRI) is a browser feature that checks a third-party file is exactly the one you expected before it runs. You add an integrity attribute holding a cryptographic hash of the file to your script or stylesheet tag, and if the file the browser receives does not match that hash, it refuses to load it.

Explainer6 min read

It matters because most sites load JavaScript from someone else's server: a CDN, an analytics provider, a payment or chat widget. If an attacker tampers with that file at its source, every site embedding it runs the attacker's code. SRI is the seatbelt that stops a swapped file from executing in your visitors' browsers.

Our free scan reads your homepage as a browser would and flags third-party scripts that load without an integrity attribute, so you can see where this particular protection is missing. It is one external best-practice signal, not a full audit of every dependency you load.

How a hijacked third-party script becomes a supply-chain attack

When your page includes a script from another domain, you are trusting that server completely. The browser fetches whatever it sends and runs it with full access to your page: your forms, your cookies (unless they are flagged HttpOnly), and anything a visitor types.

This is how Magecart-style attacks work. The attacker does not break into your site at all. They compromise a shared script, such as a chat widget, a tag-manager helper or an old library on a CDN, and quietly edit it to skim card numbers or login details. Because the file still loads from a trusted-looking source, nothing on your own site looks wrong, and the skimming can run for a long time before anyone notices.

  • The attacker tampers with a file on a third-party or CDN server, not on your own hosting.
  • Every site that embeds that file then serves the malicious version to its visitors.
  • The skimmed data is sent to the attacker straight from each visitor's browser.
  • Your server logs and your own files stay clean, which is exactly why these attacks hide so well.

How the integrity attribute stops a tampered file

SRI closes that gap with a simple check. You generate a hash (usually SHA-384) of the exact file you reviewed, then put it in the tag. The browser hashes the file it actually receives and compares the two. If a single byte has changed, the hashes will not match and the browser blocks the file outright, so the altered code never runs.

In practice the tag looks like the example below, paired with crossorigin so the check can run on a file from another domain:

  • <script src="https://cdn.example.com/lib.js" integrity="sha384-..." crossorigin="anonymous"></script>
  • The hash pins the file to the exact version you reviewed and trusted.
  • A swapped or edited file fails the check and is refused, so a poisoned CDN cannot run code on your page.
  • You only change the hash deliberately, when you choose to adopt a new version.

Where SRI fits, and where it does not

SRI protects against one specific risk: a third-party file being altered after you linked to it. It does not encrypt anything, it does not stop a script you genuinely trusted from misbehaving by design, and it does nothing for first-party scripts on your own server, where you already control the file.

It pairs well with a strong Content-Security-Policy, which limits where scripts may load from in the first place. SRI then verifies that the files you do allow are unchanged. Together they make a hijacked dependency much harder to weaponise.

Free, instant, no account

See your site's security grade in seconds.

Free, instant, and no account needed to see your grade.

Scan my site free

Plain-English A to E grade in about 10 seconds. No account needed to see it.

The honest limitation: not every provider can use SRI

SRI only works when the file's contents are stable, because the hash must match exactly. Some providers deliberately serve a file that changes: many analytics, tag-management and ad scripts update themselves or vary their output, so a fixed hash would break on the next change and block a legitimate file.

For those, SRI is not a realistic option, and forcing it would simply break your page. That is normal and not a failing on your part.

  • Good candidates for SRI: pinned, versioned library files on a CDN, such as a specific jQuery or framework build.
  • Poor candidates: self-updating analytics, tag managers and ad tags that change without warning.
  • For unstable third-party scripts, lean on a tight Content-Security-Policy and load as few of them as you can.
  • If a provider cannot support SRI, treat that as a reason to limit and review what you load, not to drop the protection everywhere else.

What our free scan reports about SRI

Our scan looks at the third-party scripts your homepage pulls in and, where one is loaded without an integrity attribute, flags it as a best-practice finding with a plain-English explanation and the fix. It reports the missing attribute; it does not read, run or judge the contents of the script itself.

SRI is a hardening measure, so a missing integrity attribute will only ever nudge your grade, never sink it on its own. It sits alongside the other external checks we run, such as your security headers, your certificate and exposed files, to give you a fast, honest first picture. For full coverage of every dependency you load, treat the scan as a starting point and follow it with a proper review of your third-party scripts.

Subresource Integrity pins a third-party file to a hash so a tampered version is blocked before it runs, which is your main defence against supply-chain script attacks. Use it on stable, versioned files, and lean on a strong CSP where a provider's script changes too often to hash. Our free scan flags scripts that are missing the integrity attribute, which only nudges your grade rather than capping it.

Common questions

What is Subresource Integrity in simple terms?
It is a browser check that confirms a third-party file, such as a script from a CDN, is exactly the one you expected. You add a hash of the file to your tag, and if the file served back does not match, the browser refuses to run it.
How does SRI help stop a supply-chain attack?
In a supply-chain attack, an attacker tampers with a shared script at its source rather than breaking into your site. SRI hashes the file as it arrives and blocks it if even one byte has changed, so the poisoned version never runs in your visitors' browsers.
How do I add an integrity attribute to a script?
Generate a SHA-384 hash of the exact file, then add integrity="sha384-..." to the script or link tag, along with crossorigin="anonymous" for files from another domain. Online SRI hash generators produce the value for you.
Why can't some scripts use Subresource Integrity?
SRI needs the file's contents to stay identical, because the hash must match exactly. Some providers, such as many analytics and ad scripts, update or change their files automatically, so a fixed hash would block a legitimate update. Those scripts cannot reliably use SRI.
Is SRI a replacement for a Content-Security-Policy?
No, they do different jobs. A CSP limits which sources a browser may load scripts from, while SRI verifies that an allowed file has not been altered. Used together they give much stronger protection than either alone.
Does the free EB Security scan check SRI?
Yes. The scan looks at the third-party scripts your homepage loads and flags any that lack an integrity attribute, with a plain-English fix. It reports the missing attribute only; it does not inspect what the script does. It is a best-practice signal that nudges your grade rather than capping it, and it works best alongside a full review of your dependencies.

How safe is your website?

A free A to E grade in about 10 seconds. No account needed.

Check my site