Skip to main content
Home/Blog/Cybersecurity/How to Implement SRI on Your Website
Cybersecurity

How to Implement SRI on Your Website

Master SRI implementation with integrity and crossorigin attributes, learn proper syntax for script and link tags.

By Inventive HQ Team•
How to Implement SRI on Your Website

Subresource Integrity (SRI) is a browser security feature that lets you verify a third-party script or stylesheet has not been tampered with by adding a cryptographic hash to the tag: <script src="https://cdn.example.com/lib.js" integrity="sha384-…" crossorigin="anonymous"></script>. The browser downloads the file, computes its SHA-384 (or SHA-256/SHA-512) hash, and compares it to your integrity value — if they do not match, the file is blocked and never runs. That single attribute is what stands between your users and a compromised CDN silently serving malicious JavaScript on your login page. SRI is defined by the W3C SRI specification and supported by every modern browser.

That is the summary an AI Overview gives you. Here is what it cannot show you: the exact commands to generate the hash, the CORS gotcha that breaks SRI silently, the decision flow for when a hash mismatch is your bug versus a real attack, and a copy-paste checklist you can run against your own <head>. Below is the animated build flow, a live hash generator, and a symptom-to-fix table.

The 60-second version: what SRI actually does

Every time your page loads a third-party file — jQuery from a CDN, a font stylesheet, an analytics widget — you are trusting that server to send exactly the code you expect. If that CDN is breached (a real, recurring attack class known as a supply-chain attack), it can serve modified JavaScript to your visitors, running in your origin, with access to your cookies and forms. The 2018 British Airways Magecart breach and countless skimmer campaigns work exactly this way.

SRI closes that door. You commit a hash of the file's exact bytes into your HTML. The browser will only execute the file if its content still hashes to your committed value. Change one byte — whether an attacker or a legitimate update — and the browser refuses to run it.

How a browser verifies a subresource with SRI A page requests a script from a CDN; the browser hashes the returned bytes and compares to the integrity value, allowing a match and blocking a mismatch. Browser your page CDN lib.js SHA-384 hash the bytes compare to integrity 1. request lib.js 2. bytes returned match to hash - run it mismatch - blocked

Step 1: Generate the integrity hash

SRI hashes are the base64 encoding of a raw SHA digest of the file's bytes — not the hex string you get from most hash tools. The algorithm name is prefixed to the value. There are three valid choices: sha256, sha384, and sha512. Use sha384 as your default.

From the command line (OpenSSL):

cat jquery-3.7.1.min.js | openssl dgst -sha384 -binary | openssl base64 -A

Directly from a URL (curl piped into OpenSSL):

curl -s https://code.jquery.com/jquery-3.7.1.min.js \
  | openssl dgst -sha384 -binary \
  | openssl base64 -A

In the browser, no install: use the live generator below — paste a URL or the file contents and it emits the full integrity attribute for you.

SRI Hash Generator

Generate Subresource Integrity hashes for scripts and stylesheets

Open the full SRI Hash Generator tool →
Loading interactive tool...

The -binary flag matters: you must hash the raw digest, then base64 it. If you base64-encode the hex string instead, the value will be wrong and the browser will block your file.

Step 2: Add integrity + crossorigin to the tag

For a script:

<script src="https://code.jquery.com/jquery-3.7.1.min.js"
        integrity="sha384-1H217gwSVyLSIfaLxHbE7dRb3v4mYCKbpQvzx0cegeju1MVsGrX5xXxAvs/HgeFs"
        crossorigin="anonymous"></script>

For a stylesheet:

<link rel="stylesheet"
      href="https://cdn.example.com/bootstrap.min.css"
      integrity="sha384-…"
      crossorigin="anonymous">

The crossorigin="anonymous" attribute is not optional for cross-origin files. SRI needs to read the response bytes to hash them, and the browser only allows that read when the response is CORS-enabled and you request it anonymously (no cookies sent). Omit it on a third-party URL and the browser blocks the file with a CORS error even when the hash is correct.

Step 3: Pin an immutable version — never "latest"

