Skip to content

Commit 28a3be8

Browse files
committed
Add legacy support via plugin
Add legacy support via plugin and add nonce headers to inline scripts. Related: #143
1 parent cca3e4e commit 28a3be8

File tree

4 files changed

+1403
-18
lines changed

4 files changed

+1403
-18
lines changed

identifier/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func (i *Identifier) writeWebappIndexHTML(rw http.ResponseWriter, req *http.Requ
3535
// FIXME(longsleep): Set a secure CSP. Right now we need `data:` for images
3636
// since it is used. Since `data:` URLs possibly could allow xss, a better
3737
// way should be found for our early loading inline SVG stuff.
38-
rw.Header().Set("Content-Security-Policy", fmt.Sprintf("default-src 'self'; img-src 'self' data:; font-src 'self' data:; script-src 'self'; style-src 'self' 'nonce-%s'; base-uri 'none'; frame-ancestors 'none';", nonce))
38+
rw.Header().Set("Content-Security-Policy", fmt.Sprintf("default-src 'self'; img-src 'self' data:; font-src 'self' data:; script-src 'self' 'nonce-%s'; style-src 'self' 'nonce-%s'; base-uri 'none'; frame-ancestors 'none';", nonce, nonce))
3939

4040
// Write index with random nonce to response.
41-
index := bytes.Replace(i.webappIndexHTML, []byte("__CSP_NONCE__"), []byte(nonce), 1)
41+
index := bytes.ReplaceAll(i.webappIndexHTML, []byte("__CSP_NONCE__"), []byte(nonce))
4242
rw.Write(index)
4343
}
4444

identifier/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@typescript-eslint/eslint-plugin": "^6.11.0",
5151
"@typescript-eslint/parser": "^6.9.0",
5252
"@typescript-eslint/typescript-estree": "^6.11.0",
53+
"@vitejs/plugin-legacy": "^4.0.0",
5354
"@vitejs/plugin-react": "^4.1.1",
5455
"cldr": "^7.4.0",
5556
"eslint": "^8.53.0",
@@ -60,6 +61,7 @@
6061
"if-node-version": "^1.1.1",
6162
"jsdom": "^22.1.0",
6263
"source-map-explorer": "^2.5.3",
64+
"terser": "^5.30.4",
6365
"typescript": "^5.2.2",
6466
"vite": "^4.5.2",
6567
"vite-plugin-checker": "^0.6.2",

identifier/vite.config.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
1-
import { defineConfig, splitVendorChunkPlugin } from 'vite';
2-
import react from '@vitejs/plugin-react';
3-
import checker from 'vite-plugin-checker';
1+
import { defineConfig, splitVendorChunkPlugin } from "vite";
2+
import react from "@vitejs/plugin-react";
3+
import checker from "vite-plugin-checker";
4+
import legacy from "@vitejs/plugin-legacy";
5+
6+
7+
const addScriptCSPNoncePlaceholderPlugin = () => {
8+
return {
9+
name: "add-script-nonce-placeholderP-plugin",
10+
apply: "build",
11+
transformIndexHtml: {
12+
order: "post",
13+
handler(htmlData) {
14+
15+
return htmlData.replaceAll(
16+
/<script nomodule>/gi,
17+
`<script nomodule nonce="__CSP_NONCE__">`
18+
).replaceAll(
19+
/<script type="module">/gi,
20+
`<script type="module" nonce="__CSP_NONCE__">`
21+
).replaceAll(
22+
/<script nomodule crossorigin id="vite-legacy-entry"/gi,
23+
`<script nomodule crossorigin id="vite-legacy-entry" nonce="__CSP_NONCE__"`
24+
);
25+
},
26+
},
27+
};
28+
};
429

530
export default defineConfig((env) => {
631
return {
@@ -23,13 +48,18 @@ export default defineConfig((env) => {
2348
},
2449
plugins: [
2550
react(),
26-
env.mode !== 'test' && checker({
27-
typescript: true,
28-
eslint: {
29-
lintCommand: 'eslint --max-warnings=0 src',
30-
},
51+
legacy({
52+
targets: ['edge 18'],
3153
}),
54+
env.mode !== "test" &&
55+
checker({
56+
typescript: true,
57+
eslint: {
58+
lintCommand: 'eslint --max-warnings=0 src',
59+
},
60+
}),
3261
splitVendorChunkPlugin(),
62+
addScriptCSPNoncePlaceholderPlugin(),
3363
],
3464
test: {
3565
globals: true,

0 commit comments

Comments
 (0)