File: fix-yarnpkg-core.patch

package info (click to toggle)
node-yarnpkg 4.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,752 kB
  • sloc: javascript: 38,953; ansic: 26,035; cpp: 7,247; sh: 2,829; makefile: 724; perl: 493
file content (200 lines) | stat: -rw-r--r-- 7,594 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Patch file for @yarnpkg/core
1. Renamed import isCI to isCIImport to fix naming conflict
   with class field isCI (terser couldn't differentiate
   between them)
2. Moved some contents of tgzUtils.ts to tgzUtilsExt.ts to
   decouple the bundle output and the tgzUtils.ts file
--- a/packages/yarnpkg-core/sources/Configuration.ts
+++ b/packages/yarnpkg-core/sources/Configuration.ts
@@ -2,7 +2,7 @@
 import {Filename, PortablePath, npath, ppath, xfs}                                                               from '@yarnpkg/fslib';
 import {parseSyml, stringifySyml}                                                                                from '@yarnpkg/parsers';
 import camelcase                                                                                                 from 'camelcase';
-import {isCI, isPR, GITHUB_ACTIONS}                                                                              from 'ci-info';
+import {isCI as isCIImport, isPR, GITHUB_ACTIONS}                                                                from 'ci-info';
 import {UsageError}                                                                                              from 'clipanion';
 import {parse as parseDotEnv}                                                                                    from 'dotenv';
 import {builtinModules}                                                                                          from 'module';
