Subresource integrity meaning, in plain words

Subresource Integrity lets the browser check that a file loaded from outside matches a known fingerprint. What the W3C spec says, and when it blocks.

Written by: Mio Kanzaki(Security Analyst (AI))Reviewed by: 千葉圭介Updated

SRI is a mechanism that detects tampering with files loaded from outside. The name stands for subresource integrity. The W3C specification says an integrity attribute can be added to script and link elements. If you write there a hash value (a fingerprint of the content) computed from the file you expect, the browser checks, before running the file, whether what it received matches the fingerprint.

How does SRI work?

It resembles the seal on a parcel. The sender tells you the seal number in advance, and you check the number before opening. If it differs, you send the parcel back unopened.

The W3C specification says the browser neither renders nor executes a response that fails the check. A failure raises an error event, so you can also build a page that falls back to another file. The supported hash types are SHA-256, SHA-384 and SHA-512.

One situation the specification has in mind is the CDN. Its introduction explains that if a file on a CDN is swapped, arbitrary content could be slipped in. SRI is a means of limiting the effect of such a swap.

The risks of using outside scripts are summarized in the page on third-party scripts. If you want an outside view of your own pages, the free URL check gives one.

Why does SRI matter?

MITRE CWE-829 describes the flaw of pulling in executable functionality, such as a library, from a source outside your own control. A site that includes outside measurement tags and parts can be in this state. SRI is one way of detecting a swap of the content in such a case.

The caution is for the moment when the provider legitimately updates the file. When the content changes, the fingerprint no longer matches, the file is not run, and the display or the feature stops.

For that reason it does not suit a file the provider rewrites from time to time. It suits parts distributed with a fixed version. Before you add it, check that it is acceptable for that spot to stop.

Related terms

  • CSP: a setting that tells the browser which sources it may load from
  • Third-party scripts: programs loaded from outside and run on your site
  • Supply chain attack: an attack that enters through a business partner or a parts supplier
  • CDN: a mechanism that serves from locations around the world and helps protect a site

Frequently asked questions

Can a file with SRI stop working when the provider updates it?

Yes. When the content of the file changes, it no longer matches the hash value you wrote down, and the browser does not run it. SRI does not suit a file the provider rewrites from time to time. Use it for parts that are distributed with a fixed version.

Which hash algorithm should I use?

The W3C specification requires support for SHA-256, SHA-384 and SHA-512. MD5 and SHA-1 are not recommended. At the time of writing, the specification treats SHA-384 as an easy choice to use as a baseline.

Next step

Listing your outside scripts, and deciding the conditions under which one may be stopped, are described on the page about third-party scripts. To talk about introducing SRI, contact us.

Frequently asked questions

Can a file with SRI stop working when the provider updates it?

Yes. When the content of the file changes, it no longer matches the hash value you wrote down, and the browser does not run it. SRI does not suit a file the provider rewrites from time to time. Use it for parts that are distributed with a fixed version.

Which hash algorithm should I use?

The W3C specification requires support for SHA-256, SHA-384 and SHA-512. MD5 and SHA-1 are not recommended. At the time of writing, the specification treats SHA-384 as an easy choice to use as a baseline.

See what an attacker can see, today

The free URL check returns a grade in under a minute. For costs and next steps, book a free consultation.