Homeglossary

Subresource Integrity

Subresource Integrity is a browser security feature that verifies external scripts and stylesheets match a publisher-approved cryptographic hash. It helps prevent a changed or compromised CDN-hosted file from running on a website. SRI confirms exact file bytes, so it works best with version-pinned, immutable resources and alongside controls such as Content Security Policy.

Build safer web apps with Emergent.

What is Subresource Integrity?

Subresource Integrity, or SRI, is a browser security feature that checks whether an external script or stylesheet exactly matches a publisher-approved cryptographic hash before using it. It helps stop a modified or compromised file from a CDN, such as a JavaScript library, from silently running on a website. MDN’s SRI guidance describes it as a way to verify fetched resource contents.

How Subresource Integrity works

  1. Choose an exact, version-pinned external file rather than a changing “latest” URL.
  2. Create a SHA-256, SHA-384, or SHA-512 hash from that file’s exact bytes.
  3. Add the algorithm and hash to the HTML resource’s integrity attribute.
  4. For a cross-origin resource, add crossorigin="anonymous" so the browser can perform the required CORS-enabled fetch.
  5. The browser downloads the file, calculates its hash, and compares it with the approved value.
  6. If the values differ, the browser blocks the script or stylesheet instead of using it.

Subresource Integrity example

A script tag might reference a fixed CDN URL and include integrity="sha384-..." plus crossorigin="anonymous". The integrity value is the approved fingerprint, while the crossorigin setting requires the CDN to allow the browser’s request. Changing even one character in the downloaded file produces a different hash, so a new approved hash is required.

Benefits of SRI for web security

SRI adds a browser-enforced verification step for fixed third-party assets.

  • It reduces risk from CDN compromise and third-party asset tampering.
  • It prevents a mismatch from becoming silent script execution.
  • It makes unexpected dependency changes visible as a deployment failure.
  • It gives teams a precise approval record for external files.

SRI, Content Security Policy, and HTTPS

These controls address different parts of browser security and work best together.

ControlWhat it protectsKey limit
SRIExact bytes of a referenced fileDoes not secure inline or changing assets
Content Security PolicyAllowed sources and browser behaviorDoes not verify a file’s exact content
HTTPSData while it travels over the networkDoes not prevent a trusted host from serving changed content

Practical limits of Subresource Integrity

SRI is powerful, but only for resources whose contents can remain predictable.

  • Dynamic files and unpinned URLs will break when their contents change.
  • Cross-origin files need suitable CORS support.
  • It does not protect inline scripts or an already compromised first-party HTML page.
  • A trusted script can still behave maliciously after it has passed an SRI check.

Safe implementation practices

Use SRI as part of a repeatable release process.

  • Pin immutable versions and generate hashes in the build pipeline.
  • Review hash changes like source-code changes.
  • Test the blocked-resource path before production releases.
  • Maintain an inventory of third-party assets, use CSP, and scan dependencies.
  • Avoid uploading private assets to public hash-generation tools.

Subresource Integrity in Emergent projects

When building with an AI web app builder, review every third-party script, keep external assets versioned, and include browser security checks before deployment. Emergent teams can pair SRI with sound app design and development practices to make web applications safer to ship and maintain.

Start Building
on Emergent today
Start Building