vite-plugin-sri4 4.0.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 +51 -2
  2. package/dist/index.cjs +379 -135
  3. package/dist/index.js +379 -135
  4. package/package.json +10 -9
package/dist/index.cjs CHANGED
@@ -109,8 +109,21 @@ async function checkResourceSupport(url, urlSupportCache, logger = null, retries
109
109
 
110
110
  clearTimeout(timeoutId);
111
111
 
112
+ // Only `*` can be verified at build time. Injecting integrity also means
113
+ // injecting crossorigin="anonymous"; if the server answers with a
114
+ // concrete origin that does not match wherever the HTML ends up being
115
+ // served from, that turns a working script into a blocked one. Skipping
116
+ // is the safe outcome, but say so at warn level - silence here is what
117
+ // makes an unprotected resource easy to miss.
112
118
  const corsHeader = response.headers.get('access-control-allow-origin');
113
- const isSupported = response.ok && (corsHeader === '*' || corsHeader?.includes('*'));
119
+ const isSupported = response.ok && corsHeader === '*';
120
+ if (response.ok && corsHeader && corsHeader !== '*' && logger) {
121
+ logger.warn(
122
+ `Skipping SRI for ${url}: Access-Control-Allow-Origin is "${corsHeader}", not "*", ` +
123
+ 'so crossorigin="anonymous" cannot be verified at build time. ' +
124
+ 'Add the domain to bypassDomains to silence this.'
125
+ );
126
+ }
114
127
  urlSupportCache.set(url, isSupported);
115
128
  return isSupported
116
129
  } catch (error) {
@@ -182,43 +195,104 @@ async function fetchResource(url, resourceCache, logger = null, retries = 1) {
182
195
  return null
183
196
  }
184
197
 
198
+ /**
199
+ * Read the hashable source out of a bundle entry (chunk code or asset source)
200
+ */
201
+ function bundleSource(item) {
202
+ return item.type === 'chunk' ? item.code : item.source
203
+ }
204
+
205
+ /**
206
+ * Compute an SRI string for a source that may be a string, Buffer or Uint8Array
207
+ */
208
+ function sriHash(source, hashAlgorithm) {
209
+ const hash = node_crypto.createHash(hashAlgorithm);
210
+ hash.update(typeof source === 'string' ? source : Buffer.from(source));
211
+ return `${hashAlgorithm}-${hash.digest('base64')}`
212
+ }
213
+
214
+ // Anything carrying a scheme (data:, blob:, invalid:) is not a path into the
215
+ // bundle. Protocol-relative `//host/path` is excluded here - it is a real HTTP
216
+ // URL and is fetched, not skipped.
217
+ const SCHEME_RE = /^[a-z][a-z0-9+.-]*:/i;
218
+ const HTTP_RE = /^https?:/i;
219
+
220
+ /**
221
+ * The URL to fetch for an external resource, or null if it is not fetchable.
222
+ */
223
+ function externalUrl(url) {
224
+ if (HTTP_RE.test(url)) return url
225
+ // Protocol-relative: same origin scheme as the page, https at build time
226
+ if (url.startsWith('//')) return `https:${url}`
227
+ return null
228
+ }
229
+
185
230
  /**
186
231
  * Improved method for getting bundle keys
187
232
  */
188
233
  function getBundleKey(htmlPath, url, config) {
234
+ // Bundle keys never carry a query string or fragment
235
+ const cleanUrl = url.replace(/[?#].*$/, '');
236
+
189
237
  // Handle absolute path URLs
190
- if (url.startsWith('/')) {
238
+ if (cleanUrl.startsWith('/')) {
191
239
  // Remove leading slash to match keys in bundle
192
- return url.substring(1)
240
+ return cleanUrl.substring(1)
193
241
  }
194
242
 
195
- // Handle relative paths (when config.base is relative)
243
+ // Handle relative paths (when config.base is relative). `join`, not
244
+ // `resolve` - bundle keys are relative to the output root, and `resolve`
245
+ // would make the key absolute against the process CWD.
196
246
  if (config.base === './' || config.base === '') {
197
- return path.posix.resolve(path.posix.dirname(htmlPath), url)
247
+ return path.posix.join(path.posix.dirname(htmlPath), cleanUrl)
198
248
  }
199
249
 
200
250
  // Handle other cases, remove base prefix from URL
201
- return url.startsWith(config.base)
202
- ? url.substring(config.base.length)
203
- : url
251
+ return cleanUrl.startsWith(config.base)
252
+ ? cleanUrl.substring(config.base.length)
253
+ : cleanUrl
254
+ }
255
+
256
+ /**
257
+ * Find a bundle key for a URL that did not match exactly.
258
+ *
259
+ * Matching is anchored on a path separator so `main.js` can never match
260
+ * `assets/vendor-main.js` - a cross-filename match would inject a valid-looking
261
+ * but wrong hash, which the browser rejects with no build-time error.
262
+ */
263
+ function findBundleKey(bundle, bundleKey, logger = null) {
264
+ const candidates = Object.keys(bundle).filter(key =>
265
+ key === bundleKey ||
266
+ key.endsWith(`/${bundleKey}`) ||
267
+ bundleKey.endsWith(`/${key}`)
268
+ );
269
+
270
+ if (candidates.length > 1 && logger) {
271
+ logger.warn(
272
+ `Ambiguous bundle key for "${bundleKey}": ${candidates.join(', ')} - using ${candidates[0]}`
273
+ );
274
+ }
275
+
276
+ return candidates[0]
204
277
  }
205
278
 
206
279
  /**
207
280
  * Calculate SRI integrity hash for a given resource
208
281
  */
209
282
  async function calculateIntegrity(
210
- bundle,
211
- htmlPath,
212
- url,
213
- options,
214
- config,
283
+ bundle,
284
+ htmlPath,
285
+ url,
286
+ options,
287
+ config,
215
288
  cacheManager,
216
289
  logger = null
217
290
  ) {
218
- const {
219
- ignoreMissingAsset,
220
- bypassDomains,
221
- hashAlgorithm
291
+ const {
292
+ ignoreMissingAsset,
293
+ bypassDomains,
294
+ hashAlgorithm,
295
+ hashedAssets
222
296
  } = options;
223
297
 
224
298
  // Skip specified domains
@@ -226,32 +300,43 @@ async function calculateIntegrity(
226
300
  return null
227
301
  }
228
302
 
303
+ // With an absolute `base` (assets on a CDN) Vite emits absolute URLs for our
304
+ // own build output. Those must be hashed from the bundle, not fetched - the
305
+ // CDN may not have been deployed yet, and this is the very case SRI exists
306
+ // for. Checked before the network path.
307
+ const base = config.base || '/';
308
+ const ownAsset = (HTTP_RE.test(base) || base.startsWith('//')) && url.startsWith(base);
309
+ const fetchUrl = ownAsset ? null : externalUrl(url);
310
+
229
311
  let source;
230
- if (url.startsWith('http')) {
231
- const isSupported = await checkResourceSupport(url, cacheManager.getUrlSupportCache(), logger);
312
+ let bundleFileName = null;
313
+ if (fetchUrl) {
314
+ const isSupported = await checkResourceSupport(fetchUrl, cacheManager.getUrlSupportCache(), logger);
232
315
  if (!isSupported) return null
233
- source = await fetchResource(url, cacheManager.getResourceCache(), logger);
316
+ source = await fetchResource(fetchUrl, cacheManager.getResourceCache(), logger);
234
317
  if (!source) return null
318
+ } else if (!ownAsset && SCHEME_RE.test(url)) {
319
+ // data:/blob: and unknown schemes cannot be resolved to a bundle asset
320
+ if (logger) {
321
+ logger.debug(`Skipping URL that is not a bundle asset: ${url}`);
322
+ }
323
+ return null
235
324
  } else {
236
325
  const bundleKey = getBundleKey(htmlPath, url, config);
237
326
  const bundleItem = bundle[bundleKey];
238
327
 
239
328
  if (!bundleItem) {
240
- // Fall back to suffix match in either direction to absorb hashed
241
- // filenames AND base-prefix mismatches (e.g. URL "/base/main.js" with
242
- // bare bundle key "main.js"). A mismatch here just produces a wrong
243
- // integrity hash, which the browser rejects — failure-closed.
244
- const possibleMatch = Object.keys(bundle).find(key =>
245
- key.endsWith(bundleKey) || bundleKey.endsWith(key)
246
- );
329
+ // Fall back to a path-anchored suffix match to absorb hashed filenames
330
+ // AND base-prefix mismatches (e.g. URL "/base/main.js" with bare bundle
331
+ // key "main.js").
332
+ const possibleMatch = findBundleKey(bundle, bundleKey, logger);
247
333
 
248
334
  if (possibleMatch) {
249
335
  if (logger) {
250
336
  logger.debug(`Bundle key fallback: ${bundleKey} -> ${possibleMatch}`);
251
337
  }
252
- source = bundle[possibleMatch].type === 'chunk'
253
- ? bundle[possibleMatch].code
254
- : bundle[possibleMatch].source;
338
+ bundleFileName = possibleMatch;
339
+ source = bundleSource(bundle[possibleMatch]);
255
340
  } else if (ignoreMissingAsset) {
256
341
  if (logger) {
257
342
  logger.warn(`Asset not found in bundle: ${url} (path: ${htmlPath}, key: ${bundleKey})`);
@@ -261,33 +346,65 @@ async function calculateIntegrity(
261
346
  throw new Error(`Asset ${url} not found in bundle (path: ${htmlPath}, key: ${bundleKey})`)
262
347
  }
263
348
  } else {
264
- source = bundleItem.type === 'chunk' ? bundleItem.code : bundleItem.source;
349
+ bundleFileName = bundleKey;
350
+ source = bundleSource(bundleItem);
265
351
  }
266
352
  }
267
353
 
268
354
  // Ensure source is a Uint8Array or string
269
355
  if (!source) return null
270
356
 
271
- if (typeof source === 'string') {
272
- return `${hashAlgorithm}-${node_crypto.createHash(hashAlgorithm).update(source).digest('base64')}`
273
- }
357
+ const integrity = sriHash(source, hashAlgorithm);
358
+ // Recorded so writeBundle can catch a later plugin rewriting these bytes
359
+ if (bundleFileName && hashedAssets) hashedAssets.set(bundleFileName, integrity);
360
+ return integrity
361
+ }
362
+
363
+ /**
364
+ * Match an attribute regardless of quoting style. Attribute order inside a tag
365
+ * is not significant in HTML, so attributes are read out of the matched tag
366
+ * rather than being baked into the tag regex.
367
+ */
368
+ function attrPattern(name) {
369
+ // Lookbehind on whitespace, not `\b` - `\b` matches after the hyphen in
370
+ // `data-src`, and getAttr takes the first match, so a decoy attribute would
371
+ // hijack the URL.
372
+ return new RegExp(`(?<=\\s)${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s"'>]+))`, 'i')
373
+ }
274
374
 
275
- return `${hashAlgorithm}-${node_crypto.createHash(hashAlgorithm).update(Buffer.from(source)).digest('base64')}`
375
+ // Attributes are always preceded by whitespace inside a tag, so anchoring on
376
+ // it avoids matching `data-integrity`. `crossorigin` is matched with or without
377
+ // a value - Vite emits the valueless form, and duplicating it is invalid HTML.
378
+ const CROSSORIGIN_ATTR_RE = /\scrossorigin(?=[\s=>/]|$)/i;
379
+ const INTEGRITY_ATTR_RE = /\sintegrity\s*=/i;
380
+
381
+ const SRC_RE = attrPattern('src');
382
+ const HREF_RE = attrPattern('href');
383
+ const REL_RE = attrPattern('rel');
384
+
385
+ // rel values whose tags carry an integrity attribute
386
+ const SRI_LINK_RELS = new Set(['stylesheet', 'modulepreload']);
387
+
388
+ function getAttr(tag, re) {
389
+ const match = tag.match(re);
390
+ if (!match) return null
391
+ return match[1] ?? match[2] ?? match[3] ?? null
276
392
  }
277
393
 
278
- // Optimized regex patterns for better readability and efficiency
279
394
  const HTML_PATTERNS = {
280
395
  script: {
281
- regex: /<script\b[^>]*?\bsrc\s*=\s*["']([^"']+)["'][^>]*><\/script>/g,
282
- endOffset: 10
396
+ regex: /<script\b[^>]*><\/script>/gi,
397
+ endOffset: 10, // length of '></script>'
398
+ getUrl: tag => getAttr(tag, SRC_RE)
283
399
  },
284
- stylesheet: {
285
- regex: /<link\b[^>]*?\brel\s*=\s*["']stylesheet["'][^>]*?\bhref\s*=\s*["']([^"']+)["'][^>]*>/g,
286
- endOffset: 1
287
- },
288
- modulepreload: {
289
- regex: /<link\b[^>]*?\brel\s*=\s*["']modulepreload["'][^>]*?\bhref\s*=\s*["']([^"']+)["'][^>]*>/g,
290
- endOffset: 1
400
+ link: {
401
+ regex: /<link\b[^>]*>/gi,
402
+ endOffset: 1, // length of '>'
403
+ getUrl: tag => {
404
+ const rel = getAttr(tag, REL_RE);
405
+ if (!rel || !SRI_LINK_RELS.has(rel.trim().toLowerCase())) return null
406
+ return getAttr(tag, HREF_RE)
407
+ }
291
408
  }
292
409
  };
293
410
 
@@ -302,29 +419,42 @@ function validateHtmlInput(html, htmlPath, logger) {
302
419
  return true
303
420
  }
304
421
 
422
+ /**
423
+ * Offset inside the matched tag where new attributes should go: just before the
424
+ * closing `>`, skipping back over the self-closing slash and any whitespace so
425
+ * `<link ... />` does not become `<link ... / integrity="...">`.
426
+ */
427
+ function insertOffset(tag, endOffset) {
428
+ let at = tag.length - endOffset;
429
+ while (at > 0 && (tag[at - 1] === '/' || /\s/.test(tag[at - 1]))) at--;
430
+ return at
431
+ }
432
+
305
433
  /**
306
434
  * Process a single match to create an integrity change object
307
435
  */
308
436
  async function processMatch(
309
- match,
310
- endOffset,
311
- bundle,
312
- htmlPath,
313
- options,
314
- config,
437
+ match,
438
+ pattern,
439
+ bundle,
440
+ htmlPath,
441
+ options,
442
+ config,
315
443
  cacheManager,
316
444
  logger
317
445
  ) {
318
- const [, url] = match;
446
+ const tag = match[0];
447
+ if (INTEGRITY_ATTR_RE.test(tag)) return null
448
+
449
+ const url = pattern.getUrl(tag);
319
450
  if (!url) return null
320
451
 
321
- const end = match.index + match[0].length;
322
452
  const integrity = await calculateIntegrity(
323
- bundle,
324
- htmlPath,
325
- url,
326
- options,
327
- config,
453
+ bundle,
454
+ htmlPath,
455
+ url,
456
+ options,
457
+ config,
328
458
  cacheManager,
329
459
  logger
330
460
  );
@@ -332,8 +462,8 @@ async function processMatch(
332
462
  if (integrity) {
333
463
  return {
334
464
  integrity,
335
- position: end - endOffset,
336
- tagStart: match.index,
465
+ position: match.index + insertOffset(tag, pattern.endOffset),
466
+ tag,
337
467
  url // For logging
338
468
  }
339
469
  }
@@ -344,22 +474,21 @@ async function processMatch(
344
474
  * Process matches for a specific HTML pattern
345
475
  */
346
476
  async function processPatternMatches(
347
- html,
348
- pattern,
349
- bundle,
350
- htmlPath,
351
- options,
352
- config,
477
+ html,
478
+ pattern,
479
+ bundle,
480
+ htmlPath,
481
+ options,
482
+ config,
353
483
  cacheManager,
354
484
  logger
355
485
  ) {
356
- const { regex, endOffset } = pattern;
357
- const matches = [...html.matchAll(regex)];
486
+ const matches = [...html.matchAll(pattern.regex)];
358
487
 
359
488
  // Process each match in parallel
360
489
  const matchResults = await Promise.all(
361
- matches.map(match =>
362
- processMatch(match, endOffset, bundle, htmlPath, options, config, cacheManager, logger)
490
+ matches.map(match =>
491
+ processMatch(match, pattern, bundle, htmlPath, options, config, cacheManager, logger)
363
492
  )
364
493
  );
365
494
 
@@ -371,11 +500,11 @@ async function processPatternMatches(
371
500
  * Collect all integrity changes from HTML patterns
372
501
  */
373
502
  async function collectIntegrityChanges(
374
- html,
375
- bundle,
376
- htmlPath,
377
- options,
378
- config,
503
+ html,
504
+ bundle,
505
+ htmlPath,
506
+ options,
507
+ config,
379
508
  cacheManager,
380
509
  logger
381
510
  ) {
@@ -385,12 +514,12 @@ async function collectIntegrityChanges(
385
514
  await Promise.all(
386
515
  Object.values(HTML_PATTERNS).map(async pattern => {
387
516
  const patternChanges = await processPatternMatches(
388
- html,
389
- pattern,
390
- bundle,
391
- htmlPath,
392
- options,
393
- config,
517
+ html,
518
+ pattern,
519
+ bundle,
520
+ htmlPath,
521
+ options,
522
+ config,
394
523
  cacheManager,
395
524
  logger
396
525
  );
@@ -401,22 +530,9 @@ async function collectIntegrityChanges(
401
530
  return changes
402
531
  }
403
532
 
404
- const CROSSORIGIN_ATTR_RE = /\bcrossorigin\s*=/i;
405
- const INTEGRITY_ATTR_RE = /\bintegrity\s*=/i;
406
-
407
- /**
408
- * Check if integrity attribute already exists in the same tag
409
- */
410
- function hasExistingIntegrity(html, tagStart, position) {
411
- return INTEGRITY_ATTR_RE.test(html.slice(tagStart, position))
412
- }
413
-
414
- /**
415
- * Check if crossorigin attribute already exists in the same tag
416
- */
417
- function hasExistingCrossorigin(html, tagStart, position) {
418
- return CROSSORIGIN_ATTR_RE.test(html.slice(tagStart, position))
419
- }
533
+ // Attributes are always preceded by whitespace inside a tag, so anchoring on
534
+ // it avoids matching `data-integrity`. `crossorigin` is matched with or without
535
+ // a value - Vite emits the valueless form, and duplicating it is invalid HTML.
420
536
 
421
537
  /**
422
538
  * Apply integrity changes to HTML content
@@ -425,14 +541,9 @@ function applyIntegrityChanges(html, changes, logger) {
425
541
  // Sort by position in descending order to insert from back to front
426
542
  changes.sort((a, b) => b.position - a.position);
427
543
 
428
- for (const { integrity, position, tagStart, url } of changes) {
429
- // Skip if integrity attribute already exists on this tag
430
- if (hasExistingIntegrity(html, tagStart, position)) {
431
- continue
432
- }
433
-
544
+ for (const { integrity, position, tag, url } of changes) {
434
545
  let insertText = ` integrity="${integrity}"`;
435
- if (!hasExistingCrossorigin(html, tagStart, position)) {
546
+ if (!CROSSORIGIN_ATTR_RE.test(tag)) {
436
547
  insertText += ' crossorigin="anonymous"';
437
548
  }
438
549
  html = html.slice(0, position) + insertText + html.slice(position);
@@ -442,15 +553,49 @@ function applyIntegrityChanges(html, changes, logger) {
442
553
  return html
443
554
  }
444
555
 
556
+ const EXISTING_IMPORTMAP_RE = /<script\b[^>]*\btype\s*=\s*["']importmap["']/i;
557
+ const FIRST_SCRIPT_RE = /<script\b/i;
558
+ const HEAD_CLOSE_RE = /<\/head\s*>/i;
559
+
560
+ /**
561
+ * Inject an import map carrying an `integrity` map.
562
+ *
563
+ * This is the only mechanism that covers modules pulled in at runtime by
564
+ * `import()` / Vite's preload helper, which have no build-time HTML tag to
565
+ * rewrite. Engines without support ignore the key rather than failing.
566
+ */
567
+ function injectImportmapIntegrity(html, integrity, logger) {
568
+ if (!html || typeof html !== 'string' || Object.keys(integrity).length === 0) {
569
+ return html
570
+ }
571
+
572
+ if (EXISTING_IMPORTMAP_RE.test(html)) {
573
+ logger.warn('HTML already contains an import map; skipping SRI import map injection');
574
+ return html
575
+ }
576
+
577
+ // `<` is escaped so a filename can never close the script element early
578
+ const json = JSON.stringify({ integrity }).replace(/</g, '\\u003c');
579
+ const tag = `<script type="importmap">${json}</script>`;
580
+
581
+ // Must precede every module script, otherwise the map does not apply to them
582
+ for (const re of [FIRST_SCRIPT_RE, HEAD_CLOSE_RE]) {
583
+ const at = html.search(re);
584
+ if (at !== -1) return html.slice(0, at) + tag + html.slice(at)
585
+ }
586
+
587
+ return html + tag
588
+ }
589
+
445
590
  /**
446
591
  * Transform HTML by adding SRI integrity attributes
447
592
  */
448
593
  async function transformHTML(
449
- bundle,
450
- htmlPath,
451
- html,
452
- options,
453
- config,
594
+ bundle,
595
+ htmlPath,
596
+ html,
597
+ options,
598
+ config,
454
599
  cacheManager,
455
600
  logger
456
601
  ) {
@@ -459,11 +604,11 @@ async function transformHTML(
459
604
  }
460
605
 
461
606
  const changes = await collectIntegrityChanges(
462
- html,
463
- bundle,
464
- htmlPath,
465
- options,
466
- config,
607
+ html,
608
+ bundle,
609
+ htmlPath,
610
+ options,
611
+ config,
467
612
  cacheManager,
468
613
  logger
469
614
  );
@@ -476,9 +621,9 @@ async function transformHTML(
476
621
  */
477
622
  function createTransformer(options, config, cacheManager, logger) {
478
623
  return {
479
- transformHTML: (bundle, htmlPath, html) =>
624
+ transformHTML: (bundle, htmlPath, html) =>
480
625
  transformHTML(bundle, htmlPath, html, options, config, cacheManager, logger),
481
- calculateIntegrity: (bundle, htmlPath, url) =>
626
+ calculateIntegrity: (bundle, htmlPath, url) =>
482
627
  calculateIntegrity(bundle, htmlPath, url, options, config, cacheManager, logger)
483
628
  }
484
629
  }
@@ -499,7 +644,9 @@ class Logger {
499
644
  info: 3,
500
645
  debug: 4
501
646
  };
502
- this.currentLevel = this.levels[logLevel] || this.levels.warn;
647
+ // `??`, not `||` - levels.silent is 0, which `||` treats as absent and
648
+ // silently downgrades to 'warn', the one level that must suppress output.
649
+ this.currentLevel = this.levels[logLevel] ?? this.levels.warn;
503
650
  }
504
651
 
505
652
  /**
@@ -557,74 +704,171 @@ class Logger {
557
704
  }
558
705
  }
559
706
 
560
- // Constants definition
561
707
  // Vite 6/7 uses `vite:build-import-analysis`; Vite 8 Rolldown native path
562
708
  // adds `native:import-analysis-build`. We patch whichever (or both) is present.
709
+ //
710
+ // Why patch at all, rather than use `transformIndexHtml` (order: 'post') or our
711
+ // own `enforce: 'post'` generateBundle? Both of those run BEFORE this plugin
712
+ // substitutes `__VITE_PRELOAD__` in entry chunks, so any hash taken there
713
+ // describes bytes that never ship. Measured on Vite 8.2.2: at both of those
714
+ // points the entry chunk still ends `import("./about-*.js"),__VITE_PRELOAD__)`,
715
+ // while the written file ends `import("./about-*.js"),[])`. Wrapping this
716
+ // plugin's own handler is the only hook position after that substitution.
717
+ // `test/sri.test.js > injects integrity matching the actual emitted bytes`
718
+ // fails if this is ever "simplified" away.
563
719
  const VITE_INTERNAL_ANALYSIS_PLUGINS = [
564
720
  'vite:build-import-analysis',
565
721
  'native:import-analysis-build'
566
722
  ];
567
723
  const DEFAULT_HASH_ALGORITHM = 'sha384';
568
724
  const DEFAULT_PLUGIN_NAME = 'vite-plugin-sri4';
725
+ const MANIFEST_FILE_NAME = 'sri-manifest.json';
726
+ const HTML_RE = /\.html?$/;
727
+ const JS_MODULE_RE = /\.m?js$/;
728
+
729
+ function toText(source) {
730
+ return typeof source === 'string' ? source : Buffer.from(source).toString('utf8')
731
+ }
732
+
733
+ function withTrailingSlash(base) {
734
+ if (!base) return '/'
735
+ return base.endsWith('/') ? base : `${base}/`
736
+ }
737
+
738
+ /**
739
+ * Hash every non-HTML output, keyed by bundle file name.
740
+ */
741
+ function hashBundle(bundle, hashAlgorithm) {
742
+ const hashes = {};
743
+ for (const [fileName, item] of Object.entries(bundle)) {
744
+ if (HTML_RE.test(fileName)) continue
745
+ const source = bundleSource(item);
746
+ if (source) hashes[fileName] = sriHash(source, hashAlgorithm);
747
+ }
748
+ return hashes
749
+ }
569
750
 
570
751
  function sri(options = {}) {
571
752
  const {
572
753
  ignoreMissingAsset = false,
573
754
  bypassDomains = [],
574
755
  hashAlgorithm = DEFAULT_HASH_ALGORITHM,
575
- logLevel = 'warn'
756
+ logLevel = 'warn',
757
+ manifest = false,
758
+ importmap = false
576
759
  } = options;
577
760
 
578
761
  // Create cache manager and logger instances for this plugin instance
579
762
  const cacheManager = new CacheManager();
580
763
  const logger = new Logger(logLevel, DEFAULT_PLUGIN_NAME);
581
764
 
765
+ // bundle fileName -> the integrity we injected, re-checked in writeBundle
766
+ const hashedAssets = new Map();
767
+
582
768
  return {
583
769
  name: DEFAULT_PLUGIN_NAME,
584
770
  enforce: 'post',
585
771
  apply: 'build',
586
772
 
587
- // Cleanup work
588
- buildEnd() {
589
- // Clear caches
773
+ // Every generateBundle hook has run by now. A plugin that rewrites chunk
774
+ // contents after ours (plugin-legacy, in-place compression) would leave the
775
+ // injected hashes describing bytes that no longer ship - a green build that
776
+ // only fails in the browser. Fail here instead.
777
+ writeBundle(_, bundle) {
778
+ const drifted = [];
779
+ for (const [fileName, integrity] of hashedAssets) {
780
+ const item = bundle[fileName];
781
+ if (!item) continue
782
+ const source = bundleSource(item);
783
+ if (source && sriHash(source, hashAlgorithm) !== integrity) {
784
+ drifted.push(fileName);
785
+ }
786
+ }
787
+ hashedAssets.clear();
788
+
789
+ if (drifted.length > 0) {
790
+ throw new Error(
791
+ `[${DEFAULT_PLUGIN_NAME}] content changed after integrity was computed: ` +
792
+ `${drifted.join(', ')}. A plugin running after this one rewrote these ` +
793
+ `files, so the injected hashes no longer match what ships. Move that ` +
794
+ `plugin before ${DEFAULT_PLUGIN_NAME}, or drop it.`
795
+ )
796
+ }
797
+ },
798
+
799
+ // Cleanup. Note this must not be `buildEnd`, which Rollup runs before the
800
+ // output phase - clearing there would empty the caches before use.
801
+ closeBundle() {
590
802
  cacheManager.clearAll();
803
+ hashedAssets.clear();
591
804
  },
592
805
 
593
806
  configResolved(config) {
594
807
  const transformer = createTransformer({
595
808
  ignoreMissingAsset,
596
809
  bypassDomains,
597
- hashAlgorithm
810
+ hashAlgorithm,
811
+ hashedAssets
598
812
  }, config, cacheManager, logger);
599
813
 
814
+ // Both analysis plugin names can be present at once, and each is wrapped.
815
+ // Without this guard the body runs twice per bundle: `emitFile` throws on
816
+ // the duplicate manifest fileName, and the import map warns about the one
817
+ // it just injected. If the skipped pass was the one that finalised chunk
818
+ // contents, the writeBundle drift check fails the build loudly.
819
+ const handled = new WeakSet();
820
+
600
821
  const generateBundle = async function(_, bundle) {
822
+ if (handled.has(bundle)) return
823
+ handled.add(bundle);
824
+
825
+ // Computed before emitting anything so the manifest never hashes itself
826
+ const hashes = manifest || importmap ? hashBundle(bundle, hashAlgorithm) : null;
827
+
601
828
  const htmlFiles = Object.entries(bundle).filter(
602
829
  ([, chunk]) =>
603
830
  chunk.type === 'asset' &&
604
- /\.html?$/.test(chunk.fileName)
831
+ HTML_RE.test(chunk.fileName)
605
832
  );
606
833
 
607
834
  if (htmlFiles.length === 0) {
835
+ // Normal for SSR / library builds, which render HTML at request time
836
+ // - the manifest below is how those builds get their hashes.
608
837
  logger.debug('No HTML files found in bundle');
609
- return
610
838
  }
611
839
 
612
- // Process all HTML files in parallel
840
+ // Errors are intentionally not caught: a resource that cannot be
841
+ // hashed must fail the build rather than ship without integrity.
613
842
  await Promise.all(
614
843
  htmlFiles.map(async ([name, chunk]) => {
615
- try {
616
- const originalContent = chunk.source.toString();
617
- chunk.source = await transformer.transformHTML(bundle, name, originalContent);
618
-
619
- if (originalContent !== chunk.source) {
620
- logger.debug(`SRI attributes added to ${name}`);
844
+ const originalContent = toText(chunk.source);
845
+ let html = await transformer.transformHTML(bundle, name, originalContent);
846
+
847
+ if (importmap) {
848
+ const moduleIntegrity = {};
849
+ const base = withTrailingSlash(config.base);
850
+ for (const [fileName, integrity] of Object.entries(hashes)) {
851
+ if (JS_MODULE_RE.test(fileName)) moduleIntegrity[base + fileName] = integrity;
621
852
  }
622
- } catch (error) {
623
- logger.warn(`Error processing ${name}:`, error);
624
- // Keep original content on error
853
+ html = injectImportmapIntegrity(html, moduleIntegrity, logger);
854
+ }
855
+
856
+ chunk.source = html;
857
+
858
+ if (originalContent !== chunk.source) {
859
+ logger.debug(`SRI attributes added to ${name}`);
625
860
  }
626
861
  })
627
862
  );
863
+
864
+ if (manifest) {
865
+ this.emitFile({
866
+ type: 'asset',
867
+ fileName: MANIFEST_FILE_NAME,
868
+ source: JSON.stringify(hashes, null, 2)
869
+ });
870
+ logger.debug(`Emitted ${MANIFEST_FILE_NAME} with ${Object.keys(hashes).length} entries`);
871
+ }
628
872
  };
629
873
 
630
874
  const targets = config.plugins.filter(