Skip to content
Web Design & Development

Subresource Integrity, Done Properly

A browser feature that checks a script or stylesheet loaded from a CDN against a cryptographic hash, refusing to run it if the file has been altered.

MediaScaleUp Editorial Team 2 min read 10 views
Subresource Integrity, Done Properly

What is at stake

If a third-party CDN is compromised, integrity checks prevent the altered files from running on your site.

The playbook

  1. Add integrity hashes to third-party library files
  2. Pin exact versions of CDN resources
  3. Generate hashes during the build
  4. Update hashes when upgrading libraries
  5. Combine with a Content Security Policy

Where it goes wrong

Avoid:

  • Loading latest-version URLs that change
  • Omitting crossorigin with integrity
  • Hand-copying hashes that drift out of date
  • Assuming SRI protects dynamic scripts
What to do and what to avoid with subresource integrity, side by side
Good practice against the usual mistakes, from the sources listed below.

The numbers behind it

Published figures for Subresource Integrity
MeasureFigure
Attributeintegrity, containing a hash such as sha384
crossoriginrequired for cross-origin resources
Behaviorthe browser blocks the resource if the hash does not match
Limitationworks only for files that do not change

Getting outside help

When to hand it over: Bring in help when a site loads critical scripts from third-party CDNs, or when hardening payment pages.

Where this comes from

  • MDN Web Docs — Subresource Integrity
  • W3C — Subresource Integrity specification

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development