SRI depends on the file's bytes never changing under your committed hash. A URL like https://cdn.example.com/library/latest/lib.js will eventually serve a new release, its hash will no longer match, and your site breaks. Always pin to a versioned, immutable URL:

CDN URL patternSafe with SRI?Why
.../jquery-3.7.1.min.js (explicit version)YesBytes are immutable per version
.../npm/pkg@4.6.2/dist/pkg.js (pinned semver)YesjsDelivr/cdnjs serve fixed content
.../pkg@4/dist/pkg.js (major-range)NoResolves to changing minor versions
.../pkg@latest/pkg.jsNoContent changes on every release
Your own build with content hash in filenameYesFilename changes when content does

Which should you use? For third-party libraries, pin the exact patch version on jsDelivr or cdnjs — both publish the matching SRI hash next to every file. For your own bundled assets, use a build tool (Vite, webpack, esbuild) that emits content-hashed filenames and generates SRI attributes automatically, so the hash and the file are always regenerated together.

Where SRI fails, and what to do about it

Because a mismatch is a hard block, the most common "SRI broke my site" reports are self-inflicted — an updated file with a stale hash. Use this table to triage.

SymptomLikely causeFix
Console: Failed to find a valid digest ... integrity attributeFile content changed (you updated it, or CDN mutated it) but hash was not regeneratedRegenerate the hash from the current file bytes and update the tag
Console: CORS / No 'Access-Control-Allow-Origin' with SRIMissing crossorigin="anonymous" on a cross-origin resourceAdd crossorigin="anonymous" to the tag
Hash never matches even when file looks rightYou base64-encoded the hex digest, or added a trailing newlineUse openssl dgst -sha384 -binary | openssl base64 -A — hash the raw binary digest
Works locally, blocked in productionProd CDN minifies/rewrites the file (e.g. Cloudflare auto-minify)Disable transform features for that path, or hash the transformed output
Font stylesheet (Google Fonts) breaks with SRIThe CSS URL returns different content per browser/UADo not apply SRI to UA-varying endpoints; self-host the font CSS instead
Inline script "won't take" an integrity attributeSRI does not apply to inline codeUse a CSP nonce/hash for inline scripts

Step 4: Check SRI coverage site-wide

Per-tag integrity attributes protect only the tags you annotate. The old CSP require-sri-for directive is obsolete and does not enforce SRI in current browsers. Use an Integrity-Policy header where supported, and add a build or CI check that flags third-party script and stylesheet tags without integrity. Test the policy in report-only mode before enforcing it. The per-tag attributes remain essential. SRI and CSP are complementary: CSP restricts which origins can load resources; SRI verifies the specific bytes of the files you allow. For the relationship in depth, see how SRI relates to CSP.

Implementation checklist

Run this against your own <head> before you call it done:

  1. Inventory every <script src> and <link rel="stylesheet" href> pointing at a third-party origin.
  2. Pin each to an explicit, immutable version URL — no latest, no floating major ranges.
  3. Generate a sha384 hash for each pinned file (use the generator above or OpenSSL).
  4. Add integrity="sha384-…" to every tag.
  5. Add crossorigin="anonymous" to every cross-origin tag.
  6. Test in the browser with DevTools open — a blocked resource logs an integrity/CORS error in the console.
  7. Document which URLs are pinned so the next dependency bump regenerates the hash instead of breaking silently.
  8. Automate hash regeneration in your build pipeline for first-party assets so hashes can never drift from content.

The bottom line

SRI is one attribute, two values, and a hash — and it converts "we trust our CDN" into "we cryptographically verify our CDN on every page load." It is cheap to add, safe to deploy (unsupported browsers ignore it), and it is the single most direct defense against a compromised third-party script running in your users' browsers. Pin your versions, generate sha384 hashes, add crossorigin="anonymous", and let the browser enforce the rest.

Compare algorithm choices in the SRI hash algorithm comparison, and if you load many third-party resources, read how to handle CSP for third-party resources next.

Don't wait for a breach to act

Get a free security assessment. Our experts will identify your vulnerabilities and create a protection plan tailored to your business.