Skip to content

Commit 53c8625

Browse files
committed
Add Legacy Support for Lico
Added Legacy Support for Lico via legacy plugin and added csp headers dynamically in build html. Related: #143
1 parent d829bd9 commit 53c8625

File tree

4 files changed

+1452
-20
lines changed

4 files changed

+1452
-20
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))
3939

4040
// Write index with random nonce to response.
41-
index := bytes.Replace(i.webappIndexHTML, []byte("__CSP_NONCE__"), []byte(nonce), 1)
41+
index := bytes.Replace(i.webappIndexHTML, []byte("__CSP_NONCE__"), []byte(nonce), 5)
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": "^5.3.2",
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: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,41 @@
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+
import { readFileSync, writeFileSync } from "fs";
6+
import path from "path";
7+
8+
function cspPlugin() {
9+
return {
10+
name: "csp-plugin",
11+
closeBundle() {
12+
// Path to the generated HTML file after build
13+
const htmlFilePath = path.resolve(__dirname, "build/index.html");
14+
15+
// Read the HTML file content
16+
let htmlContent = readFileSync(htmlFilePath, "utf-8");
17+
18+
// Insert the CSP meta tag in the HTML <head> section
19+
htmlContent = htmlContent.replaceAll(
20+
/<script nomodule>/gi,
21+
`<script nomodule content="__CSP_NONCE__">`
22+
);
23+
24+
htmlContent = htmlContent.replaceAll(
25+
/<script type="module">/gi,
26+
`<script type="module" content="__CSP_NONCE__">`
27+
);
28+
29+
htmlContent = htmlContent.replaceAll(
30+
/<script nomodule crossorigin id="vite-legacy-entry"/gi,
31+
`<script nomodule crossorigin id="vite-legacy-entry" content="__CSP_NONCE__"`
32+
);
33+
34+
// Write the updated HTML back to the file
35+
writeFileSync(htmlFilePath, htmlContent);
36+
},
37+
};
38+
}
439

540
export default defineConfig((env) => {
641
return {
@@ -23,12 +58,17 @@ export default defineConfig((env) => {
2358
},
2459
plugins: [
2560
react(),
26-
env.mode !== 'test' && checker({
27-
typescript: true,
28-
eslint: {
29-
lintCommand: 'eslint --max-warnings=0 src',
30-
},
61+
legacy({
62+
targets: ['edge 18'],
3163
}),
64+
cspPlugin(),
65+
env.mode !== 'test' &&
66+
checker({
67+
typescript: true,
68+
eslint: {
69+
lintCommand: 'eslint --max-warnings=0 src',
70+
},
71+
}),
3272
splitVendorChunkPlugin(),
3373
],
3474
test: {

0 commit comments

Comments
 (0)