gitmyhub

webpack-subresource-integrity

JavaScript ★ 4 updated 9y ago ⑂ fork

Webpack plugin for enabling Subresource Integrity.

A Webpack plugin that adds Subresource Integrity hashes to bundled files so browsers reject tampered scripts and stylesheets.

JavaScriptWebpacksetup: easycomplexity 2/5

Plain-English Explanation: webpack-subresource-integrity

This plugin adds a security feature called Subresource Integrity (SRI) to web applications built with Webpack. In plain terms, it cryptographically "signs" your JavaScript and CSS files so that browsers can verify the files haven't been tampered with or corrupted before running them. This is especially important if you're serving files from a CDN or third-party server—SRI lets your browser reject any file that doesn't match the signature you provided.

The plugin works by calculating a hash (a unique digital fingerprint) for each of your bundled files during the build process. It then embeds that hash into your HTML as a special integrity attribute on script and link tags. When your browser downloads the file, it recalculates the hash and compares it. If the hashes don't match—meaning the file was modified somewhere along the way—the browser refuses to load it instead of running potentially malicious code.

You'd use this if you're concerned about the security of your code delivery, especially if your files travel over networks you don't fully control or are served by third parties. It's most valuable in production environments where the stakes are high. The plugin integrates smoothly with popular Webpack tools like html-webpack-plugin, so if you're already using those, the integrity attributes get added automatically. For code-splitting and lazy-loaded chunks, it protects those too.

One thing to keep in mind: older browsers (particularly Chrome 45–46) had bugs with SRI that could break script loading if certain characters were present. Also, while this plugin does its job well, SRI protection only works in browsers that support it—older browsers will still load your files without verification. Hot Module Replacement during development isn't protected either, though that's usually fine since you typically disable this plugin in dev mode anyway.

Where it fits