Skip to content

Commit f0a9fd8

Browse files
authored
Merge pull request #186 from ConcealNetwork/fix/#3874
Check SRI
2 parents 1aaa36a + 6d8d8b8 commit f0a9fd8

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

build.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2018 Gnock
33
* Copyright (c) 2018-2019 The Masari Project
44
* Copyright (c) 2018-2020 The Karbo developers
5-
* Copyright (c) 2018-2023 Conceal Community, Conceal.Network & Conceal Devs
5+
* Copyright (c) 2018-2025 Conceal Community, Conceal.Network & Conceal Devs
66
*
77
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
88
*
@@ -16,6 +16,56 @@
1616
*/
1717

1818
const workboxBuild = require('workbox-build');
19+
const fs = require('fs');
20+
const path = require('path');
21+
const crypto = require('crypto');
22+
const dotenv = require('dotenv');
23+
24+
// Load environment variables
25+
dotenv.config();
26+
27+
// Function to generate SHA384 integrity hash for a file
28+
const generateIntegrityHash = (filePath) => {
29+
try {
30+
const fileContent = fs.readFileSync(filePath);
31+
const hash = crypto.createHash('sha384').update(fileContent).digest('base64');
32+
return `sha384-${hash}`;
33+
} catch (error) {
34+
console.error(`Error generating hash for ${filePath}:`, error);
35+
return null;
36+
}
37+
};
38+
39+
// Generate integrity hash for api.html and update the .env file
40+
const updateApiIntegrityHash = () => {
41+
const apiHtmlPath = path.join(__dirname, 'src', 'api.html');
42+
43+
const integrityHash = generateIntegrityHash(apiHtmlPath);
44+
if (!integrityHash) return;
45+
46+
console.log(`Generated new integrity hash for api.html`);
47+
48+
// Store in .env if not exists or has changed
49+
const envPath = path.join(__dirname, '.env');
50+
let envContent = '';
51+
52+
if (fs.existsSync(envPath)) {
53+
envContent = fs.readFileSync(envPath, 'utf8');
54+
}
55+
56+
// Update or add the API_INTEGRITY_HASH in .env
57+
if (!envContent.includes('API_INTEGRITY_HASH=')) {
58+
fs.appendFileSync(envPath, `\nAPI_INTEGRITY_HASH=${integrityHash}\n`);
59+
} else {
60+
const newEnvContent = envContent.replace(
61+
/API_INTEGRITY_HASH=.*/,
62+
`API_INTEGRITY_HASH=${integrityHash}`
63+
);
64+
fs.writeFileSync(envPath, newEnvContent);
65+
}
66+
67+
console.log('Updated .env with new integrity hash');
68+
};
1969

2070
// NOTE: This should be run *AFTER* all your assets are built
2171
const buildSW = () => {
@@ -33,4 +83,6 @@ const buildSW = () => {
3383
});
3484
};
3585

86+
// Update integrity hash before building SW
87+
updateApiIntegrityHash();
3688
buildSW();

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"main": "index.js",
1111
"dependencies": {
1212
"@types/node": "^22.14.0",
13+
"dotenv": "^16.5.0",
1314
"typescript": "^5.8.3",
1415
"workbox-build": "^7.3.0"
1516
},

src_client_api/api.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) 2018 Gnock
33
* Copyright (c) 2018-2019 The Masari Project
44
* Copyright (c) 2018-2020 The Karbo developers
5-
* Copyright (c) 2018-2023 Conceal Community, Conceal.Network & Conceal Devs
5+
* Copyright (c) 2018-2025 Conceal Community, Conceal.Network & Conceal Devs
66
*
77
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
88
*
@@ -15,12 +15,20 @@
1515
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1616
*/
1717

18+
// Load environment variables if available
19+
try {
20+
require('dotenv').config();
21+
} catch (e) {
22+
console.log('dotenv not available, using default values');
23+
}
24+
1825
var ConcealAPI = new function(){
1926

2027
this.ready = false;
2128
this.apiDomain = 'http://localhost:38090';
2229
this.timeoutErrorTime = 10000;
2330
this.timeoutError = 10000;
31+
this.apiIntegrityHash = process.env.API_INTEGRITY_HASH || 'sha384-akR4d4WI0aBIvwvuucK9YnuVVRJrj+riPGFT2l9zCHty3n71nJ60rUqm0rjG67Z4';
2432

2533
var self = this;
2634

@@ -59,6 +67,8 @@ var ConcealAPI = new function(){
5967
self.registerPromise('ready', resolve, reject);
6068
var ifrm = document.createElement("iframe");
6169
ifrm.setAttribute("src", self.apiDomain+"/api.html");
70+
ifrm.setAttribute("integrity", self.apiIntegrityHash);
71+
ifrm.setAttribute("crossorigin", "anonymous");
6272
ifrm.style.width = "0";
6373
ifrm.style.height = "0";
6474
ifrm.style.display = 'none';
@@ -69,6 +79,7 @@ var ConcealAPI = new function(){
6979
},self.timeoutErrorTime);
7080

7181
ifrm.addEventListener('load', function(){
82+
console.log('Iframe loaded successfully - integrity check passed');
7283
clearTimeout(self.timeoutError);
7384
self.timeoutError = 0;
7485
});

0 commit comments

Comments
 (0)