vite-plugin-sri4 3.1.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +61 -11
  2. package/dist/index.cjs +415 -139
  3. package/dist/index.js +415 -139
  4. package/package.json +10 -11
package/README.md CHANGED
@@ -12,6 +12,8 @@ A Vite plugin to generate Subresource Integrity (SRI) hashes for your assets dur
12
12
  - [Installation](#installation)
13
13
  - [Usage](#usage)
14
14
  - [Plugin Options](#plugin-options)
15
+ - [Dynamic Routes](#dynamic-routes)
16
+ - [When SRI Actually Helps](#when-sri-actually-helps)
15
17
  - [Example Project](#example-project)
16
18
  - [Best Practices](#best-practices)
17
19
  - [Troubleshooting](#troubleshooting)
@@ -27,7 +29,8 @@ A Vite plugin to generate Subresource Integrity (SRI) hashes for your assets dur
27
29
  - **Bypass Domains:** Option to specify domains to bypass SRI injection.
28
30
  - **Missing Asset Handling:** Configurable warning suppression for missing assets.
29
31
  - **Robust Content Support:** Handles various content types including strings, Buffer, and Uint8Array.
30
- - **Vite Compatibility:** Compatible with Vite 6.0 and 7.0.
32
+ - **Dynamic Routes:** Optional import map integrity and an SRI manifest cover `import()`-loaded chunks and SSR builds, which have no build-time HTML tag to rewrite.
33
+ - **Vite Compatibility:** Compatible with Vite 6.4, 7.0 and 8.0 (including the Rolldown-based native build path). `6.4` is the floor because it is the only Vite 6 line still receiving upstream security patches.
31
34
 
32
35
  ## Installation
33
36
 
@@ -48,13 +51,18 @@ export default defineConfig({
48
51
  plugins: [
49
52
  sri({
50
53
  // Optional. The security hash algorithm. Defaults to "sha384".
51
- algorithm: 'sha384',
54
+ hashAlgorithm: 'sha384',
52
55
  // Optional. Domains to bypass SRI injection.
53
56
  bypassDomains: ['example.com'],
54
57
  // Optional. Suppress warnings for missing assets.
55
58
  ignoreMissingAsset: false,
56
- // Optional. Enable debug logging.
57
- debug: false
59
+ // Optional. Log verbosity: 'silent' | 'error' | 'warn' | 'info' | 'debug'. Defaults to 'warn'.
60
+ logLevel: 'warn',
61
+ // Optional. Inject an import map carrying integrity for every JS chunk,
62
+ // covering dynamically imported routes. Defaults to false.
63
+ importmap: false,
64
+ // Optional. Emit dist/sri-manifest.json for SSR to read. Defaults to false.
65
+ manifest: false
58
66
  })
59
67
  ]
60
68
  });
@@ -76,14 +84,55 @@ Output:
76
84
 
77
85
  ## Plugin Options
78
86
 
79
- * `algorithm` (string):
80
- The hash algorithm used for computing SRI. Default is sha384. You may change it to other supported algorithms like sha256.
87
+ * `hashAlgorithm` (string):
88
+ The hash algorithm used for computing SRI. Default is `sha384`. You may change it to other supported algorithms like `sha256` or `sha512`.
81
89
  * `bypassDomains` (Array<string>):
82
90
  Array of domain names where SRI injection should be skipped. This allows external resources from specified domains to be excluded from SRI checks (for example, when they may not support CORS).
83
91
  * `ignoreMissingAsset` (boolean):
84
- When true, suppresses warnings for assets that are not found in the bundle. Default is false.
85
- * `debug` (boolean):
86
- When true, enables detailed debug logging. Default is false.
92
+ When true, suppresses warnings for assets that are not found in the bundle. Default is `false`.
93
+ * `logLevel` (string):
94
+ Log verbosity. One of `silent`, `error`, `warn`, `info`, `debug`. Default is `warn`. Use `debug` to see per-resource decisions during the build.
95
+ * `importmap` (boolean):
96
+ Inject a `<script type="importmap">` containing an `integrity` map for every JS chunk in the build. Default is `false`. See [Dynamic routes](#dynamic-routes).
97
+ * `manifest` (boolean):
98
+ Emit `sri-manifest.json` alongside the build, mapping every non-HTML output file to its SRI hash. Default is `false`. See [Dynamic routes](#dynamic-routes).
99
+
100
+ ## Dynamic routes
101
+
102
+ Rewriting HTML tags can only protect resources that have a tag at build time. A route loaded with `import()` has none - Vite's preload helper creates the `<link rel="modulepreload">` at runtime - and an SSR build emits no HTML at all. Two options cover those cases.
103
+
104
+ ### `importmap: true` (client-side dynamic imports)
105
+
106
+ Emits an import map whose `integrity` key covers every JS chunk, including chunks only ever reached through `import()`:
107
+
108
+ ```html
109
+ <script type="importmap">{"integrity":{"/assets/about-a1b2c3.js":"sha384-..."}}</script>
110
+ ```
111
+
112
+ The map is injected before the first `<script>` so it applies to every module. Engines without support ignore the `integrity` key rather than failing, so this degrades safely - but check current browser support before relying on it as your only protection.
113
+
114
+ ### `manifest: true` (SSR / server-rendered HTML)
115
+
116
+ Emits `sri-manifest.json` mapping output file names to hashes, which a server rendering HTML per request can read:
117
+
118
+ ```json
119
+ {
120
+ "assets/index-a1b2c3.js": "sha384-...",
121
+ "assets/index-d4e5f6.css": "sha384-..."
122
+ }
123
+ ```
124
+
125
+ This is the only mechanism available when the build produces no HTML asset.
126
+
127
+ ### Caveat for both
128
+
129
+ Hashes are computed during the build. A plugin that mutates chunk contents after this one (`@vitejs/plugin-legacy`, compression plugins that rewrite in place) would invalidate them, so the plugin re-hashes every file it touched in `writeBundle` and fails the build if anything drifted. You get a build error rather than a page that only breaks in the browser.
130
+
131
+ ## When SRI Actually Helps
132
+
133
+ SRI is worth the most when your HTML and your assets have **different trust boundaries** - typically HTML served from your own origin and JS/CSS served from a CDN (`base: 'https://cdn.example.com/'`). If the CDN is compromised or a cache is poisoned, the integrity attribute in your origin-served HTML is what stops the browser from running the tampered file. That is the case this plugin is built for.
134
+
135
+ If everything is served from a single origin, SRI buys much less than it appears to: an attacker who can rewrite `/assets/index-abc123.js` on your server can usually rewrite the `index.html` carrying its hash just as easily. It is not useless - it narrows some deploy and cache-layer mistakes - but for same-origin builds, a Content Security Policy and Vite's default hashed, immutable filenames do more for you than SRI does. Enable it because it is cheap, not because it closes the hole you think it closes.
87
136
 
88
137
  ## Example Project
89
138
 
@@ -150,11 +199,11 @@ The example project shows:
150
199
 
151
200
  ### Debug Mode
152
201
 
153
- Enable debug mode to see detailed logs:
202
+ Set `logLevel: 'debug'` to see detailed logs:
154
203
 
155
204
  ```javascript
156
205
  sri({
157
- debug: true
206
+ logLevel: 'debug'
158
207
  })
159
208
  ```
160
209
 
@@ -163,6 +212,7 @@ This will show:
163
212
  - SRI hash computation
164
213
  - CORS checks
165
214
  - Missing asset warnings
215
+ - Bundle-key fallback matches (when a URL is resolved via suffix match)
166
216
 
167
217
  ## Contributing
168
218