How to Configure Strict Subresource Integrity (SRI) Hash Policies in Google Chrome

Modern web applications rely heavily on Content Delivery Networks (CDNs) to host third-party JavaScript libraries, web fonts, and stylesheets. However, if a CDN provider is breached or a DNS hijack occurs, attackers can tamper with external scripts to execute Magecart-style credit card skimming or session hijacking. Subresource Integrity (SRI) eliminates this threat by verifying cryptographic hashes before executing remote code. In Google Chrome, combining SRI hashes with Content Security Policy (CSP) enforcement guarantees that only byte-exact, verified scripts run in the browser.

Understanding Subresource Integrity Attributes

The integrity attribute enables browsers to perform cryptographic verification using SHA-256, SHA-384, or SHA-512 hashes. To enable verification for cross-origin resources, the element must also declare CORS credentials via crossorigin="anonymous":

<!-- SRI verified script tag -->
<script src="https://cdn.example.com/libs/chart.min.js"
        integrity="sha384-7aD...exactHash..."
        crossorigin="anonymous"></script>

If the hash of the downloaded payload does not match the attribute value precisely, Google Chrome refuses to execute the script and triggers an error in the developer console.

Generating Cryptographic Hashes for Static Assets

You can generate compliant SRI hashes using OpenSSL on Linux, macOS, or PowerShell in Windows 11:

# Generate SHA-384 hash encoded in Base64 using OpenSSL
cat bundle.js | openssl dgst -sha384 -binary | openssl base64 -A

# Generate hash in PowerShell on Windows 11
$bytes = [System.IO.File]::ReadAllBytes("bundle.js")
$hash = [System.Security.Cryptography.SHA384]::Create().ComputeHash($bytes)
$sri = "sha384-" + [Convert]::ToBase64String($hash)
Write-Output $sri

Enforcing SRI Globally via Content Security Policy

While authoring integrity attributes on individual tags works well, rogue developers or third-party plugins might inadvertently embed unhashed scripts. Enforce SRI site-wide using the CSP directive:

# Enforce SRI for all scripts and stylesheets in Nginx
add_header Content-Security-Policy "require-sri-for script style; default-src 'self';" always;

When require-sri-for is active, Google Chrome blocks any <script> or <link rel="stylesheet"> tag that lacks a valid SRI hash, preventing unauthorised external code execution.

Auditing SRI Failures in Chrome DevTools

To inspect and troubleshoot SRI verification failures in Google Chrome:

  1. Open Google Chrome and navigate to your web page.
  2. Open Chrome DevTools by pressing Ctrl + Shift + I (or Option + Command + I on macOS).
  3. Select the Console tab. If an asset has been modified or served with an incorrect hash, Chrome outputs:
    Failed to find a valid digest in the 'integrity' attribute for resource 'https://cdn.example.com/libs/chart.min.js' with computed SHA-384 integrity 'xyz...'. The resource has been blocked.
  4. Inspect the Network tab, where the blocked request appears highlighted in red with status (blocked:integrity).
  5. Ensure your CDN server delivers the HTTP response header Access-Control-Allow-Origin: *. If CORS headers are absent, Chrome blocks SRI evaluation automatically.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.