bestzip 4.0.0 → 4.0.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/README.md +2 -1
- package/lib/bestzip.js +142 -8
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -114,4 +114,5 @@ When symlinks are present and the `followSymLinks` option has not been set expli
|
|
|
114
114
|
To follow symlinks, set `followSymLinks: true` (programmatic API) or pass `--follow-sym-links` on the command line. To keep the link entries (the default behavior) while suppressing the warning, set `followSymLinks: false` or pass `--no-follow-sym-links` on the command line.
|
|
115
115
|
|
|
116
116
|
When archiving symlinks without following them, bestzip uses the native `zip` command when available. Some native `zip` builds (notably the Windows build of Info-ZIP) cannot store symlinks as link entries at all, so bestzip falls back to its built-in Node.js implementation in that case.
|
|
117
|
-
|
|
117
|
+
|
|
118
|
+
Note that calling `nativeZip()` directly with `followSymLinks` unset/false on such a platform throws an error; use the `bestZip()` entry point, which routes to the Node.js implementation automatically. Use `nativeZipSupportsSymlinks()` to check whether the available native `zip` can store symlinks as links; it returns `true`/`false` and caches its result after the first call. `bestzip.hasNativeZip()` checks whether a native `zip` is installed at all.
|
package/lib/bestzip.js
CHANGED
|
@@ -6,13 +6,103 @@ import fs from "node:fs";
|
|
|
6
6
|
import fsp from "node:fs/promises";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import os from "node:os";
|
|
9
|
+
import { isDeepStrictEqual } from "node:util";
|
|
9
10
|
|
|
10
11
|
import { ZipArchive } from "archiver";
|
|
11
12
|
import { glob, hasMagic } from "glob";
|
|
12
|
-
|
|
13
|
+
|
|
14
|
+
// Directories where the system `zip` command is expected to live. Only these
|
|
15
|
+
// are ever consulted, so a file named `zip` anywhere else — committed into an
|
|
16
|
+
// archived directory, delivered through a relative/empty PATH element, or
|
|
17
|
+
// dropped into node_modules/.bin by a dependency — is never executed
|
|
18
|
+
// (CWE-426/CWE-427).
|
|
19
|
+
const DEFAULT_ZIP_DIRS = (() => {
|
|
20
|
+
if (process.platform === "win32") {
|
|
21
|
+
// note, some node.js environments, like MINGW64 (git-bash), report SystemRoot
|
|
22
|
+
// and ProgramFiles as uppercase, but they still support the version here
|
|
23
|
+
const systemRoot = process.env.SystemRoot || "C:\\Windows";
|
|
24
|
+
const programFiles = process.env.ProgramFiles || "C:\\Program Files";
|
|
25
|
+
const programFilesX86 =
|
|
26
|
+
process.env["ProgramFiles(x86)"] || "C:\\Program Files (x86)";
|
|
27
|
+
const localAppData = process.env.LOCALAPPDATA;
|
|
28
|
+
const userProfile = process.env.USERPROFILE;
|
|
29
|
+
|
|
30
|
+
const dirs = [
|
|
31
|
+
path.join(systemRoot, "System32"),
|
|
32
|
+
systemRoot,
|
|
33
|
+
"C:\\ProgramData\\chocolatey\\bin",
|
|
34
|
+
path.join(programFiles, "Git\\usr\\bin"),
|
|
35
|
+
path.join(programFilesX86, "Git\\usr\\bin"),
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
if (localAppData) {
|
|
39
|
+
dirs.push(path.join(localAppData, "Microsoft\\WinGet\\Links"));
|
|
40
|
+
}
|
|
41
|
+
if (userProfile) {
|
|
42
|
+
dirs.push(path.join(userProfile, "scoop\\shims"));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return dirs;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return [
|
|
49
|
+
"/usr/bin",
|
|
50
|
+
"/bin",
|
|
51
|
+
"/usr/local/bin",
|
|
52
|
+
"/opt/homebrew/bin", // Homebrew on Apple Silicon
|
|
53
|
+
"/opt/local/bin", // MacPorts
|
|
54
|
+
"/snap/bin", // Linux Snap packages
|
|
55
|
+
"/nix/var/nix/profiles/default/bin", // Nix OS/Package Manager
|
|
56
|
+
];
|
|
57
|
+
})();
|
|
58
|
+
|
|
59
|
+
const ZIP_EXECUTABLE_NAMES =
|
|
60
|
+
process.platform === "win32" ? ["zip.exe", "zip"] : ["zip"];
|
|
61
|
+
|
|
62
|
+
function isExecutableFile(file) {
|
|
63
|
+
try {
|
|
64
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
65
|
+
return fs.statSync(file).isFile();
|
|
66
|
+
} catch (e) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function findInDir(dir, names) {
|
|
72
|
+
for (const name of names) {
|
|
73
|
+
const candidate = path.join(dir, name);
|
|
74
|
+
if (isExecutableFile(candidate)) {
|
|
75
|
+
return candidate;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Resolves the native `zip` binary's absolute path, or null when none is
|
|
82
|
+
// usable (so callers fall back to the node implementation). PATH is never
|
|
83
|
+
// consulted: the child process chdirs into the (possibly untrusted) archive
|
|
84
|
+
// directory before running, so a bare command name could be shadowed by a
|
|
85
|
+
// `zip` planted there or in the dependency tree (CWE-426/CWE-427). Only the
|
|
86
|
+
// allowlisted system directories above are ever checked, and the resolved
|
|
87
|
+
// absolute path — never the bare `zip` name — is what gets spawned.
|
|
88
|
+
let nativeZipCommand;
|
|
89
|
+
function getNativeZipCommand() {
|
|
90
|
+
if (nativeZipCommand !== undefined) {
|
|
91
|
+
return nativeZipCommand;
|
|
92
|
+
}
|
|
93
|
+
nativeZipCommand = null;
|
|
94
|
+
for (const dir of DEFAULT_ZIP_DIRS) {
|
|
95
|
+
const found = findInDir(dir, ZIP_EXECUTABLE_NAMES);
|
|
96
|
+
if (found) {
|
|
97
|
+
nativeZipCommand = found;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return nativeZipCommand;
|
|
102
|
+
}
|
|
13
103
|
|
|
14
104
|
function hasNativeZip() {
|
|
15
|
-
return Boolean(
|
|
105
|
+
return Boolean(getNativeZipCommand());
|
|
16
106
|
}
|
|
17
107
|
|
|
18
108
|
let nativeSymlinkCapability;
|
|
@@ -25,6 +115,11 @@ function nativeZipSupportsSymlinks() {
|
|
|
25
115
|
if (nativeSymlinkCapability !== undefined) {
|
|
26
116
|
return nativeSymlinkCapability;
|
|
27
117
|
}
|
|
118
|
+
const command = getNativeZipCommand();
|
|
119
|
+
if (!command) {
|
|
120
|
+
nativeSymlinkCapability = false;
|
|
121
|
+
return nativeSymlinkCapability;
|
|
122
|
+
}
|
|
28
123
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bestzip-symlink-check"));
|
|
29
124
|
try {
|
|
30
125
|
const file = path.join(dir, "file");
|
|
@@ -37,7 +132,7 @@ function nativeZipSupportsSymlinks() {
|
|
|
37
132
|
return nativeSymlinkCapability;
|
|
38
133
|
}
|
|
39
134
|
const res = spawnSync(
|
|
40
|
-
|
|
135
|
+
command,
|
|
41
136
|
["-q", "--symlinks", "check.zip", "file", "link"],
|
|
42
137
|
{
|
|
43
138
|
cwd: dir,
|
|
@@ -123,6 +218,14 @@ function validateLevel(level) {
|
|
|
123
218
|
}
|
|
124
219
|
}
|
|
125
220
|
|
|
221
|
+
async function safeStat(path) {
|
|
222
|
+
try {
|
|
223
|
+
return await fsp.stat(path);
|
|
224
|
+
} catch (err) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
126
229
|
async function detectSymlinks(cwd, sources) {
|
|
127
230
|
const results = await Promise.all(
|
|
128
231
|
sources.map(async (source) => {
|
|
@@ -161,10 +264,20 @@ function maybeWarnAboutSymlinks(options, symlinks, cwd) {
|
|
|
161
264
|
|
|
162
265
|
const nativeZip = async (options) => {
|
|
163
266
|
const cwd = options.cwd || process.cwd();
|
|
164
|
-
|
|
267
|
+
// Resolve the absolute path in the parent's context and pass that to the
|
|
268
|
+
// child. Resolving the bare `zip` name inside the spawned process would let
|
|
269
|
+
// the archive directory's contents shadow the real binary when PATH has a
|
|
270
|
+
// relative or empty element (CWE-426/CWE-427).
|
|
271
|
+
const command = getNativeZipCommand();
|
|
272
|
+
if (!command) {
|
|
273
|
+
throw new Error(
|
|
274
|
+
"The native zip command was not found at a trusted location on this system."
|
|
275
|
+
);
|
|
276
|
+
}
|
|
165
277
|
const sources = await expandSources(cwd, options.source);
|
|
166
278
|
const destination = path.resolve(cwd, options.destination);
|
|
167
279
|
validateLevel(options.level);
|
|
280
|
+
const destStatBefore = await safeStat(destination);
|
|
168
281
|
// followSymLinks: true opts into following symlinks (archiving their target
|
|
169
282
|
// contents); the default (unset or false) stores symlinks as links.
|
|
170
283
|
const followSymLinks = options.followSymLinks === true;
|
|
@@ -212,15 +325,36 @@ const nativeZip = async (options) => {
|
|
|
212
325
|
cwd,
|
|
213
326
|
});
|
|
214
327
|
zipProcess.on("error", reject);
|
|
215
|
-
zipProcess.on("close", (exitCode) => {
|
|
328
|
+
zipProcess.on("close", async (exitCode) => {
|
|
216
329
|
if (exitCode === 0) {
|
|
217
|
-
|
|
330
|
+
// The native zip reported success. Make sure it actually wrote the
|
|
331
|
+
// archive — the zip exits 0 without creating or updating the
|
|
332
|
+
// destination, that's a silent no-op, not a real archive.
|
|
333
|
+
const destStatAfter = await safeStat(destination);
|
|
334
|
+
if (
|
|
335
|
+
destStatAfter &&
|
|
336
|
+
!isDeepStrictEqual(destStatBefore, destStatAfter)
|
|
337
|
+
) {
|
|
338
|
+
resolve();
|
|
339
|
+
} else {
|
|
340
|
+
reject(
|
|
341
|
+
new Error(
|
|
342
|
+
`The native zip command exited 0 but did not ${
|
|
343
|
+
destStatBefore ? "update" : "create"
|
|
344
|
+
} the destination archive '${destination}'. This may indicate a broken or incompatible native zip command, or a filesystem issue. Consider using ${
|
|
345
|
+
options.viaCli ? "--force=node" : `{force: 'node'}`
|
|
346
|
+
}.\n Executed command: '${command} ${args.join(
|
|
347
|
+
" "
|
|
348
|
+
)}'\n Executed in directory: '${cwd}'`
|
|
349
|
+
)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
218
352
|
} else {
|
|
219
353
|
reject(
|
|
220
354
|
new Error(
|
|
221
|
-
`Unexpected exit code from native zip: ${exitCode}\n
|
|
355
|
+
`Unexpected exit code from native zip: ${exitCode}\n Executed command: '${command} ${args.join(
|
|
222
356
|
" "
|
|
223
|
-
)}'\n
|
|
357
|
+
)}'\n Executed in directory '${cwd}'`
|
|
224
358
|
)
|
|
225
359
|
);
|
|
226
360
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bestzip",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Uses OS zip command if available (for better performance and speed) or node.js version if there is no system command available. Can be called via node or command line.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./lib/bestzip.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"lint": "eslint .",
|
|
15
15
|
"lint-fix": "eslint --fix .",
|
|
16
|
-
"unit-test": "node --experimental-test-snapshots --test",
|
|
16
|
+
"unit-test": "node --experimental-test-snapshots --test \"test/**/*.test.js\"",
|
|
17
17
|
"update-snapshot": "npm run unit-test -- --test-update-snapshots",
|
|
18
18
|
"test": "npm run lint && npm run unit-test"
|
|
19
19
|
},
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"archiver": "^8.0.0",
|
|
33
33
|
"glob": "^13.0.6",
|
|
34
|
-
"which": "^6.0.1",
|
|
35
34
|
"yargs": "^16.2.0"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|