Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 4aeb5a1

Browse files
Merge pull request #513 from deckgo/postbuild-update-csp
feat(#497): prerender post build update for CSP
2 parents f333959 + a3fe160 commit 4aeb5a1

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

studio/scripts/config.index.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
11
#!/usr/bin/env node
22

33
const fs = require('fs');
4+
const path = require('path');
5+
6+
const crypto = require('crypto');
47

58
const configProd = require('../config.prod');
69
const configDev = require('../config.dev');
710

811
const dev = process.argv && process.argv.indexOf('--dev') > -1;
912

10-
// https://stackoverflow.com/a/14181136/5404186
11-
function updateIndexHml(filename) {
12-
fs.readFile(`./www/${filename}`, 'utf8', function (err, data) {
13+
function updateCSP(filename) {
14+
fs.readFile(`${filename}`, 'utf8', function (err, data) {
1315
if (err) {
1416
return console.log(err);
1517
}
1618

17-
const result = data.replace(/<@API_URL@>/g, dev ? configDev.API_URL : configProd.API_URL);
19+
// 1. Replace API Url
20+
let result = data.replace(/<@API_URL@>/g, dev ? configDev.API_URL : configProd.API_URL);
21+
22+
// 2. Update service worker loader hash
23+
const swHash = findSWHash(data);
24+
if (swHash) {
25+
result = result.replace(/<@SW_LOADER@>/g, swHash);
26+
}
27+
28+
// 3. Update CSS link until https://github.com/ionic-team/stencil/issues/2039 solved
29+
result = result.replace(/rel=stylesheet media="\(max-width: 0px\)" importance=low onload="this\.media=''"/g, 'rel=stylesheet importance=low');
1830

19-
fs.writeFile(`./www/${filename}`, result, 'utf8', function (err) {
31+
fs.writeFile(`${filename}`, result, 'utf8', function (err) {
2032
if (err) return console.log(err);
2133
});
2234
});
2335
}
2436

25-
updateIndexHml('index.html');
37+
function findSWHash(data) {
38+
const sw = /(<.?script data-build.*?>)([\s\S]*?)(<\/script>)/gm;
39+
40+
let m;
41+
while (m = sw.exec(data)) {
42+
if (m && m.length >= 3 && m[2].indexOf('serviceWorker') > -1) {
43+
return `'sha256-${crypto.createHash('sha256').update(m[2]).digest('base64')}'`;
44+
}
45+
}
46+
47+
return undefined;
48+
}
49+
50+
function findHTMLFiles(dir, files) {
51+
fs.readdirSync(dir).forEach(file => {
52+
const fullPath = path.join(dir, file);
53+
if (fs.lstatSync(fullPath).isDirectory()) {
54+
findHTMLFiles(fullPath, files);
55+
} else if (path.extname(fullPath) === '.html') {
56+
files.push(fullPath);
57+
}
58+
});
59+
}
60+
61+
let htmlFiles = [];
62+
findHTMLFiles('./www/', htmlFiles);
2663

27-
if (!dev) {
28-
updateIndexHml('index-org.html');
64+
for (const file of htmlFiles) {
65+
updateCSP(`./${file}`);
2966
}

studio/src/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
img-src 'self' data: https://deckdeckgo.com https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-prod.appspot.com/ https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-beta.appspot.com/ https://www.gstatic.com https://lh5.googleusercontent.com https://pbs.twimg.com https://media.giphy.com https://media.tenor.com/ https://images.unsplash.com/ https://*.githubusercontent.com/ https://*.googleusercontent.com/;
1010
style-src 'self' 'unsafe-inline' https://cdn.firebase.com https://fonts.googleapis.com;
1111
font-src 'self' https://fonts.googleapis.com https://fonts.gstatic.com;
12-
script-src 'self' blob: 'sha256-vay/aAFxtYsaISRoBsVDHCbAzow9u6P2gHHTewRPaJY=' https://cdn.firebase.com https://apis.google.com https://unpkg.com/prismjs@latest/;
13-
connect-src 'self' <@API_URL@> https://deckdeckgo.com/ wss://api.deckdeckgo.com/ https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-prod.appspot.com/ https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-beta.appspot.com/ https://www.googleapis.com https://securetoken.googleapis.com https://firestore.googleapis.com ws://localhost:3333/ https://raw.githubusercontent.com/PrismJS https://raw.githubusercontent.com/deckgo/ https://api.tenor.com/;
12+
script-src 'self' blob: <@SW_LOADER@> 'sha256-vay/aAFxtYsaISRoBsVDHCbAzow9u6P2gHHTewRPaJY=' https://cdn.firebase.com https://apis.google.com https://unpkg.com/prismjs@latest/;
13+
connect-src 'self' <@API_URL@> https://deckdeckgo.com/ wss://api.deckdeckgo.com/ https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-prod.appspot.com/ https://firebasestorage.googleapis.com/v0/b/deckdeckgo-studio-beta.appspot.com/ https://www.googleapis.com https://securetoken.googleapis.com https://firestore.googleapis.com ws://localhost:3333/ https://raw.githubusercontent.com/PrismJS/ https://raw.githubusercontent.com/deckgo/ https://api.tenor.com/;
1414
frame-src https://deckdeckgo.com https://*.deckdeckgo.com https://deckdeckgo-studio-beta.firebaseapp.com http://localhost:3333/~dev-server https://www.youtube.com/">
1515

1616
<meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

0 commit comments

Comments
 (0)