Course contentsShow
← Web Development
Lesson 2935 of 2,98252. Frontend SecurityFree lesson

What is Subresource Integrity (SRI)

Understanding how SRI prevents tampered or malicious third-party resources from executing on your site.

What is Subresource Integrity (SRI)

You'll learn how SRI acts as a cryptographic safety seal that prevents compromised third-party resources from executing malicious code on your site.

The Problem SRI Solves

When you load JavaScript libraries or stylesheets from CDNs (like jQuery from a third-party server), you're trusting that the file hasn't been tampered with. If that CDN gets hacked or compromised, attackers could inject malicious code into those files—and your site would unknowingly execute it for all your users.

How SRI Works

Subresource Integrity is like adding a tamper-evident seal to external resources. You generate a cryptographic hash of the exact file you expect to load, then add that hash to your HTML. The browser downloads the resource, calculates its hash, and compares it to your provided value.

If the hashes match: The resource is unchanged—safe to execute.
If they don't match: The browser blocks the resource entirely, preventing potentially malicious code from running.

Think of it like a lock and key for your third-party scripts. You're saying "I only want to execute this exact version of this file—nothing else."

Why It Matters for Frontend Security

SRI complements your existing security measures (like CSP and HTTPS). Even if HTTPS ensures the connection is encrypted, it doesn't verify the content hasn't been altered at the source. SRI fills this gap by validating the integrity of the resource itself.

This is especially critical when using public CDNs, where you have no control over the server infrastructure. If a CDN is compromised, SRI is your last line of defense against executing malicious code.

Key Takeaway: Subresource Integrity (SRI) uses cryptographic hashes to verify that third-party resources haven't been tampered with, blocking execution if the file doesn't match the expected integrity hash—protecting your users from compromised CDN content.