The rspack.experiments.SubresourceIntegrityPlugin
is a plugin for enabling Subresource Integrity in Rspack.
Subresource Integrity (SRI) is a security feature that enables browsers to verify that resources they fetch (for example, from a CDN) are delivered without unexpected manipulation. It works by allowing you to provide a cryptographic hash that a fetched resource must match.
For <script>
tags, the result is to refuse to execute the code; for CSS links, the result is not to load the styles.
For more on subresource integrity, see Subresource Integrity - MDN.
The plugin supports integration with HtmlRspackPlugin
and html-webpack-plugin
. It will automatically set the integrity
and crossorigin
attributes for the injected tags.
The plugin supports code splitting. When you use dynamic imports, the plugin will automatically set the integrity
and crossorigin
attributes for the generated chunk loading tags.
You can use the plugin by importing it as an experimental plugin from @rspack/core
:
Or:
The output.crossOriginLoading option is required for SRI to work:
When the HTML plugin(HtmlRspackPlugin
or html-webpack-plugin
) is used, the integrity
and crossorigin
attributes will be set automatically.
The SubresourceIntegrityPlugin will interact with HtmlRspackPlugin
by default:
If html-webpack-plugin
is used, the htmlPlugin
option should be specified to the path of it:
inject: false
If you use the HTML plugin with inject: false
, you need to set the integrity
and crossorigin
attributes in your template manually.
With HtmlRspackPlugin
, the grammar of the template is a bit different with .ejs
(see here), you can inject them like this:
With html-webpack-plugin
, you can inject them like this:
The integrity
can also be obtained from stats.assets
. For example:
Note that when you add the integrity
attribute on your link
and
script
tags, you're also required to set the crossorigin
attribute. It is recommended to set this attribute to the same value
as the Rspack output.crossOriginLoading
configuration option.
Array<"sha256" | "sha384" | "sha512">
["sha384"]
An array of strings, each specifying the name of a hash function to be
used for calculating integrity hash values. Only supports sha256
, sha384
, and sha512
yet.
See SRI: Cryptographic hash functions for more details.
Type: "auto" | boolean
Default: "auto"
auto
is the default value, which means the plugin is enabled when Rspack mode is production
or none
, and disabled when it is development
.
true
means the plugin is enabled in any mode.
false
means the plugin is disabled in any mode.
string
"HtmlRspackPlugin"
The path to the HTML plugin, defaults to "HtmlRspackPlugin"
which means the native HTML plugin of Rspack. If you are using the html-webpack-plugin
, you can set this option to the path of it. It is recommended to set the absolute path to make sure the plugin can be found.
You can find more information about Subresource Integrity in the following resources: