Subresource Integrity (SRI)
You'll learn how browsers can cryptographically verify that third-party scripts haven't been tampered with, protecting your app from CDN compromises and man-in-the-middle attacks.
What Is Subresource Integrity?
Subresource Integrity (SRI) is a security feature that lets browsers verify that files fetched from external sources (like CDNs) haven't been modified. When you include third-party JavaScript libraries or stylesheets, you're trusting that CDN to deliver the exact file you expect. But what if the CDN gets compromised, or an attacker intercepts the request?
SRI works by adding a cryptographic hash to your <script> or <link> tags. The browser downloads the resource, computes its hash, and compares it to the hash you specified. If they don't match, the browser refuses to execute the file.
How It Protects You
Think of SRI like a tamper-evident seal on medication. You know the seal should look a certain way. If it's broken or different, you don't trust the contents — even if the bottle came from the pharmacy.
Without SRI, if an attacker compromises a popular CDN or performs a man-in-the-middle attack, they could inject malicious code into widely-used libraries. Your application would unknowingly execute that malicious code. SRI prevents this by ensuring the file content matches exactly what you intended, byte-for-byte.
How CSP and SRI Work Together
CSP (from the previous lesson) controls where resources can load from. SRI verifies what those resources contain. Together, they create defense-in-depth: CSP limits your attack surface, while SRI ensures trusted sources don't betray that trust.
Key Takeaway: Subresource Integrity uses cryptographic hashes to verify that external resources haven't been tampered with, protecting against CDN compromise and man-in-the-middle attacks by refusing to execute modified files.