On this page
Changing a CDN URL does not, by itself, invalidate a Subresource Integrity (SRI) hash. SRI checks the resource content, not the URL string. If subresource integrity fails after a CDN URL change, start by checking whether the new endpoint returns different bytes, then investigate CORS, redirects, and caching.
This guide includes a local reproduction and a deployment checklist for scripts and stylesheets. The W3C SRI specification and current MDN documentation could not be consulted in this run, and the example was not executed here; check those sources for details applicable to your browser support requirements.
Key Takeaways
- A new URL can use the existing integrity value if it returns the same resource bytes and the request passes the browser’s other checks.
- A changed file requires a new integrity hash. A version label or matching filename does not prove the contents match.
- Cross-origin resources require appropriate CORS behavior;
crossoriginalone does not configure the CDN. - Redirects and caches can cause the browser to receive something different from the file you intended to deploy.
- Update the URL and hash together, verify the deployed page, and retain a rollback path.
What SRI checks
An integrity attribute contains an algorithm name and a Base64-encoded digest. The browser computes a digest of the fetched resource and compares it with the supplied integrity metadata before using the resource. The URL identifies where to fetch the resource; it is not an input to the digest.
For example, moving an unchanged script from one CDN hostname to another does not inherently require a new hash. But the new endpoint might return a different build, an error page, or a transformed file. Any of those can make the old hash unsuitable.
For compressed responses, distinguish the resource content from the compressed bytes sent over the network. When calculating a hash from a downloaded file, make sure you are hashing the decoded resource content that corresponds to what the browser checks, not a gzip or Brotli archive saved as a file.
Advertisement
Reproduce a URL change and a content change
This local lab requires a shell, OpenSSL, Python, and a browser. It creates three script files: the first two have different URLs but identical contents; the third has different contents. It then creates a page that uses the first file’s hash for all three scripts.
mkdir -p sri-lab
cd sri-lab
printf 'console.log("SRI lab: v1");\n' > old.js
cp old.js same.js
printf 'console.log("SRI lab: v2");\n' > changed.js
HASH=$(openssl dgst -sha384 -binary old.js | openssl base64 -A)
printf '%s\n' '<!doctype html>' '<title>SRI lab</title>' > index.html
printf '<script src="old.js" integrity="sha384-%s"></script>\n' "$HASH" >> index.html
printf '<script src="same.js" integrity="sha384-%s"></script>\n' "$HASH" >> index.html
printf '<script src="changed.js" integrity="sha384-%s"></script>\n' "$HASH" >> index.html
python3 -m http.server 8000
Open http://localhost:8000/ and inspect the browser console and Network panel. The expected behavior is that old.js and same.js execute, while changed.js is rejected because its contents do not match the supplied hash. Stop the server with Ctrl+C.
You can also compare the files locally:
cmp old.js same.js
cmp old.js changed.js
The first comparison should find no difference; the second should report one. This lab uses same-origin files so that it isolates the hash comparison rather than introducing a cross-origin CORS requirement.
Diagnose the CDN response
1. Compare the actual files, not just the URLs
Download the intended old and new resources from trusted endpoints and compare their contents. For example, set ASSET_URL to the new HTTPS URL in your shell, then run:
curl --fail --location --silent --show-error --compressed \
--proto '=https' --proto-redir '=https' \
"$ASSET_URL" -o candidate.js
printf 'sha384-'
openssl dgst -sha384 -binary candidate.js | openssl base64 -A
printf '\n'
Compare the resulting integrity value with the one in the deployed HTML. --compressed asks curl to decode supported HTTP content encodings; it does not turn an intentionally served archive into a JavaScript file. Confirm the response really is the intended script or stylesheet, rather than an HTML error or challenge page.
A command-line download is a useful comparison, but it is not proof of exactly what every browser received. The CDN may vary responses by request headers, location, cache state, or other conditions. Use the browser’s Network panel to inspect the actual request, response, and redirect chain as well.
If the bytes differ, determine why before replacing the hash. The new URL might point to another release, a minified rather than unminified build, or a CDN-transformed file. Updating the hash without validating the artifact can approve the wrong content.
2. Check CORS separately
For a cross-origin script or stylesheet protected by SRI, the CDN must permit the cross-origin request through CORS. A common public-resource pattern is:
<script src="https://cdn.example.com/app.js"
integrity="sha384-REPLACE_WITH_VERIFIED_DIGEST"
crossorigin="anonymous"></script>
The hostname and digest above are illustrative; replace both with your verified resource and its actual digest. With anonymous, the request uses CORS without cross-origin credentials. The CDN must still return a suitable Access-Control-Allow-Origin response. A public, non-credentialed resource can use * for that header.
crossorigin="use-credentials" is a different choice: it permits credentials on the cross-origin request and requires corresponding credentialed CORS permission from the server, including an allowed origin rather than a wildcard. Use it only when the resource genuinely needs credentials.
Look at the browser’s console and Network panel for CORS errors as well as integrity errors. Fixing the hash cannot fix a CORS rejection, and adding crossorigin cannot make a server send the required CORS headers.
3. Inspect redirects and caching
A CDN URL may redirect to another host or path. Check the entire redirect chain, the final response URL, its status and content, and the CORS behavior of the request. A redirect does not make the URL part of the hash, but it can lead to different content or a request that fails cross-origin checks.
Caching can also separate an HTML deployment from its matching asset. For example, a browser or CDN might serve old HTML containing the previous hash while the same asset URL now returns new bytes. The reverse can happen too: new HTML can refer to content that a cache still serves in an older form.
During diagnosis, compare the deployed HTML’s integrity value with the browser’s actual asset response. Try a fresh browser session or disable the browser cache in developer tools, and inspect relevant CDN cache headers and configuration. A cache bypass can help identify stale content, but it does not prove all users or CDN locations have the new version.
Advertisement
Safe deployment checklist
- Select and verify the artifact. Obtain the intended release from a trusted source and check that its contents are the file you mean to publish. SRI checks consistency with a hash; it does not establish that the file itself is trustworthy.
- Calculate the hash from that artifact. Use a supported SRI algorithm such as SHA-384, and generate the Base64 digest from the exact resource content intended for browsers.
- Publish an immutable, versioned asset URL where practical. Avoid replacing bytes at a URL still referenced by older HTML. Confirm that CDN transformations do not alter the asset after hashing.
- Update the URL and integrity value together. Deploy the HTML or template containing both values as one change. Configure
crossoriginand CDN CORS headers when the resource is cross-origin. - Verify the deployed page. In a browser, check the final resource, redirects, console, and Network panel. Confirm the script executes or the stylesheet applies, and investigate CORS or integrity errors. Check representative cache paths where your deployment allows it.
- Keep a rollback pair. Retain the previous HTML configuration and its matching immutable asset. If verification fails, restore both the previous URL and its corresponding integrity value, then verify again.
Common Mistakes
- Assuming a changed URL means changed bytes. Different URLs can serve identical files; compare the files themselves.
- Assuming matching URLs mean matching bytes. A CDN can serve different content at the same URL over time or under different request conditions.
- Hashing the wrong representation. Check whether your downloaded file is decoded resource content or a compressed archive, and whether it is the intended build.
- Treating every load failure as a hash mismatch. CORS, redirects, Content Security Policy, network errors, and response content can also prevent a resource from loading.
- Updating only the hash. First verify why the content changed. A new hash for an unexpected response merely makes that response match the metadata.
- Using a URL comparison as an integrity check. URL differences can guide investigation, but only comparing the resource content or its digest addresses byte-level integrity.
Conclusion
When SRI fails after a CDN change, compare the content returned by the new endpoint with the content represented by the deployed hash. Then check CORS, redirects, and caches independently. Deploy a verified resource URL and its matching hash together, and keep the previous pair available for rollback.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.