@pnpm/fs.indexed-pkg-importer 1100.0.14 → 1100.0.15

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/lib/index.js CHANGED
@@ -8,6 +8,7 @@ import { globalInfo, globalWarn } from '@pnpm/logger';
8
8
  import { fastPathTemp as pathTemp } from 'path-temp';
9
9
  import { renameOverwriteSync } from 'rename-overwrite';
10
10
  import { importIndexedDir } from './importIndexedDir.js';
11
+ import { isNativeBinary, removeQuarantine } from './removeQuarantine.js';
11
12
  export {};
12
13
  export function createIndexedPkgImporter(packageImportMethod) {
13
14
  const importPackage = createImportPackage(packageImportMethod);
@@ -115,6 +116,7 @@ function tryClonePkg(to, opts) {
115
116
  if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
116
117
  const clone = createCloneFunction();
117
118
  importIndexedDir({ importFile: clone, importFileAtomic: clone }, to, opts.filesMap, opts);
119
+ removeQuarantineFromNativeBinaries(to, opts);
118
120
  return 'clone';
119
121
  }
120
122
  return undefined;
@@ -148,6 +150,7 @@ function createClonePkg() {
148
150
  return (to, opts) => {
149
151
  if (opts.resolvedFrom !== 'store' || opts.force || !pkgExistsAtTargetDir(to, opts.filesMap)) {
150
152
  importIndexedDir(importer, to, opts.filesMap, opts);
153
+ removeQuarantineFromNativeBinaries(to, opts);
151
154
  return 'clone';
152
155
  }
153
156
  return undefined;
@@ -206,6 +209,7 @@ function createCloneFunction() {
206
209
  function hardlinkPkg(importFile, to, opts) {
207
210
  if (opts.force || shouldRelinkPkg(to, opts)) {
208
211
  importIndexedDir({ importFile, importFileAtomic: importFile }, to, opts.filesMap, opts);
212
+ removeQuarantineFromNativeBinaries(to, opts);
209
213
  return 'hardlink';
210
214
  }
211
215
  return undefined;
@@ -277,6 +281,7 @@ export function copyPkg(to, opts) {
277
281
  // can leave a partially-written file. package.json is the completion
278
282
  // marker, so it must be written atomically via temp file + rename.
279
283
  importIndexedDir({ importFile: resilientCopyFileSync, importFileAtomic: atomicCopyFileSync }, to, opts.filesMap, opts);
284
+ removeQuarantineFromNativeBinaries(to, opts);
280
285
  return 'copy';
281
286
  }
282
287
  return undefined;
@@ -295,4 +300,22 @@ function atomicCopyFileSync(src, dest) {
295
300
  }
296
301
  renameOverwriteSync(tmp, dest);
297
302
  }
303
+ // macOS preserves the com.apple.quarantine xattr when files are copied or
304
+ // reflinked out of the store, and for hardlinks the imported file shares the
305
+ // store blob's inode (and thus its xattrs). Either way Gatekeeper can block a
306
+ // native binary from loading. Drop the quarantine once, in a single batched
307
+ // `xattr` call per package, restricted to the few native binaries that
308
+ // Gatekeeper actually guards. Only store imports are cleaned: that's where the
309
+ // quarantine propagation happens and where pnpm has verified file integrity.
310
+ function removeQuarantineFromNativeBinaries(to, opts) {
311
+ if (process.platform !== 'darwin' || opts.resolvedFrom !== 'store')
312
+ return;
313
+ const nativeBinaries = [];
314
+ for (const file of opts.filesMap.keys()) {
315
+ if (isNativeBinary(file)) {
316
+ nativeBinaries.push(path.join(to, file));
317
+ }
318
+ }
319
+ removeQuarantine(nativeBinaries);
320
+ }
298
321
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Native binaries are the only files that macOS Gatekeeper blocks for carrying a
3
+ * quarantine xattr, so removing it from anything else (JavaScript, text, etc.)
4
+ * just wastes a syscall.
5
+ */
6
+ export declare function isNativeBinary(filePath: string): boolean;
7
+ /**
8
+ * Remove the macOS Gatekeeper quarantine xattr (com.apple.quarantine) from the
9
+ * given files using a single `xattr` invocation.
10
+ *
11
+ * macOS preserves extended attributes when pnpm copies or reflinks files out of
12
+ * its content-addressable store. If a store blob carries com.apple.quarantine
13
+ * (e.g. it was first written under a Gatekeeper-enabled app such as a Git
14
+ * client), the quarantine propagates to node_modules and Gatekeeper blocks the
15
+ * native binary from loading, even though pnpm already verified the file's
16
+ * integrity against the lockfile. This mirrors Homebrew's behaviour of dropping
17
+ * quarantine from verified downloads.
18
+ *
19
+ * File paths are passed as separate arguments rather than interpolated into a
20
+ * shell command, so package-controlled filenames cannot inject shell commands.
21
+ * They are split into chunks that stay under the OS argv limit.
22
+ */
23
+ export declare function removeQuarantine(filePaths: string[]): void;
@@ -0,0 +1,90 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import { globalWarn } from '@pnpm/logger';
4
+ const QUARANTINE_ATTR = 'com.apple.quarantine';
5
+ // Quarantine removal only ever runs on macOS, so the set lists the binary
6
+ // formats Gatekeeper guards there (.dll is Windows-only and never matches).
7
+ const NATIVE_BINARY_EXTENSIONS = new Set(['.node', '.dylib', '.so']);
8
+ // Cap the bytes of file-path arguments per `xattr` call so a package with many
9
+ // (or very long) native-binary paths can't blow past the OS argv limit
10
+ // (ARG_MAX, ~1 MB on macOS). Well under the limit to leave room for argv0 and
11
+ // the environment block.
12
+ const MAX_ARG_BYTES = 100_000;
13
+ /**
14
+ * Native binaries are the only files that macOS Gatekeeper blocks for carrying a
15
+ * quarantine xattr, so removing it from anything else (JavaScript, text, etc.)
16
+ * just wastes a syscall.
17
+ */
18
+ export function isNativeBinary(filePath) {
19
+ return NATIVE_BINARY_EXTENSIONS.has(path.extname(filePath).toLowerCase());
20
+ }
21
+ /**
22
+ * Remove the macOS Gatekeeper quarantine xattr (com.apple.quarantine) from the
23
+ * given files using a single `xattr` invocation.
24
+ *
25
+ * macOS preserves extended attributes when pnpm copies or reflinks files out of
26
+ * its content-addressable store. If a store blob carries com.apple.quarantine
27
+ * (e.g. it was first written under a Gatekeeper-enabled app such as a Git
28
+ * client), the quarantine propagates to node_modules and Gatekeeper blocks the
29
+ * native binary from loading, even though pnpm already verified the file's
30
+ * integrity against the lockfile. This mirrors Homebrew's behaviour of dropping
31
+ * quarantine from verified downloads.
32
+ *
33
+ * File paths are passed as separate arguments rather than interpolated into a
34
+ * shell command, so package-controlled filenames cannot inject shell commands.
35
+ * They are split into chunks that stay under the OS argv limit.
36
+ */
37
+ export function removeQuarantine(filePaths) {
38
+ if (process.platform !== 'darwin')
39
+ return;
40
+ for (const chunk of chunkByArgSize(filePaths)) {
41
+ removeQuarantineFromChunk(chunk);
42
+ }
43
+ }
44
+ function removeQuarantineFromChunk(filePaths) {
45
+ try {
46
+ execFileSync('/usr/bin/xattr', ['-d', QUARANTINE_ATTR, ...filePaths], {
47
+ stdio: ['ignore', 'ignore', 'pipe'],
48
+ });
49
+ }
50
+ catch (err) {
51
+ // `xattr -d` exits non-zero when a file simply has no quarantine xattr to
52
+ // remove ("No such xattr"), which is the common, expected case. It also
53
+ // reports "No such file" for entries the importer legitimately dropped or
54
+ // renamed (e.g. case-insensitive filename conflicts). Surface only errors
55
+ // that are not of those kinds (e.g. permission denied).
56
+ const realErrors = getStderr(err)
57
+ .split('\n')
58
+ .filter((line) => line.trim() !== '' && !line.includes('No such xattr') && !line.includes('No such file'));
59
+ if (realErrors.length > 0) {
60
+ globalWarn(`Failed to remove the macOS quarantine attribute:\n${realErrors.join('\n')}`);
61
+ }
62
+ }
63
+ }
64
+ function chunkByArgSize(filePaths) {
65
+ const chunks = [];
66
+ let chunk = [];
67
+ let chunkBytes = 0;
68
+ for (const filePath of filePaths) {
69
+ const bytes = Buffer.byteLength(filePath) + 1; // +1 for the argv null terminator
70
+ if (chunk.length > 0 && chunkBytes + bytes > MAX_ARG_BYTES) {
71
+ chunks.push(chunk);
72
+ chunk = [];
73
+ chunkBytes = 0;
74
+ }
75
+ chunk.push(filePath);
76
+ chunkBytes += bytes;
77
+ }
78
+ if (chunk.length > 0)
79
+ chunks.push(chunk);
80
+ return chunks;
81
+ }
82
+ function getStderr(err) {
83
+ if (typeof err === 'object' && err !== null && 'stderr' in err) {
84
+ const stderr = err.stderr;
85
+ if (stderr != null)
86
+ return stderr.toString();
87
+ }
88
+ return err instanceof Error ? err.message : String(err);
89
+ }
90
+ //# sourceMappingURL=removeQuarantine.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/fs.indexed-pkg-importer",
3
- "version": "1100.0.14",
3
+ "version": "1100.0.15",
4
4
  "description": "Replicates indexed directories using hard links, copies, or cloning",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -45,9 +45,9 @@
45
45
  "path-temp": "^3.0.0",
46
46
  "rename-overwrite": "^7.0.1",
47
47
  "sanitize-filename": "^1.6.4",
48
- "@pnpm/fs.graceful-fs": "1100.1.0",
49
48
  "@pnpm/core-loggers": "1100.2.1",
50
- "@pnpm/store.controller-types": "1100.1.5"
49
+ "@pnpm/store.controller-types": "1100.1.5",
50
+ "@pnpm/fs.graceful-fs": "1100.1.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "@pnpm/logger": "^1100.0.0"
@@ -55,8 +55,8 @@
55
55
  "devDependencies": {
56
56
  "@jest/globals": "30.4.1",
57
57
  "@types/fs-extra": "^11.0.4",
58
- "@pnpm/fs.indexed-pkg-importer": "1100.0.14",
59
58
  "@pnpm/logger": "1100.0.0",
59
+ "@pnpm/fs.indexed-pkg-importer": "1100.0.15",
60
60
  "@pnpm/prepare": "1100.0.16"
61
61
  },
62
62
  "engines": {