toolcrib 0.11.1 → 0.12.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
@@ -36,6 +36,18 @@ export function powerShellQuote(value) {
36
36
  * release is a few hundred small files.
37
37
  */
38
38
  export function listZipEntryNames(zipBuffer) {
39
+ return readZipCentralDirectory(zipBuffer).map((e) => e.name);
40
+ }
41
+
42
+ /**
43
+ * Read every central-directory entry: its name, and whether its Unix mode
44
+ * (the external attributes' high 16 bits) says it is a symlink. The "version
45
+ * made by" host isn't consulted: Info-ZIP's unzip applies those mode bits for
46
+ * several hosts besides 3/Unix, so a host check would let an attacker pick
47
+ * another one. DOS/Windows-made zips leave the bits at 0, so nothing real
48
+ * is rejected.
49
+ */
50
+ function readZipCentralDirectory(zipBuffer) {
39
51
  const EOCD_SIG = 0x06054b50;
40
52
  const CEN_SIG = 0x02014b50;
41
53
  // The end-of-central-directory record is 22 bytes plus an optional comment
@@ -56,7 +68,7 @@ export function listZipEntryNames(zipBuffer) {
56
68
  throw new Error('Zip64 archives are not supported.');
57
69
  }
58
70
 
59
- const names = [];
71
+ const entries = [];
60
72
  let pos = centralOffset;
61
73
  for (let n = 0; n < entryCount; n++) {
62
74
  if (pos + 46 > zipBuffer.length || zipBuffer.readUInt32LE(pos) !== CEN_SIG) {
@@ -65,10 +77,14 @@ export function listZipEntryNames(zipBuffer) {
65
77
  const nameLen = zipBuffer.readUInt16LE(pos + 28);
66
78
  const extraLen = zipBuffer.readUInt16LE(pos + 30);
67
79
  const commentLen = zipBuffer.readUInt16LE(pos + 32);
68
- names.push(zipBuffer.toString('utf8', pos + 46, pos + 46 + nameLen));
80
+ const externalAttrs = zipBuffer.readUInt32LE(pos + 38);
81
+ entries.push({
82
+ name: zipBuffer.toString('utf8', pos + 46, pos + 46 + nameLen),
83
+ isSymlink: ((externalAttrs >>> 16) & 0o170000) === 0o120000,
84
+ });
69
85
  pos += 46 + nameLen + extraLen + commentLen;
70
86
  }
71
- return names;
87
+ return entries;
72
88
  }
73
89
 
74
90
  /**
@@ -81,7 +97,12 @@ export function listZipEntryNames(zipBuffer) {
81
97
  * than modern unzip (#831).
82
98
  */
83
99
  export function assertSafeZipEntries(zipBuffer) {
84
- for (const name of listZipEntryNames(zipBuffer)) {
100
+ for (const { name, isSymlink } of readZipCentralDirectory(zipBuffer)) {
101
+ // A symlink entry is recreated by unzip on Linux/macOS and then followed
102
+ // by every later read; a toolcrib release never contains one (#1031).
103
+ if (isSymlink) {
104
+ throw new Error(`Refusing to extract an archive with a symlink entry: ${name}`);
105
+ }
85
106
  const segments = name.split(/[\\/]/);
86
107
  if (name.startsWith('/') || name.startsWith('\\') || /^[A-Za-z]:/.test(name) || name.includes('\0') || segments.includes('..')) {
87
108
  throw new Error(`Refusing to extract an archive with an unsafe entry path: ${name}`);
@@ -157,7 +178,9 @@ export function listFilesRecursive(dir, base = dir) {
157
178
  let results = [];
158
179
  for (const entry of entries) {
159
180
  const full = path.join(dir, entry.name);
160
- if (entry.isDirectory()) {
181
+ if (entry.isSymbolicLink()) {
182
+ continue; // defence in depth: never follow or list a link (#1031)
183
+ } else if (entry.isDirectory()) {
161
184
  results = results.concat(listFilesRecursive(full, base));
162
185
  } else {
163
186
  results.push(path.relative(base, full).split(path.sep).join('/'));