Introduction
Third‑party libraries (CDNs, analytics, payment widgets) are a common attack vector. Subresource Integrity (SRI) allows browsers to verify that a fetched resource matches a known cryptographic hash, preventing tampering. This guide shows how to add SRI to Magento 2 and Shopify themes while preserving performance.
1. How SRI Works
| Step | Explanation |
|---|---|
| 1️⃣ Browser requests a script from a CDN. | |
2️⃣ The <script> tag includes
integrity and crossorigin attributes. |
|
| 3️⃣ Browser computes the hash of the downloaded file and compares it to the provided value. | |
| 4️⃣ If they match, the script executes; otherwise, it is blocked. |
2. Generating Hashes
# For a JavaScript file
openssl dgst -sha384 -binary path/to/file.js | openssl base64 -A
# Output: "sha384-<base64hash>"- Use SHA‑384 for best compatibility across browsers.
3. Magento 2 Implementation
3.1 Identify External Scripts
In
app/design/frontend/<Vendor>/<theme>/Magento_Theme/layout/default_head_blocks.xml
locate
<script src="https://cdnjs.cloudflare.com/ajax/libs/..."/>
entries. ### 3.2 Add SRI Attributes
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
src_type="url"
integrity="sha384-<hash>"
crossorigin="anonymous"/>
</head>- Replace
<hash>with the value generated in step 2. - Clear cache:
bin/magento cache:clean.
3.3 Automated Build Step
Add a composer script generate-sri that scans
*.js CDN URLs, computes hashes, and updates the XML file.
Run it during CI:
"scripts": { "generate-sri": "php bin/generate-sri.php" }4. Shopify Implementation
4.1 Edit
layout/theme.liquid
{% comment %} Load Font Awesome with SRI {% endcomment %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/js/all.min.js"
integrity="sha384-<hash>"
crossorigin="anonymous"></script>
4.2 Use Asset URL Filters for Store‑hosted files
Shopify serves theme assets via
{{ 'app.js' | asset_url }} which are already hosted on
Shopify’s CDN. SRI is unnecessary for first‑party assets but can be
added for external resources only.
5. SEO & Security Benefits
- Prevents Supply‑Chain Attacks – If a CDN is compromised, the malicious script is blocked.
- Boosts Trust – Google flags sites serving tampered third‑party code as unsafe, affecting rankings.
- No Performance Penalty – Browser only performs a SHA‑384 hash check, which is negligible.
6. Testing & Validation
- Open the page in Chrome DevTools → Console. Errors
like
Failed to find a valid digest …indicate a mismatch. - Use csp-evaluator or
SecurityHeaders.com to confirm
integrityattributes are present. - Run Lighthouse; the “Avoid non‑integrity‑protected resources” audit should pass.
7. Checklist
Conclusion & Call‑to‑Action
Subresource Integrity is a simple, high‑impact security measure that protects your Magento or Shopify storefront while preserving SEO health. Ready to lock down third‑party scripts with SRI? Click the “Start a project” button in the MODRACX portfolio and let’s secure your site together.
Kenneth D’Silva – Magento & Shopify specialist, MODRACX