nx 22.7.8 → 22.7.9
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/dist/src/command-line/reset/reset.js +6 -1
- package/dist/src/daemon/client/client.d.ts +45 -3
- package/dist/src/daemon/client/client.js +138 -42
- package/dist/src/daemon/client/daemon-socket-messenger.js +4 -0
- package/dist/src/daemon/message-types/daemon-message.d.ts +33 -0
- package/dist/src/daemon/message-types/daemon-message.js +48 -0
- package/dist/src/daemon/server/server.js +22 -0
- package/dist/src/daemon/server/shutdown-utils.d.ts +5 -0
- package/dist/src/daemon/server/shutdown-utils.js +11 -4
- package/dist/src/daemon/socket-utils.d.ts +1 -0
- package/dist/src/daemon/socket-utils.js +24 -3
- package/dist/src/daemon/tmp-dir.d.ts +43 -3
- package/dist/src/daemon/tmp-dir.js +437 -15
- package/dist/src/devkit-internals.d.ts +1 -0
- package/dist/src/devkit-internals.js +4 -1
- package/dist/src/native/index.js +71 -28
- package/dist/src/native/native-file-cache-location.d.ts +17 -1
- package/dist/src/native/native-file-cache-location.js +86 -12
- package/dist/src/native/nx.wasm32-wasi.debug.wasm +0 -0
- package/dist/src/native/nx.wasm32-wasi.wasm +0 -0
- package/dist/src/project-graph/plugins/isolation/isolated-plugin.d.ts +1 -0
- package/dist/src/project-graph/plugins/isolation/isolated-plugin.js +18 -1
- package/dist/src/project-graph/plugins/isolation/messaging.d.ts +16 -3
- package/dist/src/project-graph/plugins/isolation/messaging.js +3 -0
- package/dist/src/project-graph/plugins/isolation/plugin-worker.js +58 -8
- package/dist/src/project-graph/project-graph.js +20 -0
- package/dist/src/utils/nx-tmp-dir.d.ts +18 -0
- package/dist/src/utils/nx-tmp-dir.js +40 -0
- package/dist/src/utils/owned-private-dir.d.ts +132 -0
- package/dist/src/utils/owned-private-dir.js +323 -0
- package/dist/src/utils/safe-spawn.d.ts +24 -0
- package/dist/src/utils/safe-spawn.js +104 -0
- package/dist/src/utils/shell-quoting.d.ts +15 -0
- package/dist/src/utils/shell-quoting.js +35 -0
- package/dist/src/utils/wait-for-socket-connection.d.ts +11 -0
- package/dist/src/utils/wait-for-socket-connection.js +14 -8
- package/package.json +11 -11
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
declare const safeSharedRootBrand: unique symbol;
|
|
2
|
+
declare const sharedRootEstablishedBrand: unique symbol;
|
|
3
|
+
declare const ownedRealDirBrand: unique symbol;
|
|
4
|
+
declare const ownedPrivateDirBrand: unique symbol;
|
|
5
|
+
/** Verified safe to keep an owner-only directory under. Not created. */
|
|
6
|
+
export type SafeSharedRoot = string & {
|
|
7
|
+
readonly [safeSharedRootBrand]: true;
|
|
8
|
+
};
|
|
9
|
+
/** Created if absent, then verified safe as above. */
|
|
10
|
+
export type EstablishedSharedRoot = string & {
|
|
11
|
+
readonly [sharedRootEstablishedBrand]: true;
|
|
12
|
+
};
|
|
13
|
+
/** An existing real directory owned by us. Mode is *not* checked. */
|
|
14
|
+
export type OwnedRealDir = string & {
|
|
15
|
+
readonly [ownedRealDirBrand]: true;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* POSIX: created if absent, owned by us, and carrying no group or other bits —
|
|
19
|
+
* re-locked first if it was looser. The mode is checked on whichever branch
|
|
20
|
+
* produced it, so the brand does not depend on who created the directory.
|
|
21
|
+
*
|
|
22
|
+
* Windows: only *is a real directory*. `getuid` is unavailable there, so
|
|
23
|
+
* neither ownership nor mode is checked; `%TMP%` is already per-account.
|
|
24
|
+
*/
|
|
25
|
+
export type OwnedPrivateDir = string & {
|
|
26
|
+
readonly [ownedPrivateDirBrand]: true;
|
|
27
|
+
};
|
|
28
|
+
/** Why a guard refused a directory. Data, not a sentence: `remedyFor` decides on it. */
|
|
29
|
+
export type DirRefusal = {
|
|
30
|
+
kind: 'not-created';
|
|
31
|
+
dir: string;
|
|
32
|
+
code?: string;
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'not-inspectable';
|
|
35
|
+
dir: string;
|
|
36
|
+
code?: string;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'not-a-directory';
|
|
39
|
+
dir: string;
|
|
40
|
+
symlink?: true;
|
|
41
|
+
} | {
|
|
42
|
+
kind: 'foreign-owner';
|
|
43
|
+
dir: string;
|
|
44
|
+
uid: number;
|
|
45
|
+
} | {
|
|
46
|
+
kind: 'foreign-shared-container';
|
|
47
|
+
dir: string;
|
|
48
|
+
uid: number;
|
|
49
|
+
} | {
|
|
50
|
+
kind: 'not-tightenable';
|
|
51
|
+
dir: string;
|
|
52
|
+
mode: number;
|
|
53
|
+
} | {
|
|
54
|
+
kind: 'peer-writable-not-sticky';
|
|
55
|
+
dir: string;
|
|
56
|
+
mode: number;
|
|
57
|
+
};
|
|
58
|
+
/** A guard's verdict. */
|
|
59
|
+
export type GuardResult<T> = {
|
|
60
|
+
status: 'ok';
|
|
61
|
+
path: T;
|
|
62
|
+
} | {
|
|
63
|
+
status: 'refused';
|
|
64
|
+
refusal: DirRefusal;
|
|
65
|
+
};
|
|
66
|
+
/** The user-facing sentence for a refusal. The only place wording is decided. */
|
|
67
|
+
export declare function describeRefusal(r: DirRefusal): string;
|
|
68
|
+
/** What the user can do about a refusal, or `undefined` when there is nothing. */
|
|
69
|
+
export declare function remedyFor(r: DirRefusal): string | undefined;
|
|
70
|
+
/** One refusal as an `Error`, so several can travel in an `AggregateError`. */
|
|
71
|
+
export declare class DirectoryRefusedError extends Error {
|
|
72
|
+
readonly refusal: DirRefusal;
|
|
73
|
+
constructor(refusal: DirRefusal);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Whether a shared container is safe to keep an owner-only directory under.
|
|
77
|
+
*
|
|
78
|
+
* On POSIX, a container writable by other users must be sticky, and it must be
|
|
79
|
+
* owned by either root or the current user. Windows short-circuits after the
|
|
80
|
+
* directory test — the OS temp root is already scoped to one account, so there
|
|
81
|
+
* is no shared level whose ownership could matter.
|
|
82
|
+
*
|
|
83
|
+
* Sticky directories still let the directory's own owner rename entries, so a
|
|
84
|
+
* container owned by another unprivileged user could replace a previously
|
|
85
|
+
* verified private directory beneath it.
|
|
86
|
+
*/
|
|
87
|
+
export declare function isSafeSharedRoot(dir: string): GuardResult<SafeSharedRoot>;
|
|
88
|
+
/**
|
|
89
|
+
* Whether other users on this machine can write into `dir`. Gates whether a
|
|
90
|
+
* refusal message may cite other users, so it must not over-report: `false` on
|
|
91
|
+
* Windows, where libuv synthesizes `st_mode` from the READONLY attribute and
|
|
92
|
+
* reports an ordinary per-account directory as `0666`, and `false` on a path
|
|
93
|
+
* that cannot be inspected.
|
|
94
|
+
*
|
|
95
|
+
* `statSync`, not `lstatSync`: the question is about the directory that will be
|
|
96
|
+
* used, not the link pointing at it. Link modes mislead in both directions —
|
|
97
|
+
* Linux creates symlinks `0777`, macOS applies the umask. Nothing here decides
|
|
98
|
+
* whether a path is accepted, so following the link costs nothing the guards
|
|
99
|
+
* do not already re-check.
|
|
100
|
+
*/
|
|
101
|
+
export declare function isPeerWritable(dir: string): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Create a shared container as sticky + world-writable if it does not exist,
|
|
104
|
+
* and report whether the resulting path is safe for the current user.
|
|
105
|
+
*
|
|
106
|
+
* **A container that already exists is never modified — only judged.** Do not
|
|
107
|
+
* chmod before deciding trust: `CAP_FOWNER` (root's default in Docker and most
|
|
108
|
+
* CI images) can chmod a directory it does not own, so a peer-owned root would
|
|
109
|
+
* be widened to `1777` and then refused, and an operator who deliberately
|
|
110
|
+
* tightened this root would have it re-widened on every `nx` process.
|
|
111
|
+
*/
|
|
112
|
+
export declare function ensureSafeSharedRoot(dir: string): GuardResult<EstablishedSharedRoot>;
|
|
113
|
+
/**
|
|
114
|
+
* Whether `dir` is an existing real directory owned by us. Unlike
|
|
115
|
+
* `ensureOwnedPrivateDir` it creates nothing and repairs nothing — for callers
|
|
116
|
+
* that only want to know whether a path is safe to act on, such as deleting.
|
|
117
|
+
*/
|
|
118
|
+
export declare function isOwnedRealDirectory(dir: string): OwnedRealDir | null;
|
|
119
|
+
/** The path segment separating one user's Nx runtime state from another's. */
|
|
120
|
+
export declare function getUserSegment(): string;
|
|
121
|
+
/**
|
|
122
|
+
* Ensure `dir` exists, is a real directory owned by us, and carries no group or
|
|
123
|
+
* other bits at all — read and search alone reach a socket inside it, so 0755 is
|
|
124
|
+
* re-locked rather than accepted. A `refused` status carries which check said
|
|
125
|
+
* no — usually `foreign-owner` for a directory another user planted, but also
|
|
126
|
+
* `not-created`/`not-inspectable` for a filesystem error, `not-a-directory`, or
|
|
127
|
+
* `not-tightenable` when the re-lock did not land.
|
|
128
|
+
*
|
|
129
|
+
* Node builtins only: reached from the native binding loader.
|
|
130
|
+
*/
|
|
131
|
+
export declare function ensureOwnedPrivateDir(dir: string): GuardResult<OwnedPrivateDir>;
|
|
132
|
+
export {};
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DirectoryRefusedError = void 0;
|
|
4
|
+
exports.describeRefusal = describeRefusal;
|
|
5
|
+
exports.remedyFor = remedyFor;
|
|
6
|
+
exports.isSafeSharedRoot = isSafeSharedRoot;
|
|
7
|
+
exports.isPeerWritable = isPeerWritable;
|
|
8
|
+
exports.ensureSafeSharedRoot = ensureSafeSharedRoot;
|
|
9
|
+
exports.isOwnedRealDirectory = isOwnedRealDirectory;
|
|
10
|
+
exports.getUserSegment = getUserSegment;
|
|
11
|
+
exports.ensureOwnedPrivateDir = ensureOwnedPrivateDir;
|
|
12
|
+
const node_fs_1 = require("node:fs");
|
|
13
|
+
const node_os_1 = require("node:os");
|
|
14
|
+
// A string discriminant, not a boolean `ok`: this repo compiles with
|
|
15
|
+
// `strict: false`, where TypeScript does not narrow a union on a boolean literal.
|
|
16
|
+
const allow = (path) => ({ status: 'ok', path });
|
|
17
|
+
const deny = (refusal) => ({
|
|
18
|
+
status: 'refused',
|
|
19
|
+
refusal,
|
|
20
|
+
});
|
|
21
|
+
const notADirectory = (dir, stats) => stats.isSymbolicLink()
|
|
22
|
+
? { kind: 'not-a-directory', dir, symlink: true }
|
|
23
|
+
: { kind: 'not-a-directory', dir };
|
|
24
|
+
// Four octal digits, so a sticky container reads `1777` and a plain directory
|
|
25
|
+
// `0755` — the notation `chmod` and `ls` use. Prefixing a literal `0` instead
|
|
26
|
+
// renders sticky modes as `01777`, which is not a form anyone writes.
|
|
27
|
+
const asMode = (mode) => (mode & 0o7777).toString(8).padStart(4, '0');
|
|
28
|
+
/** The user-facing sentence for a refusal. The only place wording is decided. */
|
|
29
|
+
function describeRefusal(r) {
|
|
30
|
+
switch (r.kind) {
|
|
31
|
+
case 'not-created':
|
|
32
|
+
return `${r.dir} could not be created${r.code ? ` (${r.code})` : ''}`;
|
|
33
|
+
case 'not-inspectable':
|
|
34
|
+
return `${r.dir} could not be inspected${r.code ? ` (${r.code})` : ''}`;
|
|
35
|
+
case 'not-a-directory':
|
|
36
|
+
return r.symlink
|
|
37
|
+
? `${r.dir} is a symlink, not a real directory — something replaced the path Nx expected to create`
|
|
38
|
+
: `${r.dir} exists and is not a directory`;
|
|
39
|
+
case 'foreign-owner':
|
|
40
|
+
return `${r.dir} is owned by uid ${r.uid}, not by you`;
|
|
41
|
+
case 'foreign-shared-container':
|
|
42
|
+
return `${r.dir} belongs to another user (uid ${r.uid}) rather than to you or to root`;
|
|
43
|
+
case 'not-tightenable':
|
|
44
|
+
return `${r.dir} is reachable by other users (mode ${asMode(r.mode)}) and could not be tightened to 0700`;
|
|
45
|
+
case 'peer-writable-not-sticky':
|
|
46
|
+
return `${r.dir} is writable by other users but not sticky (mode ${asMode(r.mode)}), so a peer could replace directories inside it`;
|
|
47
|
+
default: {
|
|
48
|
+
// This repo sets `strict: false` and leaves `noImplicitReturns` unset, so
|
|
49
|
+
// a new DirRefusal member would otherwise compile here and render as
|
|
50
|
+
// `undefined` inside the aggregate message. Assignability to `never` still
|
|
51
|
+
// holds under these settings, so this is a real compile-time guard.
|
|
52
|
+
const unhandled = r;
|
|
53
|
+
throw new Error(`Unhandled directory refusal: ${unhandled.kind}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Single-quoted for a shell, so a path with a space or quote survives a paste. */
|
|
58
|
+
const shellQuote = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
59
|
+
/** What the user can do about a refusal, or `undefined` when there is nothing. */
|
|
60
|
+
function remedyFor(r) {
|
|
61
|
+
switch (r.kind) {
|
|
62
|
+
case 'not-a-directory':
|
|
63
|
+
return r.symlink
|
|
64
|
+
? `${r.dir} is a symlink where Nx expects a directory. If you did not create it, treat it as hostile: remove the link itself (not what it points at) and run the command again.`
|
|
65
|
+
: undefined;
|
|
66
|
+
case 'foreign-owner':
|
|
67
|
+
return `${r.dir} belongs to another user on this machine, so Nx cannot keep its own directory there. Set NX_SOCKET_DIR to a short directory your user owns, or move it aside — which you can do yourself if you own the directory it sits in, and otherwise needs an administrator.`;
|
|
68
|
+
case 'not-tightenable':
|
|
69
|
+
// Both producers reach here having already established that the directory
|
|
70
|
+
// is ours, so `chmod` is the user's to run. Names no cause: one producer
|
|
71
|
+
// is a mount that discards the mode, the other is any `chmod` failure.
|
|
72
|
+
return `${r.dir} is reachable by other users (mode ${asMode(r.mode)}) and Nx could not restrict it. Run \`chmod 0700 ${shellQuote(r.dir)}\` and try again; if the mode does not stick, set NX_SOCKET_DIR to a short directory on a filesystem that keeps POSIX permissions.`;
|
|
73
|
+
case 'foreign-shared-container':
|
|
74
|
+
// Unreachable today: `isSafeSharedRoot` denies with this kind only when
|
|
75
|
+
// `stats.uid !== 0`, so a root-owned container never reaches here.
|
|
76
|
+
if (r.uid === 0) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const q = shellQuote(r.dir);
|
|
80
|
+
return `${r.dir} belongs to another user on this machine, so Nx cannot keep a private directory beneath it. Ask an administrator to hand it to root with \`sudo chown root ${q} && sudo chmod 1777 ${q}\`; every user can then keep their own directory under it.`;
|
|
81
|
+
case 'not-created':
|
|
82
|
+
case 'not-inspectable':
|
|
83
|
+
case 'peer-writable-not-sticky':
|
|
84
|
+
return undefined;
|
|
85
|
+
default: {
|
|
86
|
+
// Matches `describeRefusal`'s arm, so a new kind cannot ship with a
|
|
87
|
+
// sentence and no advice — which is how `not-tightenable` went unadvised.
|
|
88
|
+
const unhandled = r;
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** One refusal as an `Error`, so several can travel in an `AggregateError`. */
|
|
94
|
+
class DirectoryRefusedError extends Error {
|
|
95
|
+
constructor(refusal) {
|
|
96
|
+
super(describeRefusal(refusal));
|
|
97
|
+
this.refusal = refusal;
|
|
98
|
+
this.name = 'DirectoryRefusedError';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.DirectoryRefusedError = DirectoryRefusedError;
|
|
102
|
+
/**
|
|
103
|
+
* chmod a path only if it is a real directory, never following a symlink at its
|
|
104
|
+
* final component — `chmodSync` follows them, retargeting the mode change. The
|
|
105
|
+
* directory check is on the descriptor, not the errno: a deny-list fails *open*
|
|
106
|
+
* on codes it does not know. `O_NONBLOCK` stops a planted FIFO blocking `openSync`.
|
|
107
|
+
*/
|
|
108
|
+
function chmodRealDirectory(path, mode) {
|
|
109
|
+
let fd;
|
|
110
|
+
try {
|
|
111
|
+
fd = (0, node_fs_1.openSync)(path, node_fs_1.constants.O_RDONLY | node_fs_1.constants.O_NOFOLLOW | node_fs_1.constants.O_NONBLOCK);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
if (!(0, node_fs_1.fstatSync)(fd).isDirectory()) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
(0, node_fs_1.fchmodSync)(fd, mode);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
(0, node_fs_1.closeSync)(fd);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Sticky. Restricts rename and unlink in a writable directory to the owner of
|
|
132
|
+
* each entry — plus the directory's own owner, which is why the ownership check
|
|
133
|
+
* below is not redundant with this one.
|
|
134
|
+
*/
|
|
135
|
+
const S_ISVTX = 0o1000;
|
|
136
|
+
/**
|
|
137
|
+
* Whether a shared container is safe to keep an owner-only directory under.
|
|
138
|
+
*
|
|
139
|
+
* On POSIX, a container writable by other users must be sticky, and it must be
|
|
140
|
+
* owned by either root or the current user. Windows short-circuits after the
|
|
141
|
+
* directory test — the OS temp root is already scoped to one account, so there
|
|
142
|
+
* is no shared level whose ownership could matter.
|
|
143
|
+
*
|
|
144
|
+
* Sticky directories still let the directory's own owner rename entries, so a
|
|
145
|
+
* container owned by another unprivileged user could replace a previously
|
|
146
|
+
* verified private directory beneath it.
|
|
147
|
+
*/
|
|
148
|
+
function isSafeSharedRoot(dir) {
|
|
149
|
+
try {
|
|
150
|
+
const stats = (0, node_fs_1.lstatSync)(dir);
|
|
151
|
+
if (!stats.isDirectory()) {
|
|
152
|
+
return deny(notADirectory(dir, stats));
|
|
153
|
+
}
|
|
154
|
+
if (process.platform === 'win32') {
|
|
155
|
+
// The OS temp root is already scoped to the current Windows user.
|
|
156
|
+
return allow(dir);
|
|
157
|
+
}
|
|
158
|
+
if (typeof process.getuid === 'function' &&
|
|
159
|
+
stats.uid !== process.getuid() &&
|
|
160
|
+
stats.uid !== 0) {
|
|
161
|
+
return deny({ kind: 'foreign-shared-container', dir, uid: stats.uid });
|
|
162
|
+
}
|
|
163
|
+
return !(stats.mode & 0o022) || !!(stats.mode & S_ISVTX)
|
|
164
|
+
? allow(dir)
|
|
165
|
+
: deny({ kind: 'peer-writable-not-sticky', dir, mode: stats.mode });
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
return deny({ kind: 'not-inspectable', dir, code: e?.code });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Whether other users on this machine can write into `dir`. Gates whether a
|
|
173
|
+
* refusal message may cite other users, so it must not over-report: `false` on
|
|
174
|
+
* Windows, where libuv synthesizes `st_mode` from the READONLY attribute and
|
|
175
|
+
* reports an ordinary per-account directory as `0666`, and `false` on a path
|
|
176
|
+
* that cannot be inspected.
|
|
177
|
+
*
|
|
178
|
+
* `statSync`, not `lstatSync`: the question is about the directory that will be
|
|
179
|
+
* used, not the link pointing at it. Link modes mislead in both directions —
|
|
180
|
+
* Linux creates symlinks `0777`, macOS applies the umask. Nothing here decides
|
|
181
|
+
* whether a path is accepted, so following the link costs nothing the guards
|
|
182
|
+
* do not already re-check.
|
|
183
|
+
*/
|
|
184
|
+
function isPeerWritable(dir) {
|
|
185
|
+
if (process.platform === 'win32') {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
return !!((0, node_fs_1.statSync)(dir).mode & 0o022);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Create a shared container as sticky + world-writable if it does not exist,
|
|
197
|
+
* and report whether the resulting path is safe for the current user.
|
|
198
|
+
*
|
|
199
|
+
* **A container that already exists is never modified — only judged.** Do not
|
|
200
|
+
* chmod before deciding trust: `CAP_FOWNER` (root's default in Docker and most
|
|
201
|
+
* CI images) can chmod a directory it does not own, so a peer-owned root would
|
|
202
|
+
* be widened to `1777` and then refused, and an operator who deliberately
|
|
203
|
+
* tightened this root would have it re-widened on every `nx` process.
|
|
204
|
+
*/
|
|
205
|
+
function ensureSafeSharedRoot(dir) {
|
|
206
|
+
if (process.platform === 'win32') {
|
|
207
|
+
try {
|
|
208
|
+
(0, node_fs_1.mkdirSync)(dir, { recursive: true });
|
|
209
|
+
return allow(dir);
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
return deny({ kind: 'not-created', dir, code: e?.code });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
(0, node_fs_1.mkdirSync)(dir, { mode: 0o1777 });
|
|
217
|
+
// Ours, created a statement ago. Load-bearing on macOS: XNU strips S_ISVTX
|
|
218
|
+
// at mkdir, so the sticky bit exists solely because of this call.
|
|
219
|
+
chmodRealDirectory(dir, 0o1777);
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
if (e?.code !== 'EEXIST') {
|
|
223
|
+
return deny({ kind: 'not-created', dir, code: e?.code });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// One verdict for both branches: the chmod above can fail and leave a
|
|
227
|
+
// peer-writable non-sticky container, which trusting the creation would brand
|
|
228
|
+
// safe.
|
|
229
|
+
const verdict = isSafeSharedRoot(dir);
|
|
230
|
+
return verdict.status === 'ok'
|
|
231
|
+
? allow(dir)
|
|
232
|
+
: deny(verdict.refusal);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Whether `dir` is an existing real directory owned by us. Unlike
|
|
236
|
+
* `ensureOwnedPrivateDir` it creates nothing and repairs nothing — for callers
|
|
237
|
+
* that only want to know whether a path is safe to act on, such as deleting.
|
|
238
|
+
*/
|
|
239
|
+
function isOwnedRealDirectory(dir) {
|
|
240
|
+
try {
|
|
241
|
+
const stats = (0, node_fs_1.lstatSync)(dir);
|
|
242
|
+
if (!stats.isDirectory()) {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
return typeof process.getuid !== 'function' ||
|
|
246
|
+
stats.uid === process.getuid()
|
|
247
|
+
? dir
|
|
248
|
+
: null;
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** The path segment separating one user's Nx runtime state from another's. */
|
|
255
|
+
function getUserSegment() {
|
|
256
|
+
try {
|
|
257
|
+
if (typeof process.getuid === 'function') {
|
|
258
|
+
return String(process.getuid());
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
catch { }
|
|
262
|
+
try {
|
|
263
|
+
const { username } = (0, node_os_1.userInfo)();
|
|
264
|
+
if (username) {
|
|
265
|
+
return username;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
catch { }
|
|
269
|
+
return 'unknown';
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Ensure `dir` exists, is a real directory owned by us, and carries no group or
|
|
273
|
+
* other bits at all — read and search alone reach a socket inside it, so 0755 is
|
|
274
|
+
* re-locked rather than accepted. A `refused` status carries which check said
|
|
275
|
+
* no — usually `foreign-owner` for a directory another user planted, but also
|
|
276
|
+
* `not-created`/`not-inspectable` for a filesystem error, `not-a-directory`, or
|
|
277
|
+
* `not-tightenable` when the re-lock did not land.
|
|
278
|
+
*
|
|
279
|
+
* Node builtins only: reached from the native binding loader.
|
|
280
|
+
*/
|
|
281
|
+
function ensureOwnedPrivateDir(dir) {
|
|
282
|
+
try {
|
|
283
|
+
(0, node_fs_1.mkdirSync)(dir, { mode: 0o700 });
|
|
284
|
+
}
|
|
285
|
+
catch (e) {
|
|
286
|
+
if (e?.code !== 'EEXIST') {
|
|
287
|
+
return deny({ kind: 'not-created', dir, code: e?.code });
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// One verdict for both branches, as in `ensureSafeSharedRoot`: mounts that
|
|
291
|
+
// ignore the mode argument can land a directory Nx asked for at 0700 on 0777,
|
|
292
|
+
// so the creation path is judged like any other.
|
|
293
|
+
try {
|
|
294
|
+
const stats = (0, node_fs_1.lstatSync)(dir);
|
|
295
|
+
// Before the Windows short-circuit: "is a real directory" holds on every
|
|
296
|
+
// platform.
|
|
297
|
+
if (!stats.isDirectory()) {
|
|
298
|
+
return deny(notADirectory(dir, stats));
|
|
299
|
+
}
|
|
300
|
+
if (typeof process.getuid !== 'function') {
|
|
301
|
+
// Windows: the roots there are per-user OS temp dirs, not a shared /tmp.
|
|
302
|
+
return allow(dir);
|
|
303
|
+
}
|
|
304
|
+
if (stats.uid !== process.getuid()) {
|
|
305
|
+
return deny({ kind: 'foreign-owner', dir, uid: stats.uid });
|
|
306
|
+
}
|
|
307
|
+
if (stats.mode & 0o077) {
|
|
308
|
+
if (!chmodRealDirectory(dir, 0o700)) {
|
|
309
|
+
return deny({ kind: 'not-tightenable', dir, mode: stats.mode });
|
|
310
|
+
}
|
|
311
|
+
// Read the mode back rather than trusting the chmod's return: mounts that
|
|
312
|
+
// ignore modes report success and change nothing.
|
|
313
|
+
const after = (0, node_fs_1.lstatSync)(dir);
|
|
314
|
+
if (after.mode & 0o077) {
|
|
315
|
+
return deny({ kind: 'not-tightenable', dir, mode: after.mode });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return allow(dir);
|
|
319
|
+
}
|
|
320
|
+
catch (e) {
|
|
321
|
+
return deny({ kind: 'not-inspectable', dir, code: e?.code });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ChildProcess, ExecFileSyncOptions, SpawnOptions } from 'child_process';
|
|
2
|
+
/**
|
|
3
|
+
* Spawn a binary that may be a Windows `.cmd`/`.bat` shim, without letting its
|
|
4
|
+
* arguments become shell syntax.
|
|
5
|
+
*
|
|
6
|
+
* Off Windows, and for a `.exe`, no shell is involved at all. Where one is —
|
|
7
|
+
* a shim or a bare name — the binary and every argument are quoted for cmd.exe.
|
|
8
|
+
*
|
|
9
|
+
* Not a complete guarantee: cmd.exe expands `%VAR%` in the binary or any
|
|
10
|
+
* argument whether or not it is quoted, so a caller with untrusted arguments
|
|
11
|
+
* inherits that gap (NXC-4798). See the note on `LINE_BREAK` above.
|
|
12
|
+
*
|
|
13
|
+
* @throws on Windows when the binary or an argument contains a line break, or a
|
|
14
|
+
* literal `"` (via `quoteShellArg`). Those are the only refusals — the `%`
|
|
15
|
+
* expansion above is not one of them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function safeSpawn(binary: string, args: readonly string[], options?: Omit<SpawnOptions, 'shell'>): ChildProcess;
|
|
18
|
+
/**
|
|
19
|
+
* Synchronous {@link safeSpawn}, returning the child's stdout.
|
|
20
|
+
*
|
|
21
|
+
* Carries the same Windows caveats: `%VAR%` is expanded quoted or not
|
|
22
|
+
* (NXC-4798), and a line break or literal `"` throws.
|
|
23
|
+
*/
|
|
24
|
+
export declare function safeExecFileSync(binary: string, args: readonly string[], options?: Omit<ExecFileSyncOptions, 'encoding' | 'shell'>): string;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.safeSpawn = safeSpawn;
|
|
4
|
+
exports.safeExecFileSync = safeExecFileSync;
|
|
5
|
+
const child_process_1 = require("child_process");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const shell_quoting_1 = require("./shell-quoting");
|
|
8
|
+
// Node refuses to launch a Windows `.cmd`/`.bat` shim without a shell
|
|
9
|
+
// (CVE-2024-27980). A bare name takes the shell for the same reason, one step
|
|
10
|
+
// removed: libuv walks PATH and PATHEXT itself, so no shell is needed to *find*
|
|
11
|
+
// the binary — but the walk may land on a `.cmd`/`.bat`, and this cannot know
|
|
12
|
+
// which in advance, so it is deliberately conservative. Everything else,
|
|
13
|
+
// including a `.exe`, spawns directly, so the shell and the quoting below reach
|
|
14
|
+
// only the invocations that cannot do without them.
|
|
15
|
+
//
|
|
16
|
+
// Deliberately a function: a module-level const is fixed at import time, which
|
|
17
|
+
// would make the Windows branch untestable.
|
|
18
|
+
function needsShell(binary) {
|
|
19
|
+
if (process.platform !== 'win32') {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
// win32.extname, not the platform default: this branch only runs on Windows,
|
|
23
|
+
// where `path` is win32 anyway, and pinning it lets a test model the real
|
|
24
|
+
// parse rather than posix's (which reads `C:\ws\my.app\gradlew` as `.app\gradlew`).
|
|
25
|
+
const ext = path_1.win32.extname(binary).toLowerCase();
|
|
26
|
+
return ext === '' || ext === '.cmd' || ext === '.bat';
|
|
27
|
+
}
|
|
28
|
+
// A line break ends the command line whatever the quoting, so nothing can carry
|
|
29
|
+
// one.
|
|
30
|
+
//
|
|
31
|
+
// `%` is deliberately NOT refused, and this is a known gap rather than a safe
|
|
32
|
+
// case. cmd.exe expands `%VAR%` whether or not the value is quoted — see
|
|
33
|
+
// `quoteShellArg`'s own docstring in ./shell-quoting, which says so and pins it
|
|
34
|
+
// with a test — so quoting contains nothing here. It applies to the binary path
|
|
35
|
+
// as much as to the arguments, and an expansion yielding a `"` defeats the
|
|
36
|
+
// literal-quote refusal below, because that inspects the pre-expansion bytes.
|
|
37
|
+
// `bin/nx.ts` loads the workspace's own `.env` before the graph is built, so a
|
|
38
|
+
// repo can set the variable too.
|
|
39
|
+
//
|
|
40
|
+
// Refusing `%` is what an earlier revision did, and it made any workspace under
|
|
41
|
+
// a `%` path — a legal Windows directory name — fail graph construction. The gap
|
|
42
|
+
// is bounded by the argument that bounds this whole class: a repo that can reach
|
|
43
|
+
// here already has `mvnw` / `pom.xml` executing on its behalf. Closing it
|
|
44
|
+
// properly is tracked as NXC-4798.
|
|
45
|
+
const LINE_BREAK = /[\r\n]/;
|
|
46
|
+
/**
|
|
47
|
+
* Spawn a binary that may be a Windows `.cmd`/`.bat` shim, without letting its
|
|
48
|
+
* arguments become shell syntax.
|
|
49
|
+
*
|
|
50
|
+
* Off Windows, and for a `.exe`, no shell is involved at all. Where one is —
|
|
51
|
+
* a shim or a bare name — the binary and every argument are quoted for cmd.exe.
|
|
52
|
+
*
|
|
53
|
+
* Not a complete guarantee: cmd.exe expands `%VAR%` in the binary or any
|
|
54
|
+
* argument whether or not it is quoted, so a caller with untrusted arguments
|
|
55
|
+
* inherits that gap (NXC-4798). See the note on `LINE_BREAK` above.
|
|
56
|
+
*
|
|
57
|
+
* @throws on Windows when the binary or an argument contains a line break, or a
|
|
58
|
+
* literal `"` (via `quoteShellArg`). Those are the only refusals — the `%`
|
|
59
|
+
* expansion above is not one of them.
|
|
60
|
+
*/
|
|
61
|
+
function safeSpawn(binary, args, options = {}) {
|
|
62
|
+
const shell = needsShell(binary);
|
|
63
|
+
return (0, child_process_1.spawn)(quoteBinary(binary, shell), quoteArgs(args, shell), {
|
|
64
|
+
...options,
|
|
65
|
+
windowsHide: true,
|
|
66
|
+
shell,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Synchronous {@link safeSpawn}, returning the child's stdout.
|
|
71
|
+
*
|
|
72
|
+
* Carries the same Windows caveats: `%VAR%` is expanded quoted or not
|
|
73
|
+
* (NXC-4798), and a line break or literal `"` throws.
|
|
74
|
+
*/
|
|
75
|
+
function safeExecFileSync(binary, args, options = {}) {
|
|
76
|
+
const shell = needsShell(binary);
|
|
77
|
+
return (0, child_process_1.execFileSync)(quoteBinary(binary, shell), quoteArgs(args, shell), {
|
|
78
|
+
stdio: 'pipe',
|
|
79
|
+
...options,
|
|
80
|
+
windowsHide: true,
|
|
81
|
+
shell,
|
|
82
|
+
encoding: 'utf-8',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// The binary needs quoting too: Node joins it into the same command line, so a
|
|
86
|
+
// path holding a space or an `&` would split there.
|
|
87
|
+
function quoteBinary(binary, shell) {
|
|
88
|
+
if (!shell) {
|
|
89
|
+
return binary;
|
|
90
|
+
}
|
|
91
|
+
return quoteForCmd(binary, `the path ${JSON.stringify(binary)}`);
|
|
92
|
+
}
|
|
93
|
+
function quoteForCmd(value, described, remedy = '') {
|
|
94
|
+
if (LINE_BREAK.test(value)) {
|
|
95
|
+
throw new Error(`Cannot pass ${described} to cmd.exe: a line break inside it would end the command line before cmd.exe reached the rest of it.${remedy}`);
|
|
96
|
+
}
|
|
97
|
+
return (0, shell_quoting_1.quoteShellArg)(value);
|
|
98
|
+
}
|
|
99
|
+
function quoteArgs(args, shell) {
|
|
100
|
+
if (!shell) {
|
|
101
|
+
return [...args];
|
|
102
|
+
}
|
|
103
|
+
return args.map((arg) => quoteForCmd(arg, `the argument ${JSON.stringify(arg)}`, ' Remove it from your Nx configuration and try again.'));
|
|
104
|
+
}
|
|
@@ -8,3 +8,18 @@ export declare function needsShellQuoting(str: string): boolean;
|
|
|
8
8
|
* Check if a string is already wrapped in matching quotes (single or double).
|
|
9
9
|
*/
|
|
10
10
|
export declare function isAlreadyQuoted(str: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Quote a string so it survives being interpolated into a shell command line
|
|
13
|
+
* as a single argument.
|
|
14
|
+
*
|
|
15
|
+
* On Windows the safety boundary is one unbroken double-quoted run: it keeps
|
|
16
|
+
* `^`, `&`, `|`, `<` and `>` literal through cmd.exe's parse and a `.cmd`
|
|
17
|
+
* shim's re-parse of `%*`, but not `%`, which cmd.exe expands inside double
|
|
18
|
+
* quotes too.
|
|
19
|
+
*
|
|
20
|
+
* @throws on Windows when the argument contains a double quote, which ends that
|
|
21
|
+
* run, since cmd.exe recognizes no backslash escape. Carrying one means
|
|
22
|
+
* caret-escaping every metacharacter instead, doubled for a `.cmd` shim, which
|
|
23
|
+
* this path does not implement.
|
|
24
|
+
*/
|
|
25
|
+
export declare function quoteShellArg(arg: string): string;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.needsShellQuoting = needsShellQuoting;
|
|
4
4
|
exports.isAlreadyQuoted = isAlreadyQuoted;
|
|
5
|
+
exports.quoteShellArg = quoteShellArg;
|
|
5
6
|
/**
|
|
6
7
|
* Shell metacharacters that have special meaning and require quoting.
|
|
7
8
|
*
|
|
@@ -39,3 +40,37 @@ function isAlreadyQuoted(str) {
|
|
|
39
40
|
((str[0] === "'" && str[str.length - 1] === "'") ||
|
|
40
41
|
(str[0] === '"' && str[str.length - 1] === '"')));
|
|
41
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Quote a string so it survives being interpolated into a shell command line
|
|
45
|
+
* as a single argument.
|
|
46
|
+
*
|
|
47
|
+
* On Windows the safety boundary is one unbroken double-quoted run: it keeps
|
|
48
|
+
* `^`, `&`, `|`, `<` and `>` literal through cmd.exe's parse and a `.cmd`
|
|
49
|
+
* shim's re-parse of `%*`, but not `%`, which cmd.exe expands inside double
|
|
50
|
+
* quotes too.
|
|
51
|
+
*
|
|
52
|
+
* @throws on Windows when the argument contains a double quote, which ends that
|
|
53
|
+
* run, since cmd.exe recognizes no backslash escape. Carrying one means
|
|
54
|
+
* caret-escaping every metacharacter instead, doubled for a `.cmd` shim, which
|
|
55
|
+
* this path does not implement.
|
|
56
|
+
*/
|
|
57
|
+
function quoteShellArg(arg) {
|
|
58
|
+
const isWindows = process.platform === 'win32';
|
|
59
|
+
if (isWindows && arg.includes('"')) {
|
|
60
|
+
throw new Error(`Cannot safely pass ${arg} to cmd.exe as a single argument: a double quote inside it would end the quoting and leave the rest of the argument to be read as commands. Remove the double quote and run the command again.`);
|
|
61
|
+
}
|
|
62
|
+
if (arg === '') {
|
|
63
|
+
// an unquoted empty string would vanish when joined into a command line
|
|
64
|
+
return isWindows ? '""' : "''";
|
|
65
|
+
}
|
|
66
|
+
// `^` is cmd.exe syntax rather than shell syntax, so it earns quoting only
|
|
67
|
+
// where cmd.exe parses the command line.
|
|
68
|
+
if (!needsShellQuoting(arg) && !(isWindows && arg.includes('^'))) {
|
|
69
|
+
return arg;
|
|
70
|
+
}
|
|
71
|
+
return isWindows
|
|
72
|
+
? // MSVCRT reads the backslashes that precede the closing quote as escapes,
|
|
73
|
+
// so they have to be doubled to survive as themselves.
|
|
74
|
+
`"${arg.replace(/(\\+)$/, '$1$1')}"`
|
|
75
|
+
: `'${arg.replace(/'/g, `'\\''`)}'`;
|
|
76
|
+
}
|
|
@@ -12,4 +12,15 @@ export declare function waitForSocketConnection(socketPath: string | (() => stri
|
|
|
12
12
|
signal?: AbortSignal;
|
|
13
13
|
maxAttempts?: number;
|
|
14
14
|
delayMs?: number;
|
|
15
|
+
/**
|
|
16
|
+
* Called with the errno of each failed connect and the path it was made
|
|
17
|
+
* against. Return `true` to stop polling: a permission refusal does not
|
|
18
|
+
* heal by retrying, and without a way out the caller waits the full budget
|
|
19
|
+
* and then cannot say why.
|
|
20
|
+
*
|
|
21
|
+
* The path is passed rather than left to the caller to recompute — with a
|
|
22
|
+
* resolver it can differ per attempt, and the caller that reports the
|
|
23
|
+
* refusal may no longer be able to resolve one at all.
|
|
24
|
+
*/
|
|
25
|
+
onConnectError?: (error: NodeJS.ErrnoException, socketPath: string) => boolean | void;
|
|
15
26
|
}): Promise<Socket | null>;
|