@nwutils/getter 0.2.8 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -4
- package/src/decompress.js +0 -8
- package/src/main.js +26 -3
- package/src/verify.js +69 -28
- package/types/verify.d.ts +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nwutils/getter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Get NW.js and related binaries for Linux, MacOS and Windows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"NW.js",
|
|
@@ -50,8 +50,16 @@
|
|
|
50
50
|
"tar": "^7.5.22",
|
|
51
51
|
"yauzl-promise": "^4.0.0"
|
|
52
52
|
},
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
53
|
+
"devEngines": {
|
|
54
|
+
"runtime": {
|
|
55
|
+
"name": "node",
|
|
56
|
+
"version": "26.7.0",
|
|
57
|
+
"onFail": "warn"
|
|
58
|
+
},
|
|
59
|
+
"packageManager": {
|
|
60
|
+
"name": "npm",
|
|
61
|
+
"version": "12.0.2",
|
|
62
|
+
"onFail": "warn"
|
|
63
|
+
}
|
|
56
64
|
}
|
|
57
65
|
}
|
package/src/decompress.js
CHANGED
|
@@ -46,13 +46,6 @@ export default async function decompress(filePath, cacheDir, limits = {}) {
|
|
|
46
46
|
limits.maxTotalExtractedSize ?? MAX_TOTAL_EXTRACTED_SIZE;
|
|
47
47
|
|
|
48
48
|
if (filePath.endsWith(".zip")) {
|
|
49
|
-
/*
|
|
50
|
-
* Every entry unzip() writes is resolved through resolveWithin() first -
|
|
51
|
-
* file entries, symlink names and symlink targets alike. The rule reports
|
|
52
|
-
* the call site rather than the write site, so it cannot see that guard
|
|
53
|
-
* from here.
|
|
54
|
-
*/
|
|
55
|
-
// eslint-disable-next-line node-security/no-zip-slip
|
|
56
49
|
await unzip(filePath, cacheDir, maxEntries, maxTotalExtractedSize);
|
|
57
50
|
} else {
|
|
58
51
|
/*
|
|
@@ -67,7 +60,6 @@ export default async function decompress(filePath, cacheDir, limits = {}) {
|
|
|
67
60
|
* `maxTotalExtractedSize` and skips any entry that would exceed it, so
|
|
68
61
|
* this is bounded even though the linter can't see through the closure.
|
|
69
62
|
*/
|
|
70
|
-
// eslint-disable-next-line node-security/no-zip-slip, secure-coding/no-unlimited-resource-allocation
|
|
71
63
|
await tar.extract({
|
|
72
64
|
file: filePath,
|
|
73
65
|
C: cacheDir,
|
package/src/main.js
CHANGED
|
@@ -250,13 +250,20 @@ async function get(options) {
|
|
|
250
250
|
const checksumHost =
|
|
251
251
|
uri.protocol === "file:" ? options.downloadUrl : "https://dl.nwjs.io";
|
|
252
252
|
|
|
253
|
-
/*
|
|
253
|
+
/*
|
|
254
|
+
* The main NW.js binary's SHASUMS256.txt from `dl.nwjs.io` is always
|
|
255
|
+
* plain-text, regardless of `options.ffmpeg` - that flag only switches
|
|
256
|
+
* the JSON format used for the community ffmpeg release below.
|
|
257
|
+
*/
|
|
254
258
|
await verify(
|
|
255
259
|
`${checksumHost}/v${options.version}/SHASUMS256.txt`,
|
|
256
260
|
`${options.cacheDir}/shasum/${options.version}.txt`,
|
|
257
261
|
options.cacheDir,
|
|
258
|
-
|
|
262
|
+
false,
|
|
259
263
|
options.shaSum,
|
|
264
|
+
options.version,
|
|
265
|
+
options.platform,
|
|
266
|
+
options.arch,
|
|
260
267
|
path.basename(nwFilePath),
|
|
261
268
|
);
|
|
262
269
|
|
|
@@ -294,6 +301,19 @@ async function get(options) {
|
|
|
294
301
|
);
|
|
295
302
|
}
|
|
296
303
|
|
|
304
|
+
const ffmpegChecksumHost = `https://api.github.com/repos/nwjs-ffmpeg-prebuilt/nwjs-ffmpeg-prebuilt/releases/tags/${options.version}`;
|
|
305
|
+
await verify(
|
|
306
|
+
`${ffmpegChecksumHost}/SHASUMS256.txt`,
|
|
307
|
+
`${options.cacheDir}/shasum/${options.version}.txt`,
|
|
308
|
+
options.cacheDir,
|
|
309
|
+
options.ffmpeg,
|
|
310
|
+
options.shaSum,
|
|
311
|
+
options.version,
|
|
312
|
+
options.platform,
|
|
313
|
+
options.arch,
|
|
314
|
+
path.basename(ffmpegFilePath),
|
|
315
|
+
);
|
|
316
|
+
|
|
297
317
|
await decompress(ffmpegFilePath, options.cacheDir);
|
|
298
318
|
|
|
299
319
|
/**
|
|
@@ -378,8 +398,11 @@ async function get(options) {
|
|
|
378
398
|
`${checksumHost}/v${options.version}/SHASUMS256.txt`,
|
|
379
399
|
`${options.cacheDir}/shasum/${options.version}.txt`,
|
|
380
400
|
options.cacheDir,
|
|
381
|
-
|
|
401
|
+
false,
|
|
382
402
|
options.shaSum,
|
|
403
|
+
options.version,
|
|
404
|
+
options.platform,
|
|
405
|
+
options.arch,
|
|
383
406
|
headersFileName(options.version),
|
|
384
407
|
);
|
|
385
408
|
|
package/src/verify.js
CHANGED
|
@@ -8,12 +8,15 @@ import request from "./request.js";
|
|
|
8
8
|
* Verify the SHA256 checksum of downloaded artifacts.
|
|
9
9
|
* @async
|
|
10
10
|
* @function
|
|
11
|
+
* @param {string} version - Version of NW.js.
|
|
12
|
+
* @param {string} platform - Platform of NW.js.
|
|
13
|
+
* @param {string} arch - Architecture of NW.js.
|
|
11
14
|
* @param {string} shaUrl - URL to get the shasum text file from.
|
|
12
15
|
* @param {string} shaOut - File path to shasum text file.
|
|
13
16
|
* @param {string} cacheDir - File path to cache directory.
|
|
14
17
|
* @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
|
|
15
18
|
* @param {boolean} shaSum - Throws error if true, otherwise logs a warning. Applies to both a checksum mismatch and `expectedFile` never being checked at all.
|
|
16
|
-
* @param {string}
|
|
19
|
+
* @param {string} expectedFile - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
|
|
17
20
|
* @throws {Error}
|
|
18
21
|
* @returns {Promise<boolean>} - Returns true if the checksums match.
|
|
19
22
|
*/
|
|
@@ -23,6 +26,9 @@ export default async function verify(
|
|
|
23
26
|
cacheDir,
|
|
24
27
|
ffmpeg,
|
|
25
28
|
shaSum,
|
|
29
|
+
version,
|
|
30
|
+
platform,
|
|
31
|
+
arch,
|
|
26
32
|
expectedFile,
|
|
27
33
|
) {
|
|
28
34
|
const shaOutExists = fs.existsSync(shaOut);
|
|
@@ -31,39 +37,74 @@ export default async function verify(
|
|
|
31
37
|
/* Create directory if does not exist. */
|
|
32
38
|
await fs.promises.mkdir(path.dirname(shaOut), { recursive: true });
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
if (ffmpeg) {
|
|
41
|
+
/* Download SHASUM text file for community ffmpeg binary. */
|
|
42
|
+
await request(shaUrl, shaOut);
|
|
43
|
+
} else {
|
|
44
|
+
/* Download SHASUM text file. */
|
|
45
|
+
await request(shaUrl, shaOut);
|
|
46
|
+
}
|
|
36
47
|
}
|
|
37
48
|
|
|
38
|
-
/*
|
|
39
|
-
const shasum = await fs.promises.readFile(shaOut, { encoding: "utf-8" });
|
|
40
|
-
const shasums = shasum.trim().split("\n");
|
|
49
|
+
/* TODO: refactor and make it cleaner */
|
|
41
50
|
let expectedFileWasChecked = false;
|
|
51
|
+
if (ffmpeg) {
|
|
52
|
+
const shasum = JSON.parse(
|
|
53
|
+
await fs.promises.readFile(shaOut, { encoding: "utf-8" }),
|
|
54
|
+
);
|
|
55
|
+
const assetName = `${version}-${platform}-${arch}.zip`;
|
|
56
|
+
for await (const asset of shasum.assets) {
|
|
57
|
+
if (asset.name === assetName) {
|
|
58
|
+
const storedSha = asset.digest.slice(7).toLowerCase();
|
|
59
|
+
const relativeFilePath = path.resolve(cacheDir, expectedFile);
|
|
60
|
+
const relativefilePathExists = fs.existsSync(relativeFilePath);
|
|
61
|
+
if (relativefilePathExists) {
|
|
62
|
+
expectedFileWasChecked = true;
|
|
42
63
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
64
|
+
const fileBuffer = await fs.promises.readFile(relativeFilePath);
|
|
65
|
+
const hash = crypto.createHash("sha256");
|
|
66
|
+
hash.update(fileBuffer);
|
|
67
|
+
const generatedSha = hash.digest("hex");
|
|
68
|
+
if (
|
|
69
|
+
!crypto.timingSafeEqual(
|
|
70
|
+
Buffer.from(generatedSha, "hex"),
|
|
71
|
+
Buffer.from(storedSha, "hex"),
|
|
72
|
+
)
|
|
73
|
+
) {
|
|
74
|
+
const message = `SHA256 checksums do not match. The file ${expectedFile} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
|
|
75
|
+
if (shaSum) {
|
|
76
|
+
throw new Error(message);
|
|
77
|
+
} else {
|
|
78
|
+
console.log(message);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
50
82
|
}
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
/* Read SHASUM text file */
|
|
86
|
+
const shasum = await fs.promises.readFile(shaOut, { encoding: "utf-8" });
|
|
87
|
+
const shasums = shasum.trim().split("\n");
|
|
88
|
+
|
|
89
|
+
for await (const line of shasums) {
|
|
90
|
+
const [storedSha, filePath] = line.split(/\s+/);
|
|
91
|
+
const relativeFilePath = path.resolve(cacheDir, filePath);
|
|
92
|
+
const relativefilePathExists = fs.existsSync(relativeFilePath);
|
|
93
|
+
if (relativefilePathExists) {
|
|
94
|
+
if (filePath === expectedFile) {
|
|
95
|
+
expectedFileWasChecked = true;
|
|
96
|
+
}
|
|
51
97
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (filePath.includes("ffmpeg") && ffmpeg) {
|
|
63
|
-
console.warn(
|
|
64
|
-
`The generated shasum for the community ffmpeg at ${filePath} is ${generatedSha}. The integrity of this file should be manually verified.`,
|
|
65
|
-
);
|
|
66
|
-
} else {
|
|
98
|
+
const fileBuffer = await fs.promises.readFile(relativeFilePath);
|
|
99
|
+
const hash = crypto.createHash("sha256");
|
|
100
|
+
hash.update(fileBuffer);
|
|
101
|
+
const generatedSha = hash.digest("hex");
|
|
102
|
+
if (
|
|
103
|
+
!crypto.timingSafeEqual(
|
|
104
|
+
Buffer.from(generatedSha, "hex"),
|
|
105
|
+
Buffer.from(storedSha, "hex"),
|
|
106
|
+
)
|
|
107
|
+
) {
|
|
67
108
|
const message = `SHA256 checksums do not match. The file ${filePath} expected shasum is ${storedSha} but the actual shasum is ${generatedSha}.`;
|
|
68
109
|
if (shaSum) {
|
|
69
110
|
throw new Error(message);
|
package/types/verify.d.ts
CHANGED
|
@@ -2,13 +2,16 @@
|
|
|
2
2
|
* Verify the SHA256 checksum of downloaded artifacts.
|
|
3
3
|
* @async
|
|
4
4
|
* @function
|
|
5
|
+
* @param {string} version - Version of NW.js.
|
|
6
|
+
* @param {string} platform - Platform of NW.js.
|
|
7
|
+
* @param {string} arch - Architecture of NW.js.
|
|
5
8
|
* @param {string} shaUrl - URL to get the shasum text file from.
|
|
6
9
|
* @param {string} shaOut - File path to shasum text file.
|
|
7
10
|
* @param {string} cacheDir - File path to cache directory.
|
|
8
11
|
* @param {boolean} ffmpeg - Toggle between community (true) and official (false) ffmpeg binary
|
|
9
12
|
* @param {boolean} shaSum - Throws error if true, otherwise logs a warning. Applies to both a checksum mismatch and `expectedFile` never being checked at all.
|
|
10
|
-
* @param {string}
|
|
13
|
+
* @param {string} expectedFile - Relative path, as listed in the SHASUMS file, that this call is actually relying on being verified. Other listed files that don't exist locally (eg. other platforms) are still skipped silently - only `expectedFile` going unchecked is treated as a failure, since that means the caller's "integrity verified" belief was never actually true.
|
|
11
14
|
* @throws {Error}
|
|
12
15
|
* @returns {Promise<boolean>} - Returns true if the checksums match.
|
|
13
16
|
*/
|
|
14
|
-
export default function verify(shaUrl: string, shaOut: string, cacheDir: string, ffmpeg: boolean, shaSum: boolean, expectedFile
|
|
17
|
+
export default function verify(shaUrl: string, shaOut: string, cacheDir: string, ffmpeg: boolean, shaSum: boolean, version: string, platform: string, arch: string, expectedFile: string): Promise<boolean>;
|