@pnpm/global.packages 1100.0.9 → 1100.0.10

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.
@@ -1,3 +1,4 @@
1
+ import crypto from 'node:crypto';
1
2
  import fs from 'node:fs';
2
3
  import path from 'node:path';
3
4
  import util from 'node:util';
@@ -20,9 +21,24 @@ export function resolveInstallDir(globalDir, hash) {
20
21
  }
21
22
  }
22
23
  export function createInstallDir(globalDir) {
23
- const name = `${process.pid.toString(16)}-${Date.now().toString(16)}`;
24
- const dir = path.join(globalDir, name);
25
- fs.mkdirSync(dir, { recursive: true });
26
- return dir;
24
+ // Ensure the parent exists, then create the per-group dir *exclusively*
25
+ // (no `recursive`, which would silently reuse an existing entry or follow
26
+ // a pre-existing symlink). The name adds random bytes on top of pid+time
27
+ // so it isn't predictable and can't collide within the same millisecond.
28
+ fs.mkdirSync(globalDir, { recursive: true });
29
+ for (let i = 0; i < 10; i++) {
30
+ const name = `${process.pid.toString(16)}-${Date.now().toString(16)}-${crypto.randomBytes(8).toString('hex')}`;
31
+ const dir = path.join(globalDir, name);
32
+ try {
33
+ fs.mkdirSync(dir);
34
+ return dir;
35
+ }
36
+ catch (err) {
37
+ if (util.types.isNativeError(err) && 'code' in err && err.code === 'EEXIST')
38
+ continue;
39
+ throw err;
40
+ }
41
+ }
42
+ throw new Error('Could not create a unique global install directory');
27
43
  }
28
44
  //# sourceMappingURL=globalPackageDir.js.map
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { createGlobalCacheKey } from './cacheKey.js';
2
2
  export { createInstallDir, getHashLink, resolveInstallDir, } from './globalPackageDir.js';
3
- export { cleanOrphanedInstallDirs, findGlobalPackage, getGlobalPackageDetails, getInstalledBinNames, type GlobalPackageInfo, type InstalledGlobalPackage, scanGlobalPackages, } from './scanGlobalPackages.js';
3
+ export { cleanOrphanedInstallDirs, findGlobalPackage, getGlobalPackageDetails, getInstalledBinNames, type GlobalPackageInfo, type InstalledGlobalPackage, isValidGlobalDependencyAlias, scanGlobalPackages, } from './scanGlobalPackages.js';
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { createGlobalCacheKey } from './cacheKey.js';
2
2
  export { createInstallDir, getHashLink, resolveInstallDir, } from './globalPackageDir.js';
3
- export { cleanOrphanedInstallDirs, findGlobalPackage, getGlobalPackageDetails, getInstalledBinNames, scanGlobalPackages, } from './scanGlobalPackages.js';
3
+ export { cleanOrphanedInstallDirs, findGlobalPackage, getGlobalPackageDetails, getInstalledBinNames, isValidGlobalDependencyAlias, scanGlobalPackages, } from './scanGlobalPackages.js';
4
4
  //# sourceMappingURL=index.js.map
@@ -1,4 +1,5 @@
1
1
  import type { PackageManifest } from '@pnpm/types';
2
+ export declare function isValidGlobalDependencyAlias(alias: string): boolean;
2
3
  export interface GlobalPackageInfo {
3
4
  hash: string;
4
5
  installDir: string;
@@ -3,6 +3,43 @@ import path from 'node:path';
3
3
  import util from 'node:util';
4
4
  import { getBinsFromPackageManifest } from '@pnpm/bins.resolver';
5
5
  import { readPackageJsonFromDirRawSync, safeReadPackageJsonFromDir } from '@pnpm/pkg-manifest.reader';
6
+ const RESERVED_ALIASES = new Set(['node_modules', 'favicon.ico']);
7
+ const isUrlFriendly = (segment) => encodeURIComponent(segment) === segment;
8
+ // A dependency alias from a global group's package.json becomes a directory
9
+ // name under `node_modules` at every join site (list, conflict-check,
10
+ // remove, update). A tampered manifest could use an alias like `../x` or an
11
+ // absolute path to escape the install dir, so only valid npm package names
12
+ // are trusted. This applies the same `validForOldPackages` rules that
13
+ // `validate-npm-package-name` (and `@pnpm/fs.symlink-dependency`'s
14
+ // `safeJoinModulesDir`) enforce — implemented inline to avoid adding a
15
+ // dependency to this low-level package.
16
+ export function isValidGlobalDependencyAlias(alias) {
17
+ if (alias.length === 0)
18
+ return false;
19
+ if (/^[._-]/.test(alias))
20
+ return false;
21
+ if (alias.trim() !== alias)
22
+ return false;
23
+ if (RESERVED_ALIASES.has(alias.toLowerCase()))
24
+ return false;
25
+ if (isUrlFriendly(alias))
26
+ return true;
27
+ const scoped = /^@([^/]+)\/([^/]+)$/.exec(alias);
28
+ if (scoped) {
29
+ const [, scope, name] = scoped;
30
+ return !name.startsWith('.') && isUrlFriendly(scope) && isUrlFriendly(name);
31
+ }
32
+ return false;
33
+ }
34
+ function pickValidDependencies(dependencies) {
35
+ const result = {};
36
+ for (const [alias, spec] of Object.entries(dependencies)) {
37
+ if (isValidGlobalDependencyAlias(alias)) {
38
+ result[alias] = spec;
39
+ }
40
+ }
41
+ return result;
42
+ }
6
43
  export function scanGlobalPackages(globalDir) {
7
44
  let entries;
8
45
  try {
@@ -34,12 +71,15 @@ export function scanGlobalPackages(globalDir) {
34
71
  catch {
35
72
  continue;
36
73
  }
37
- if (!pkgJson.dependencies || Object.keys(pkgJson.dependencies).length === 0)
74
+ if (!pkgJson.dependencies)
75
+ continue;
76
+ const dependencies = pickValidDependencies(pkgJson.dependencies);
77
+ if (Object.keys(dependencies).length === 0)
38
78
  continue;
39
79
  result.push({
40
80
  hash: entry.name,
41
81
  installDir,
42
- dependencies: pkgJson.dependencies,
82
+ dependencies,
43
83
  });
44
84
  }
45
85
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pnpm/global.packages",
3
- "version": "1100.0.9",
3
+ "version": "1100.0.10",
4
4
  "description": "Utilities for managing isolated global packages",
5
5
  "keywords": [
6
6
  "pnpm",
@@ -29,13 +29,13 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@pnpm/util.lex-comparator": "^4.0.1",
32
- "@pnpm/pkg-manifest.reader": "1100.0.9",
33
- "@pnpm/types": "1101.3.2",
34
32
  "@pnpm/bins.resolver": "1100.0.8",
35
- "@pnpm/crypto.hash": "1100.0.1"
33
+ "@pnpm/crypto.hash": "1100.0.1",
34
+ "@pnpm/pkg-manifest.reader": "1100.0.9",
35
+ "@pnpm/types": "1101.3.2"
36
36
  },
37
37
  "devDependencies": {
38
- "@pnpm/global.packages": "1100.0.9"
38
+ "@pnpm/global.packages": "1100.0.10"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=22.13"