-
Notifications
You must be signed in to change notification settings - Fork 7
/
csp-patch.js
76 lines (65 loc) · 2.74 KB
/
csp-patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require('fs')
const path = require('path')
const shared = require('./shared');
const config = shared.readConfig();
function updatePreloadBundles(rootFolder) {
return new Promise((resolve) => {
(async function() {
if (config.csp.patch_preload_bundles) {
// Check if the preload bundles exist
const updateBundle = async function (bundleName) {
if (fs.existsSync(bundleName)) {
console.log("↪️ Updating " + path.basename(bundleName));
const tempFolder = await shared.unzipProject(bundleName, 'bundle-temp');
await addCspMetadata(tempFolder);
await shared.zipProject(tempFolder, bundleName);
}
}
await updateBundle(rootFolder + '/preload-android.zip');
await updateBundle(rootFolder + '/preload-ios.zip');
}
resolve(rootFolder);
})();
});
}
function addCspMetadata(projectLocation) {
return new Promise((resolve, reject) => {
console.log("✔️ Adding CSP");
var indexLocation = path.resolve(projectLocation, 'index.html');
var indexContents = fs.readFileSync(indexLocation, 'utf-8');
var headStart = indexContents.indexOf("<head>");
if (headStart < 0) {
reject(new Error('Could not find head tag in index.html', indexLocation, indexContents));
} else {
var cspMetadata = getCspMetadataTag();
var indexWithCsp = indexContents.replace("<head>", "<head>\n\t"+cspMetadata);
fs.writeFileSync(indexLocation, indexWithCsp);
resolve(path.dirname(indexLocation));
}
});
}
function getCspMetadataTag() {
var tag = "<meta http-equiv=\"Content-Security-Policy\" content=\"{0}\" />"
var content = "";
for (var key in config.csp) {
if (key !== 'patch_preload_bundles') {
content += key;
for (var i in config.csp[key]) {
var value = config.csp[key][i];
content += " " + value
}
content += "; "
}
}
return tag.replace("{0}", content);
}
// Unzip the build and change the index.html CSP
// If there are preload bundles, then the index.html in the
// bundles will need to be modified too
shared.downloadProject(config, "temp/downloads")
.then((zipLocation) => shared.unzipProject(zipLocation, "contents/"))
.then(addCspMetadata)
.then(updatePreloadBundles)
.then((rootFolder) => shared.zipProject(rootFolder, 'temp/out/'+config.playcanvas.name+'_WithCSP.zip'))
.then(outputZip => console.log("Success", outputZip))
.catch(err => console.log("Error", err));