Developer and Text Tools

Subresource Integrity: Protect CDN Scripts with SRI Hashes

How SRI works, which algorithm to choose, why crossorigin matters, how to add hashes to your build, and how CSP hashes cover inline scripts and styles.

Saizul Amin
Saizul Amin3 Nov 2026 · 3 min read
Subresource Integrity: Protect CDN Scripts with SRI Hashes

The risk of trusting someone else's server

Loading a library from a public CDN is convenient and fast, but it hands control of your page to that server. If the CDN is breached, a package maintainer account is taken over, or an old domain expires and is bought by someone else, the file your visitors receive can change without your code changing at all. Several large incidents worked exactly this way: a widely embedded script was replaced, and every site using it began running the attacker's code.

Subresource Integrity is the browser's answer. You add an integrity attribute to the script or link tag containing a cryptographic digest of the file you expect. Before running a script or applying a stylesheet, the browser hashes what it received and compares. If the values differ, the file is blocked and an error is logged. The page may lose a feature, but it does not run unknown code.

How the attribute is written

The value is the algorithm name, a dash and the Base64 encoded digest, for example sha384- followed by 64 characters. Browsers support SHA-256, SHA-384 and SHA-512. SHA-384 is the conventional choice and is what most CDNs show on their copy buttons. You may list several values separated by spaces. When you do, the browser looks only at the strongest algorithm present and accepts the file if any of the values for that algorithm match. Listing two SHA-384 values lets you roll a file over without downtime.

Files from another origin need one more attribute. The browser can only inspect the bytes of a cross-origin response that was requested in CORS mode, so the tag must include crossorigin, normally with the value anonymous so no cookies are sent. Without it, the integrity check cannot run and the load fails. The CDN must also send Access-Control-Allow-Origin, which all major public CDNs do.

Generating hashes you can trust

The hash must be computed over the exact file your users will download, byte for byte. Build the asset first, then hash the output. Using the Subresource Integrity (SRI) Hash Generator, drop the built files, set the base URL they will be served from, choose the tag type and crossorigin value, and copy the finished tags. For CDN files you did not build, paste the exact versioned URL; if the CDN allows cross-origin reads, the browser fetches and hashes it directly. Never hash a URL that points at a moving target such as latest, because the next release will break your page.

For many files, the JSON map export is handy: it lists each URL with its integrity value, which a template or build script can read. In larger projects, bundler plugins can add integrity automatically on every build; a manual check with an independent tool is still useful when something fails.

Inline code and CSP hashes

SRI covers external files. Inline scripts and styles are controlled by Content Security Policy, and CSP can allow a specific inline block by its hash instead of falling back to unsafe-inline. The digest is computed the same way over the exact text between the tags, including spaces and line breaks. The inline tab produces the quoted source expression and a ready directive. Each time the snippet changes, the hash changes, so keep inline code small and stable.

Troubleshooting a blocked file

When the console reports an integrity failure, check three things. First, did the file change, perhaps through a new deploy or a CDN that minifies on the fly? Second, does the tag include crossorigin, and does the server send the CORS header? Third, is a proxy rewriting the file, for example by injecting scripts or changing line endings? The verify tab compares your tag against the actual file and tells you which algorithm was checked and whether it matches, which narrows the cause quickly.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages