Loading JavaScript libraries or stylesheets from a Content Delivery Network (CDN) can help speed up websites and reduce server load. However, third-party files also carry risks: if a served file changes without the site administrator’s knowledge, new code can run in visitors’ browsers. Subresource Integrity (SRI) is a browser feature that helps verify whether received external files match hashes set by developers.
SRI is not a replacement for comprehensive website security controls. It works best as one protective layer alongside HTTPS, Content Security Policy (CSP), dependency management, and controlled deployment processes.
What Is Subresource Integrity?
Subresource Integrity is a mechanism that allows HTML pages to include cryptographic values for external resources, such as JavaScript or CSS files. The browser downloads the file, computes a hash of its contents, then compares it to the value in the integrity attribute. If the hashes don’t match, the browser refuses to use that resource.
Thus, SRI helps detect content changes that don’t match the approved version. For example, a site loads a library from a CDN. If the received file differs from the file verified when the integration was created, the browser can block it rather than execute mismatched code.
How Does SRI Work?
Developers generate a hash from a trusted file, then store that hash in the HTML element that loads the resource. The browser recomputes the hash after downloading the file. A match means the file contents match the declared value; a mismatch causes the browser to reject the resource.
The following example shows the attribute format. The hash value below is only a placeholder, not a hash ready for direct use:
<script
src="https://cdn.example.com/library.min.js"
integrity="sha384-BASE64_HASH"
crossorigin="anonymous">
</script>
Replace BASE64_HASH with an actual SHA-384 hash computed from the exact file to be used. Don’t copy this example to a production site before verifying the hash value. SRI can also be used on link elements for external stylesheets.
When Is SRI Useful?
SRI is most useful when a site loads static files from external sources with verifiable versions, such as JavaScript or CSS libraries pinned to specific versions. Examples include documentation pages or marketing sites using a fixed version of a library from a CDN.
This feature is less practical for URLs that always point to the latest version or files that change dynamically. Any legitimate content change also changes the hash, so the integrity value needs updating alongside version changes. Otherwise, browsers may block files that were intentionally updated.
Steps to Implement SRI Safely
1. Pin dependency versions
Use URLs that reference specific library versions, not aliases that change with new releases. Fixed versions make expected files easier to verify and reduce surprises when CDN providers update assets.
2. Compute hashes from the correct file
Download or obtain the release file from a trusted source, verify its version and origin, then compute the hash using appropriate tools. For example, the following OpenSSL command generates a Base64 value for SHA-384 from a local file:
openssl dgst -sha384 -binary library.min.js | openssl base64 -A
Add the sha384- prefix to the Base64 result when placing it in the integrity attribute. Ensure the computed file exactly matches the file to be served, including after any build or optimization processes.
3. Configure CORS for cross-origin sources
For cross-origin resources, the crossorigin="anonymous" attribute is commonly used with SRI. The CDN server must also provide appropriate CORS headers so browsers can inspect the response. If CORS configuration doesn’t match, resources may fail to load even when the URL is correct.
4. Test after every update
After updating dependencies, regenerate hashes and test the page in browsers. Check the Console and Network panels to see whether files are blocked due to integrity failures or CORS issues. Include hash updates in your code review process so changes aren’t made manually without verification.
Limitations to Understand
SRI checks the contents of tagged resources, not entire pages. It doesn’t automatically protect files without integrity attributes, API responses, or malicious code already injected into the main HTML. If an attacker can modify the HTML containing hash values, they may also be able to replace those hashes. Therefore, protect your deployment processes and site administrator accounts.
SRI also doesn’t guarantee that the library itself is free of vulnerabilities. A matching hash only indicates that the file matches the set value, not that the code is safe or suitable for all needs. Continue updating dependencies, reviewing sources, and removing libraries no longer needed.
Checklist Before Enabling SRI
- Ensure URLs point to fixed, trusted dependency versions.
- Compute hashes from the final file to be loaded, not from different files or other versions.
- Use appropriate
integrityattributes andcrossoriginconfigurations. - Test pages in browsers and review Console messages and CORS responses.
- Update hashes alongside version changes, then conduct code reviews.
- Combine SRI with HTTPS, CSP, and deployment pipeline security.
Conclusion
Subresource Integrity gives browsers a way to verify whether external JavaScript or CSS matches approved files. Implementation is relatively straightforward for version-pinned static assets, but requires disciplined hash management whenever dependencies are updated. Use SRI as an additional defensive layer, not as your sole security control.
For technical reference, read the Subresource Integrity documentation on MDN and related specifications at W3C.

Komentar (0)
Belum ada komentar. Jadilah yang pertama berkomentar.