From 4c2aec89f25ecea1fdfa8c5615f8aa8064324b70 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Fri, 14 Feb 2025 18:26:25 +0800 Subject: [PATCH] docs: add `SubresourceIntegrityPlugin` (#9302) docs: add sri plugin --- .../scripts/check-documentation-coverage.ts | 3 +- .../rspack/subresource-integrity-plugin.mdx | 189 ++++++++++++++++++ .../rspack/subresource-integrity-plugin.mdx | 185 +++++++++++++++++ 3 files changed, 375 insertions(+), 2 deletions(-) create mode 100644 website/docs/en/plugins/rspack/subresource-integrity-plugin.mdx create mode 100644 website/docs/zh/plugins/rspack/subresource-integrity-plugin.mdx diff --git a/packages/rspack/scripts/check-documentation-coverage.ts b/packages/rspack/scripts/check-documentation-coverage.ts index f033f46c4512..7cfb8707f00c 100644 --- a/packages/rspack/scripts/check-documentation-coverage.ts +++ b/packages/rspack/scripts/check-documentation-coverage.ts @@ -111,8 +111,7 @@ function checkPluginsDocumentationCoverage() { const excludedPlugins = [ "OriginEntryPlugin", "RuntimePlugin", // This plugin only provides hooks, should not be used separately - "RsdoctorPlugin", // This plugin is not stable yet - "SubresourceIntegrityPlugin" // TODO: add document in next pr + "RsdoctorPlugin" // This plugin is not stable yet ]; const undocumentedPlugins = Array.from(implementedPlugins).filter( diff --git a/website/docs/en/plugins/rspack/subresource-integrity-plugin.mdx b/website/docs/en/plugins/rspack/subresource-integrity-plugin.mdx new file mode 100644 index 000000000000..1b98d6312867 --- /dev/null +++ b/website/docs/en/plugins/rspack/subresource-integrity-plugin.mdx @@ -0,0 +1,189 @@ +import { ApiMeta } from '@components/ApiMeta.tsx'; + +# SubresourceIntegrityPlugin + + + +The `rspack.experiments.SubresourceIntegrityPlugin` is a plugin for enabling Subresource Integrity in Rspack. + +## What is SRI + +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 ` +<% } %> + +<% for _ in htmlRspackPlugin.files.css { %> + +<% } %> +``` + +With [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin), you can inject them like this: + +```ejs title="index.ejs" +<% for (let index in htmlWebpackPlugin.files.js) { %> + +<% } %> + +<% for (let index in htmlWebpackPlugin.files.css) { %> + +<% } %> +``` + +### Without HTML Plugin + +The `integrity` can also be obtained from `stats.assets`. For example: + +```js +compiler.plugin('done', stats => { + const integrityValues = stats + .toJson() + .assets.map(asset => [asset.name, asset.integrity]); +}); +``` + +:::tip +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. +::: + +## Options + +#### hashFuncNames + +- **Type:** `Array<"sha256" | "sha384" | "sha512">` +- **Default:** `["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](http://www.w3.org/TR/SRI/#cryptographic-hash-functions) for more details. + +#### enabled + +- **Type:** `"auto" | boolean` +- **Default:** `"auto"` + +- `auto` is the default value, which means the plugin is enabled when [Rspack mode](/config/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. + +#### htmlPlugin + +- **Type:** `string` +- **Default:** `"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. + +## More Information + +You can find more information about Subresource Integrity in the following resources: + +- [webpack-subresource-integrity](https://github.com/waysact/webpack-subresource-integrity/blob/main/webpack-subresource-integrity/README.md) +- [MDN: Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) diff --git a/website/docs/zh/plugins/rspack/subresource-integrity-plugin.mdx b/website/docs/zh/plugins/rspack/subresource-integrity-plugin.mdx new file mode 100644 index 000000000000..428973e35ce9 --- /dev/null +++ b/website/docs/zh/plugins/rspack/subresource-integrity-plugin.mdx @@ -0,0 +1,185 @@ +import { ApiMeta } from '@components/ApiMeta.tsx'; + +# SubresourceIntegrityPlugin + + + +`rspack.experiments.SubresourceIntegrityPlugin` 是一个用于在 Rspack 中启用子资源完整性的插件。 + +## 什么是 SRI + +子资源完整性 Subresource Integrity(SRI)是专门用来校验资源的一种方案,它读取资源标签中的 integrity 属性,将其中的信息摘要值,和资源实际的信息摘要值进行对比,如果发现无法匹配,那么浏览器就会拒绝执行资源。 + +对于 ` +<% } %> + +<% for _ in htmlRspackPlugin.files.css { %> + +<% } %> +``` + +对于 [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin),可以这样注入属性: + +```ejs title="index.ejs" +<% for (let index in htmlWebpackPlugin.files.js) { %> + +<% } %> + +<% for (let index in htmlWebpackPlugin.files.css) { %> + +<% } %> +``` + +### 不使用 HTML 插件 + +若不使用 HTML 插件,也可以从 `stats.assets` 中获取 `integrity` 属性,如: + +```js +compiler.plugin('done', stats => { + const integrityValues = stats + .toJson() + .assets.map(asset => [asset.name, asset.integrity]); +}); +``` + +:::tip 提示 +当你在 `link` 和 `script` 标签上添加 `integrity` 属性时,你还需要设置 `crossorigin` 属性。建议将此属性设置为与 Rspack 的 `output.crossOriginLoading` 配置选项相同的值。 +::: + +## 选项 + +#### hashFuncNames + +- **类型:** `Array<"sha256" | "sha384" | "sha512">` +- **默认值:** `["sha384"]` + +一个字符串数组,每个字符串指定用于计算完整性哈希值的哈希函数的名称。目前仅支持 `sha256`、`sha384` 和 `sha512`。 + +> 更多详情请参见 [SRI:加密哈希函数](http://www.w3.org/TR/SRI/#cryptographic-hash-functions)。 + +#### enabled + +- **类型:** `"auto" | boolean` +- **默认值:** `"auto"` + +- `auto` 是默认值,表示在 [Rspack mode](/config/mode) 为 `production` 或 `none` 时启用插件,在 `development` 时禁用插件。 +- `true` 表示在任意 mode 下都启用插件。 +- `false` 表示在任意 mode 下都不启用插件。 + +#### htmlPlugin + +- **类型:** `string` +- **默认值:** `"HtmlRspackPlugin"` + +HTML 插件的路径,默认为 `"HtmlRspackPlugin"`,表示 Rspack 的原生 HTML 插件。如果你使用的是 [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin),你可以将此选项设置为它的路径。建议设置绝对路径以确保能找到正确的插件实例。 + +## 更多信息 + +更多关于子资源完整性的信息可参考: + +- [webpack-subresource-integrity](https://github.com/waysact/webpack-subresource-integrity/blob/main/webpack-subresource-integrity/README.md) +- [MDN:子资源完整性](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)