🧩
Security

Subresource Integrity (SRI): protecting external scripts

03.12.2026
← All articles

Many websites load jQuery, Bootstrap or font libraries from an external CDN rather than their own server. It is convenient: files load fast, hosting traffic is saved and updates are easy. But one question is often overlooked: if that CDN is compromised or its domain passes to a new owner, foreign code runs in every visitor's browser. Subresource Integrity (SRI) protects against exactly this. The browser compares the downloaded file with a hash you specified in advance and refuses to run it if they differ.

How SRI works

You add an integrity attribute to a <script> or <link> tag containing a cryptographic hash of the file, for example a base64-encoded SHA-384 value. After downloading the file, the browser computes the hash itself. If even one character has changed, the hash differs and the resource is blocked with a console error. Instead of trusting the CDN, you trust a specific file version, so an attacker who alters the file only breaks that library rather than stealing passwords or card data. Popular CDN domains have changed hands and served malicious code in the past, so this is a real risk.

Syntax and the crossorigin attribute

<script src="https://cdn.example.com/library@3.7.1/library.min.js"
        integrity="sha384-YOUR_COMPUTED_HASH"
        crossorigin="anonymous"></script>

<link rel="stylesheet"
      href="https://cdn.example.com/design@5.3.3/design.min.css"
      integrity="sha384-YOUR_COMPUTED_HASH"
      crossorigin="anonymous">

For cross-origin resources the request must use CORS, so crossorigin="anonymous" is required and the CDN must send Access-Control-Allow-Origin. Major public CDNs do; without it the resource will not load at all. You may list several hashes separated by spaces, and the browser uses those with the strongest algorithm.

Computing the hash

OpenSSL, available on Linux, macOS and Git Bash for Windows, is enough:

curl -sO https://cdn.example.com/library@3.7.1/library.min.js
openssl dgst -sha384 -binary library.min.js | openssl base64 -A

Prefix the result with sha384-. If the file is already compromised you would be approving a malicious version, so compare your value with the hash published by the library's maintainers where available.

Example: a school website in Andijan

A private school in Andijan has a simple HTML and PHP site with a CSS library and several scripts from a public CDN, plus a parent login page where a tampered script could exfiltrate passwords. The developer pins exact versions, computes hashes and adds integrity and crossorigin; for the login page the libraries are moved to the school's own hosting. Months later the developer upgrades a library but forgets the hash, the menu breaks and the console shows:

Failed to find a valid digest in the 'integrity' attribute for resource
'https://cdn.example.com/library@3.8.0/library.min.js' with computed
SHA-384 integrity '...'. The resource has been blocked.

That is SRI working as intended. Recomputing the hash fixes it, and keeping versions and hashes together in one config file prevents repeats.

SRI in WordPress and build tools

wp_enqueue_script does not add integrity itself, but a filter can:

add_filter('script_loader_tag', function ($tag, $handle) {
    if ($handle === 'library') {
        $tag = str_replace(' src=',
            ' integrity="sha384-YOUR_HASH" crossorigin="anonymous" src=',
            $tag);
    }
    return $tag;
}, 10, 2);

Webpack and Vite have plugins that compute hashes at build time and write them into the HTML.

Where SRI does not fit

SRI only suits files that never change. Unversioned "latest" URLs, analytics tags, chat widgets and payment SDKs that providers update regularly cannot use it; there you should choose trusted providers, reduce external scripts and restrict sources with a Content Security Policy. Often the simplest answer is to self-host key libraries. sayt.uz hosting runs on servers in Uzbekistan on the Tas-IX network, so local visitors get such files quickly without depending on an external CDN.

Related articles

📂 Secrets in a public GitHub repo: what to check before you publish 📄 XXE Vulnerabilities and XML Security: Stopping External Entities 🚨 Where to report a cyberattack in Uzbekistan 🌐 What Is SSRF and How to Protect Your Server From It
🌐 Language
🇺🇿 O⁠'zbek 🇺🇿 Özbek (yangi) 🇺🇿 Ўзбек 🇷🇺 Русский 🇬🇧 English ✓