toolcrib 0.9.0 → 0.11.0

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/src/lib/zip.js CHANGED
@@ -29,7 +29,68 @@ export function powerShellQuote(value) {
29
29
  return `'${value.replace(/'/g, "''")}'`;
30
30
  }
31
31
 
32
+ /**
33
+ * Read every entry name out of a zip's central directory, in plain Node.
34
+ * Throws if the buffer isn't a readable zip. Zip64 archives (the 0xFFFF /
35
+ * 0xFFFFFFFF sentinels) are rejected rather than guessed at: a toolcrib
36
+ * release is a few hundred small files.
37
+ */
38
+ export function listZipEntryNames(zipBuffer) {
39
+ const EOCD_SIG = 0x06054b50;
40
+ const CEN_SIG = 0x02014b50;
41
+ // The end-of-central-directory record is 22 bytes plus an optional comment
42
+ // of up to 65535 bytes, so it starts somewhere in the last ~64 KiB.
43
+ const searchFrom = Math.max(0, zipBuffer.length - 22 - 0xffff);
44
+ let eocd = -1;
45
+ for (let i = zipBuffer.length - 22; i >= searchFrom; i--) {
46
+ if (zipBuffer.readUInt32LE(i) === EOCD_SIG) {
47
+ eocd = i;
48
+ break;
49
+ }
50
+ }
51
+ if (eocd < 0) throw new Error('Not a readable zip archive (no end-of-central-directory record).');
52
+
53
+ const entryCount = zipBuffer.readUInt16LE(eocd + 10);
54
+ const centralOffset = zipBuffer.readUInt32LE(eocd + 16);
55
+ if (entryCount === 0xffff || centralOffset === 0xffffffff) {
56
+ throw new Error('Zip64 archives are not supported.');
57
+ }
58
+
59
+ const names = [];
60
+ let pos = centralOffset;
61
+ for (let n = 0; n < entryCount; n++) {
62
+ if (pos + 46 > zipBuffer.length || zipBuffer.readUInt32LE(pos) !== CEN_SIG) {
63
+ throw new Error('Corrupt zip central directory.');
64
+ }
65
+ const nameLen = zipBuffer.readUInt16LE(pos + 28);
66
+ const extraLen = zipBuffer.readUInt16LE(pos + 30);
67
+ const commentLen = zipBuffer.readUInt16LE(pos + 32);
68
+ names.push(zipBuffer.toString('utf8', pos + 46, pos + 46 + nameLen));
69
+ pos += 46 + nameLen + extraLen + commentLen;
70
+ }
71
+ return names;
72
+ }
73
+
74
+ /**
75
+ * Refuse an archive with an entry that would extract outside the target
76
+ * directory (zip-slip): an absolute path, a drive letter, or a `..` segment,
77
+ * with either separator (a backslash is a path separator to Windows'
78
+ * Expand-Archive but a filename character to unzip, so both are checked).
79
+ * Done before extraction rather than trusting the platform tool, because
80
+ * Windows PowerShell 5.1's Expand-Archive has historically been weaker here
81
+ * than modern unzip (#831).
82
+ */
83
+ export function assertSafeZipEntries(zipBuffer) {
84
+ for (const name of listZipEntryNames(zipBuffer)) {
85
+ const segments = name.split(/[\\/]/);
86
+ if (name.startsWith('/') || name.startsWith('\\') || /^[A-Za-z]:/.test(name) || name.includes('\0') || segments.includes('..')) {
87
+ throw new Error(`Refusing to extract an archive with an unsafe entry path: ${name}`);
88
+ }
89
+ }
90
+ }
91
+
32
92
  export async function extractZip(zipBuffer, targetDir) {
93
+ assertSafeZipEntries(zipBuffer);
33
94
  await fsp.mkdir(targetDir, { recursive: true });
34
95
 
35
96
  // A bare `Date.now()`-keyed filename collides under real concurrent use: