# Subresource Integrity \(SRI\)

Subresource Integrity lets a browser check a fetched resource against an expected cryptographic digest before using it.

- Type: method
- Domain: Engineering & architecture
- Level: Intermediate
- Last reviewed: 2026-10-19
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/subresource-integrity/>)

## What it means

Subresource Integrity, or SRI, attaches integrity metadata to supported resources such as scripts and stylesheets. The browser hashes the fetched content and compares it with the expected digest; a mismatch prevents the resource from being used. For cross-origin resources, the required CORS conditions must also be satisfied. SRI verifies expected bytes, not whether the code is safe.

## Why it matters

Externally hosted dependencies can change or be compromised independently of a site. Pinning expected content adds a check between retrieving a resource and executing or applying it.

## When to use it

Use it for supported, versioned resources whose contents are stable and whose delivery configuration supports the check. For continuously changing resources, arrange a controlled versioning and update process first. Self-hosting and dependency review remain separate decisions.

## How it works

1. Select a reviewed, versioned resource and obtain its exact contents through a trusted process.
2. Generate supported integrity metadata from those bytes.
3. Configure the resource reference and any required cross-origin access settings.
4. Test successful loading and rejection when the digest does not match.
5. Update the resource and digest together after review, preserving a recovery path.

## A practical example

A documentation site loads a versioned script from a CDN with an expected digest. An unexpected content change causes browsers to reject it. The team restores the reviewed version rather than replacing the digest blindly to make the warning disappear.

## Where it breaks

SRI does not protect a page whose attacker can change both the reference and digest, and it does not assess the security of approved code. Resource updates require coordinated metadata changes. A rejected critical dependency can break functionality, so failure behavior needs planning.

## Common mistakes

- Updating the digest without reviewing the changed resource.
- Assuming a matching hash proves the script is trustworthy.
- Ignoring cross-origin requirements or relying on an unversioned resource URL.

## Sources

1. [Subresource Integrity](<https://www.w3.org/TR/sri/>) — W3C · Standard
2. [Subresource Integrity](<https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Subresource_Integrity>) — MDN Web Docs · Official documentation

## Related topics

- [Hashing](<https://wiki.soroush.uk/topic/hashing/>)
- [Content Delivery Network \(CDN\)](<https://wiki.soroush.uk/topic/content-delivery-network/>)
- [Content Security Policy \(CSP\)](<https://wiki.soroush.uk/topic/content-security-policy/>)