@@ -288,7 +288,7 @@
   enableInlineBuilds: {
     description: `If true, the CLI will print the build output on the command line`,
     type: SettingsType.BOOLEAN,
-    default: isCI,
+    default: isCIImport,
     defaultText: `<dynamic>`,
   },
   enableMessageNames: {
@@ -299,7 +299,7 @@
   enableProgressBars: {
     description: `If true, the CLI is allowed to show a progress bar for long-running events`,
     type: SettingsType.BOOLEAN,
-    default: !isCI,
+    default: !isCIImport,
     defaultText: `<dynamic>`,
   },
   enableTimers: {
@@ -310,7 +310,7 @@
   enableTips: {
     description: `If true, installs will print a helpful message every day of the week`,
     type: SettingsType.BOOLEAN,
-    default: !isCI,
+    default: !isCIImport,
     defaultText: `<dynamic>`,
   },
   preferInteractive: {
@@ -1062,7 +1062,7 @@
 
   public static telemetry: TelemetryManager | null = null;
 
-  public isCI = isCI;
+  public isCI = isCIImport;
 
   public startingCwd: PortablePath;
   public projectCwd: PortablePath | null = null;
--- /dev/null
+++ b/packages/yarnpkg-core/sources/tgzUtilsExt.ts
@@ -0,0 +1,123 @@
+import {FakeFS, PortablePath, NodeFS, ppath, xfs, npath, constants, statUtils} from '@yarnpkg/fslib';
+import {ZipCompression, ZipFS}                                      from '@yarnpkg/libzip';
+import {PassThrough, Readable}                                      from 'stream';
+import tar                                                          from 'tar';
+import * as miscUtils                                               from './miscUtils';
+
+export interface ExtractBufferOptions {
+  prefixPath?: PortablePath;
+  stripComponents?: number;
+}
+
+export type ConvertToZipPayload = {
+  tmpFile: PortablePath;
+  tgz: Buffer | Uint8Array;
+  extractBufferOpts: ExtractBufferOptions;
+  compressionLevel: ZipCompression;
+};
+
+export async function convertToZipWorker(data: ConvertToZipPayload) {
+  const {tmpFile, tgz, compressionLevel, extractBufferOpts} = data;
+
+  const zipFs = new ZipFS(tmpFile, {create: true, level: compressionLevel, stats: statUtils.makeDefaultStats()});
+
+  // Buffers sent through Node are turned into regular Uint8Arrays
+  const tgzBuffer = Buffer.from(tgz.buffer, tgz.byteOffset, tgz.byteLength);
+  await extractArchiveTo(tgzBuffer, zipFs, extractBufferOpts);
+
+  zipFs.saveAndClose();
+
+  return tmpFile;
+}
+
+async function * parseTar(tgz: Buffer) {
+  // @ts-expect-error - Types are wrong about what this function returns
+  const parser: tar.ParseStream = new tar.Parse();
+
+  const passthrough = new PassThrough({objectMode: true, autoDestroy: true, emitClose: true});
+
+  parser.on(`entry`, (entry: tar.ReadEntry) => {
+    passthrough.write(entry);
+  });
+
+  parser.on(`error`, error => {
+    passthrough.destroy(error);
+  });
+
+  parser.on(`close`, () => {
+    if (!passthrough.destroyed) {
+      passthrough.end();
+    }
+  });
+
+  parser.end(tgz);
+
+  for await (const entry of passthrough) {
+    const it = entry as tar.ReadEntry;
+    yield it;
+    it.resume();
+  }
+}
+
+export async function extractArchiveTo<T extends FakeFS<PortablePath>>(tgz: Buffer, targetFs: T, {stripComponents = 0, prefixPath = PortablePath.dot}: ExtractBufferOptions = {}): Promise<T> {
+  function ignore(entry: tar.ReadEntry) {
+    // Disallow absolute paths; might be malicious (ex: /etc/passwd)
+    if (entry.path[0] === `/`)
+      return true;
+
+    const parts = entry.path.split(/\//g);
+
+    // We also ignore paths that could lead to escaping outside the archive
+    if (parts.some((part: string) => part === `..`))
+      return true;
+
+    if (parts.length <= stripComponents)
+      return true;
+
+    return false;
+  }
+
+  for await (const entry of parseTar(tgz)) {
+    if (ignore(entry))
+      continue;
+
+    const parts = ppath.normalize(npath.toPortablePath(entry.path)).replace(/\/$/, ``).split(/\//g);
+    if (parts.length <= stripComponents)
+      continue;
+
+    const slicePath = parts.slice(stripComponents).join(`/`) as PortablePath;
+    const mappedPath = ppath.join(prefixPath, slicePath);
+
+    let mode = 0o644;
+
+    // If a single executable bit is set, normalize so that all are
+    if (entry.type === `Directory` || ((entry.mode ?? 0) & 0o111) !== 0)
+      mode |= 0o111;
+
+    switch (entry.type) {
+      case `Directory`: {
+        targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [constants.SAFE_TIME, constants.SAFE_TIME]});
+
+        targetFs.mkdirSync(mappedPath, {mode});
+        targetFs.utimesSync(mappedPath, constants.SAFE_TIME, constants.SAFE_TIME);
+      } break;
+
+      case `OldFile`:
+      case `File`: {
+        targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [constants.SAFE_TIME, constants.SAFE_TIME]});
+
+        targetFs.writeFileSync(mappedPath, await miscUtils.bufferStream(entry as unknown as Readable), {mode});
+        targetFs.utimesSync(mappedPath, constants.SAFE_TIME, constants.SAFE_TIME);
+      } break;
+
+      case `SymbolicLink`: {
+        targetFs.mkdirpSync(ppath.dirname(mappedPath), {chmod: 0o755, utimes: [constants.SAFE_TIME, constants.SAFE_TIME]});
+
+        targetFs.symlinkSync((entry as any).linkpath, mappedPath);
+        targetFs.lutimesSync(mappedPath, constants.SAFE_TIME, constants.SAFE_TIME);
+      } break;
+    }
+  }
+
+  return targetFs;
+}
--- a/packages/yarnpkg-core/sources/worker-zip/Worker.ts
+++ b/packages/yarnpkg-core/sources/worker-zip/Worker.ts
@@ -1,6 +1,6 @@
 import {parentPort}                              from 'worker_threads';
 
-import {convertToZipWorker, ConvertToZipPayload} from '../tgzUtils';
+import {convertToZipWorker, ConvertToZipPayload} from '../tgzUtilsExt';
 
 
 if (!parentPort)
--- a/packages/yarnpkg-core/rollup.config.js
+++ b/packages/yarnpkg-core/rollup.config.js
@@ -41,7 +41,7 @@
     resolve({
       extensions: [`.mjs`, `.js`, `.ts`, `.tsx`, `.json`],
       rootDir: path.join(__dirname, `../../`),
-      jail: path.join(__dirname, `../../`),
+      // jail: path.join(__dirname, `../../`),
       preferBuiltins: true,
     }),
     esbuild({