Web Security & OWASP (50 Qs)•Part 4: Browser Security, HTTP Headers & Client Hardening (36 - 42)
Subresource Integrity (SRI): Defending against compromised CDN scripts using cryptographic hashes.
The CDN Compromise Attack Vector
Modern applications frequently load libraries (React, Bootstrap, jQuery, analytics) from third-party public Content Delivery Networks (CDNs). If an attacker compromises the CDN and alters the remote file, they instantly achieve Remote Code Execution across every website loading that script.
ini
VULNERABLE (Blindly trusts remote CDN):
<script src="https://cdn.example.com/analytics.js"></script>
If cdn.example.com is hacked, attacker replaces script with keylogger!
SECURE (Subresource Integrity Enforced):
<script
src="https://cdn.example.com/analytics.js"
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
crossorigin="anonymous">
</script>
How the Browser Evaluates SRI
- The browser downloads
analytics.jsfrom the external CDN. - The browser computes the SHA-384 cryptographic digest of the received bytes.
- If the computed hash does not match the
integrityattribute byte-for-byte, the browser refuses to execute the script, terminates execution, and logs a security error in the console. crossorigin="anonymous": Mandatory for SRI on cross-origin requests to prevent leaking file content cross-origin.