Skip to content

Commit 72107cd

Browse files
committed
feat: use constant inline script + provide CSP hashes
1 parent 782fd12 commit 72107cd

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

packages/plugin-legacy/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ export default {
128128
}
129129
```
130130

131+
## Content Security Policy
132+
133+
The legacy plugin requires inline scripts for [Safari 10.1 `nomodule` fix](https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc) and SystemJS initialization. If you have a strict CSP policy requirement, you will need to [add the corresponding hashes to your `script-src` list](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script):
134+
135+
- `MS6/3FCg4WjP9gwgaBGwLpRCY6fZBgwmhVCdrPrNf3E=`
136+
- `tQjf8gvb2ROOMapIxFvFAYBeUJ0v1HCbOcSmDNXGtDo=`
137+
138+
These values can also be retrived via
139+
140+
```js
141+
const { cspHashes } = require('@vitejs/plugin-legacy')
142+
```
143+
131144
## References
132145

133146
- [Vue CLI modern mode](https://cli.vuejs.org/guide/browser-compatibility.html#modern-mode)

packages/plugin-legacy/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ export interface Options {
2626
declare function createPlugin(options?: Options): Plugin
2727

2828
export default createPlugin
29+
30+
export const cspHashes: string[]

packages/plugin-legacy/index.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
const path = require('path')
3+
const { createHash } = require('crypto')
34
const { build } = require('vite')
45

56
// lazy load babel since it's not used during dev
@@ -10,8 +11,12 @@ let babel
1011
const loadBabel = () => babel || (babel = require('@babel/standalone'))
1112

1213
// https://gist.github.com/samthor/64b114e4a4f539915a95b91ffd340acc
14+
// DO NOT ALTER THIS CONTENT
1315
const safari10NoModuleFix = `!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();`
1416

17+
const legacyEntryId = 'vite-legacy-entry'
18+
const systemJSInlineCode = `System.import(document.getElementById('${legacyEntryId}').getAttribute('data-src'))`
19+
1520
/**
1621
* @param {import('.').Options} options
1722
* @returns {import('vite').Plugin[]}
@@ -293,8 +298,15 @@ function viteLegacyPlugin(options = {}) {
293298
if (legacyEntryFilename) {
294299
tags.push({
295300
tag: 'script',
296-
attrs: { nomodule: true },
297-
children: `System.import("${config.build.base}${legacyEntryFilename}")`,
301+
attrs: {
302+
nomodule: true,
303+
// we set the entry path on the element as an attribute so that the
304+
// script content will stay consistent - which allows using a constant
305+
// hash value for CSP.
306+
id: legacyEntryId,
307+
'data-src': config.build.base + legacyEntryFilename
308+
},
309+
children: systemJSInlineCode,
298310
injectTo: 'body'
299311
})
300312
} else {
@@ -446,4 +458,10 @@ function isLegacyOutput(options) {
446458
}
447459

448460
module.exports = viteLegacyPlugin
461+
449462
viteLegacyPlugin.default = viteLegacyPlugin
463+
464+
viteLegacyPlugin.cpsHashes = [
465+
createHash('sha256').update(safari10NoModuleFix).digest('base64'),
466+
createHash('sha256').update(systemJSInlineCode).digest('base64')
467+
]

0 commit comments

Comments
 (0)