@camunda8/cli 4.2.0-alpha.2 → 4.2.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/dist/core/config.d.ts +26 -2
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +500 -12
- package/dist/core/config.js.map +1 -1
- package/dist/default-plugins/bpmn/c8ctl-plugin.js +476 -288
- package/dist/default-plugins/bpmn/package.json +4 -4
- package/dist/default-plugins/element-template/c8ctl-plugin.js +394 -268
- package/dist/default-plugins/element-template/package.json +3 -3
- package/dist/default-plugins/feel/c8ctl-plugin.js +5815 -7974
- package/dist/default-plugins/feel/package.json +1 -1
- package/dist/vendor/bpmn-element-templates.cjs +4780 -3050
- package/dist/vendor/bpmnlint.cjs +6447 -15536
- package/package.json +1 -1
package/dist/core/config.d.ts
CHANGED
|
@@ -122,11 +122,28 @@ export declare function getPluginsDir(): string;
|
|
|
122
122
|
*/
|
|
123
123
|
export declare function ensurePluginsDir(): string;
|
|
124
124
|
/**
|
|
125
|
-
* Load c8ctl profiles from profiles.json
|
|
125
|
+
* Load c8ctl profiles from profiles.json.
|
|
126
|
+
*
|
|
127
|
+
* A MISSING file legitimately means "no profiles yet" → `[]`. But a file that
|
|
128
|
+
* exists and cannot be read/parsed is NOT empty — it is corrupt or transiently
|
|
129
|
+
* unreadable (e.g. a torn write from a crash or two c8ctl processes racing).
|
|
130
|
+
* Returning `[]` in that case is catastrophic: the very next `saveProfiles`
|
|
131
|
+
* (via `addProfile`/`ensureDefaultProfile`/`removeProfile`) would overwrite the
|
|
132
|
+
* file with a truncated set, silently DESTROYING every saved profile. So we
|
|
133
|
+
* distinguish the two: absent → `[]`; present-but-unreadable → THROW (after
|
|
134
|
+
* preserving the original bytes in a content-addressed backup), so no caller can
|
|
135
|
+
* mistake corruption for emptiness and clobber the user's profiles.
|
|
126
136
|
*/
|
|
127
137
|
export declare function loadProfiles(): Profile[];
|
|
128
138
|
/**
|
|
129
|
-
* Save c8ctl profiles to profiles.json
|
|
139
|
+
* Save c8ctl profiles to profiles.json ATOMICALLY.
|
|
140
|
+
*
|
|
141
|
+
* A bare `writeFileSync` truncates the target and then streams the new bytes, so
|
|
142
|
+
* a crash, a full disk, or a second c8ctl process reading mid-write can observe
|
|
143
|
+
* a TORN file — which `loadProfiles` then can't parse, and (pre-fix) treated as
|
|
144
|
+
* "no profiles", cascading into a total wipe. Writing to a same-directory temp
|
|
145
|
+
* file and `rename`-ing over the target makes the swap atomic: a reader sees
|
|
146
|
+
* either the whole old file or the whole new one, never a partial one.
|
|
130
147
|
*/
|
|
131
148
|
export declare function saveProfiles(profiles: Profile[]): void;
|
|
132
149
|
/**
|
|
@@ -189,6 +206,13 @@ export declare const DEFAULT_PROFILE_CONFIG: Profile;
|
|
|
189
206
|
/**
|
|
190
207
|
* Ensure the default 'local' profile exists in profiles.json.
|
|
191
208
|
* If no 'local' profile is configured, creates one with the localhost defaults.
|
|
209
|
+
*
|
|
210
|
+
* Runs on virtually every command (via `loadSessionState`). It MUST NOT be the
|
|
211
|
+
* thing that destroys a user's profiles: if `getProfile` throws because
|
|
212
|
+
* profiles.json is present but unreadable/corrupt, seeding here would reload the
|
|
213
|
+
* (still-unreadable) file, see zero profiles, and overwrite it — wiping every
|
|
214
|
+
* saved profile. So on a read failure we warn and leave the file untouched
|
|
215
|
+
* rather than reseed. `loadProfiles` already preserved the original bytes.
|
|
192
216
|
*/
|
|
193
217
|
export declare function ensureDefaultProfile(): void;
|
|
194
218
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAqBH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAQ9C,eAAO,MAAM,YAAY;;;C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,CAAC,MAAM,EAAE,MAAM,CAAC,EAC9B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GACX,IAAI,CAQN;AASD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAyB1E;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GACtC,OAAO,CAUT;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAGzC;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,aAAa,CAIxE;AAqED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAuB5D"}
|
package/dist/core/config.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* c8ctl stores its own profiles in DATA_DIR/c8ctl/profiles.json
|
|
5
5
|
* Modeler connections are read from settings.json (read-only) with "modeler:" prefix
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
7
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
8
|
+
import { closeSync, constants, existsSync, fchmodSync, fstatSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
8
9
|
import { homedir, platform } from "node:os";
|
|
9
10
|
import { join } from "node:path";
|
|
10
11
|
import { getLogger, isRecord } from "./logger.js";
|
|
@@ -183,29 +184,502 @@ function getModelerSettingsPath() {
|
|
|
183
184
|
return join(getModelerDataDir(), "settings.json");
|
|
184
185
|
}
|
|
185
186
|
/**
|
|
186
|
-
*
|
|
187
|
+
* The permission bits (0oNNN) of an existing file, or `undefined` if it cannot
|
|
188
|
+
* be stat'd. Used to carry a user-protected `profiles.json` mode (e.g. 0600 —
|
|
189
|
+
* it holds client secrets and passwords) onto its backups and atomic-replace
|
|
190
|
+
* temp files, so persistence never silently widens the file to the umask
|
|
191
|
+
* default and exposes credentials to other local users.
|
|
192
|
+
*/
|
|
193
|
+
function fileMode(path) {
|
|
194
|
+
try {
|
|
195
|
+
return statSync(path).mode & 0o777;
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/** True on native Windows, where POSIX permission bits do not apply. */
|
|
202
|
+
const isWindows = platform() === "win32";
|
|
203
|
+
/**
|
|
204
|
+
* Re-tighten an open fd to `mode` THROUGH THE FD — but only where the platform
|
|
205
|
+
* implements it. `fchmodSync` throws `ERR_METHOD_NOT_IMPLEMENTED` on Windows
|
|
206
|
+
* (access there is governed by ACLs, not POSIX mode bits), so an unconditional
|
|
207
|
+
* call would throw on every profile save and corrupt-file recovery on the
|
|
208
|
+
* Windows CI leg — the catch would then delete the staged temp and report that
|
|
209
|
+
* no backup was written. The create-time `mode` already covers the only bit
|
|
210
|
+
* Windows honours (read-only), so skipping the fd chmod there loses nothing.
|
|
211
|
+
*/
|
|
212
|
+
function pinFdMode(fd, mode) {
|
|
213
|
+
if (!isWindows)
|
|
214
|
+
fchmodSync(fd, mode);
|
|
215
|
+
}
|
|
216
|
+
/** The optional, string-typed fields of a `Profile`. */
|
|
217
|
+
const OPTIONAL_STRING_PROFILE_FIELDS = [
|
|
218
|
+
"clientId",
|
|
219
|
+
"clientSecret",
|
|
220
|
+
"audience",
|
|
221
|
+
"oAuthUrl",
|
|
222
|
+
"scope",
|
|
223
|
+
"username",
|
|
224
|
+
"password",
|
|
225
|
+
"defaultTenantId",
|
|
226
|
+
];
|
|
227
|
+
/** A `Record<string, string>` — every value is a string. */
|
|
228
|
+
function isStringRecord(value) {
|
|
229
|
+
return (isRecord(value) && Object.values(value).every((v) => typeof v === "string"));
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* A well-formed `Profile`. Validates the required `name`/`baseUrl` AND every
|
|
233
|
+
* optional field's runtime type: a syntactically valid file whose optional
|
|
234
|
+
* fields have the wrong type (a non-string credential, a non-map `headers`, a
|
|
235
|
+
* non-boolean `exactBaseUrl`) is schema corruption, not a healthy profile —
|
|
236
|
+
* accepting it would let `profileToClusterConfig`/the request layer later
|
|
237
|
+
* mishandle authentication, headers, or URL suffixing instead of the file
|
|
238
|
+
* being rejected up front.
|
|
239
|
+
*/
|
|
240
|
+
function isValidProfile(value) {
|
|
241
|
+
if (!isRecord(value) ||
|
|
242
|
+
typeof value.name !== "string" ||
|
|
243
|
+
typeof value.baseUrl !== "string") {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
for (const field of OPTIONAL_STRING_PROFILE_FIELDS) {
|
|
247
|
+
if (value[field] !== undefined && typeof value[field] !== "string") {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (value.exactBaseUrl !== undefined &&
|
|
252
|
+
typeof value.exactBaseUrl !== "boolean") {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
if (value.headers !== undefined && !isStringRecord(value.headers)) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Runtime schema validation for a parsed profiles.json. A compile-time
|
|
262
|
+
* `ProfilesFile` annotation is erased at runtime, so a syntactically valid but
|
|
263
|
+
* schema-corrupt file (`{}`, `{"profiles": null}`, `[]`, or entries missing a
|
|
264
|
+
* name/baseUrl) would otherwise slip through `profilesFile.profiles || []` as
|
|
265
|
+
* an "empty" store and reopen the silent-wipe path. Returns the validated
|
|
266
|
+
* profiles array, or `null` when the value is not a valid profiles file.
|
|
267
|
+
*/
|
|
268
|
+
function extractValidProfiles(parsed) {
|
|
269
|
+
if (!isRecord(parsed))
|
|
270
|
+
return null;
|
|
271
|
+
const rawProfiles = parsed.profiles;
|
|
272
|
+
if (!Array.isArray(rawProfiles))
|
|
273
|
+
return null;
|
|
274
|
+
if (!rawProfiles.every(isValidProfile))
|
|
275
|
+
return null;
|
|
276
|
+
return rawProfiles;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Preserve the original bytes of a corrupt profiles.json in a content-addressed,
|
|
280
|
+
* mode-preserving backup (best-effort). Returns the backup path and whether it
|
|
281
|
+
* actually exists on disk afterwards.
|
|
282
|
+
*
|
|
283
|
+
* - Deduplicates: `loadProfiles` runs on virtually every CLI startup (via
|
|
284
|
+
* `loadSessionState`), so leaving the corrupt file in place would otherwise
|
|
285
|
+
* spew a fresh full copy on every command and eventually fill the disk. The
|
|
286
|
+
* backup name is the SHA-256 of the corrupt bytes, so a
|
|
287
|
+
* `profiles.json.corrupt-<sha256>` backup already holding the exact same
|
|
288
|
+
* bytes is reused instead of writing another.
|
|
289
|
+
* - Restrictive mode: the backup carries credentials, so it inherits the source
|
|
290
|
+
* file's permission bits (e.g. 0600) rather than the umask default — and is
|
|
291
|
+
* CREATED with that mode (or a 0600 fallback), never briefly world-readable.
|
|
292
|
+
* - Byte-exact: takes the raw `Buffer` read off disk (never a UTF-8-decoded
|
|
293
|
+
* string), so a torn write ending mid-multibyte-sequence is backed up and
|
|
294
|
+
* deduplicated by its true bytes, not lossily re-encoded replacement chars.
|
|
295
|
+
*/
|
|
296
|
+
function backupCorruptProfiles(profilesPath, data) {
|
|
297
|
+
// The mode a credential backup must carry: the source file's bits (e.g.
|
|
298
|
+
// 0600) or a restrictive 0600 fallback. Computed up front so a REUSED
|
|
299
|
+
// backup can be re-tightened too, not just a freshly created one.
|
|
300
|
+
const mode = fileMode(profilesPath) ?? 0o600;
|
|
301
|
+
// CONTENT-ADDRESSED canonical name: the backup filename is a pure function
|
|
302
|
+
// of the corrupt BYTES (their SHA-256), so identical corruption always maps
|
|
303
|
+
// to exactly ONE file. This is what bounds the credential copies across
|
|
304
|
+
// processes: N c8ctl children racing on the same corrupt profiles.json all
|
|
305
|
+
// derive the same name, and an EXCLUSIVE (`wx`) create lets exactly one win
|
|
306
|
+
// — every other loses with EEXIST and REUSES that single canonical backup,
|
|
307
|
+
// instead of each writing its own random-suffixed copy. A cross-process
|
|
308
|
+
// readdir/dedup scan could NOT prevent that fan-out (every child can finish
|
|
309
|
+
// scanning empty before any has written); a content-addressed name closes
|
|
310
|
+
// the race deterministically with no lock, and subsumes the old dedup scan
|
|
311
|
+
// for free (same bytes → same name).
|
|
312
|
+
const hash = createHash("sha256").update(data).digest("hex");
|
|
313
|
+
const backup = `${profilesPath}.corrupt-${hash}`;
|
|
314
|
+
// Stage the bytes in a private temp file FIRST, fully written and mode-pinned,
|
|
315
|
+
// then publish the canonical name with an atomic `link()`. This is what makes
|
|
316
|
+
// the canonical backup safe to reuse under concurrency: `link()` only ever
|
|
317
|
+
// exposes `backup` once it points at fully written content, so a losing racer
|
|
318
|
+
// that observes EEXIST reads COMPLETE bytes (never a half-finished write that
|
|
319
|
+
// would fail the equality check and fan out into a second copy). `link()`
|
|
320
|
+
// also refuses to follow or overwrite a pre-existing symlink at the canonical
|
|
321
|
+
// name, and the reuse read below opens with O_NOFOLLOW — so a symlink
|
|
322
|
+
// squatting the canonical path can neither redirect the reused read nor a
|
|
323
|
+
// chmod outside the profile store.
|
|
324
|
+
const tmp = `${profilesPath}.corrupt-tmp-${randomUUID()}`;
|
|
325
|
+
let stagedFd;
|
|
326
|
+
try {
|
|
327
|
+
// Create-exclusive (O_EXCL) + no-follow (O_NOFOLLOW) with a restrictive
|
|
328
|
+
// mode up front (never the 0666 umask default), so a credential temp is
|
|
329
|
+
// never briefly world-readable and a symlink can neither be followed nor
|
|
330
|
+
// pre-created at the temp path. Then write and re-tighten the exact mode
|
|
331
|
+
// (create mode is umask-masked) THROUGH THE FD via `fchmodSync` — never a
|
|
332
|
+
// pathname `chmodSync`, which a symlink swapped in after the file closed
|
|
333
|
+
// could redirect to a target outside the profile store.
|
|
334
|
+
stagedFd = openSync(tmp, constants.O_WRONLY |
|
|
335
|
+
constants.O_CREAT |
|
|
336
|
+
constants.O_EXCL |
|
|
337
|
+
constants.O_NOFOLLOW, mode);
|
|
338
|
+
writeFileSync(stagedFd, data);
|
|
339
|
+
pinFdMode(stagedFd, mode);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
// Could not even stage the temp copy — best-effort, give up cleanly.
|
|
343
|
+
if (stagedFd !== undefined)
|
|
344
|
+
closeQuietly(stagedFd);
|
|
345
|
+
removeQuietly(tmp);
|
|
346
|
+
return { backup, backedUp: false };
|
|
347
|
+
}
|
|
348
|
+
closeQuietly(stagedFd);
|
|
349
|
+
try {
|
|
350
|
+
// Publish under a BOUNDED, DETERMINISTIC set of content-addressed names:
|
|
351
|
+
// the canonical name first, then a single `.dup` fallback for the
|
|
352
|
+
// (astronomically unlikely) case where the canonical name is squatted by
|
|
353
|
+
// a non-matching or symlinked file. Both names are pure functions of the
|
|
354
|
+
// corrupt bytes, so repeated recoveries of the same corruption always map
|
|
355
|
+
// to the SAME (at most two) files and REUSE them — never a fresh
|
|
356
|
+
// random-suffixed copy per run. If BOTH names are unusable (e.g. an
|
|
357
|
+
// attacker squatting both with symlinks), we stop after preserving the
|
|
358
|
+
// original on disk rather than accumulate unbounded credential copies.
|
|
359
|
+
for (const name of [backup, `${backup}.dup`]) {
|
|
360
|
+
if (publishBackup(tmp, name, data, mode)) {
|
|
361
|
+
return { backup: name, backedUp: true };
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return { backup, backedUp: false };
|
|
365
|
+
}
|
|
366
|
+
finally {
|
|
367
|
+
// Drop the temp NAME. Whether we linked (content also at a published
|
|
368
|
+
// name) or gave up, unlinking the temp name never removes the
|
|
369
|
+
// hard-linked content it published elsewhere.
|
|
370
|
+
removeQuietly(tmp);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Publish the mode-pinned `tmp` under content-addressed `name` via an atomic
|
|
375
|
+
* `link()`, or REUSE `name` if it already holds these exact bytes. Returns
|
|
376
|
+
* `true` when `name` ends up as a usable regular-file backup of `data`, `false`
|
|
377
|
+
* when `name` is unusable (squatted by a symlink / non-matching file, or a
|
|
378
|
+
* non-EEXIST link error) so the caller can try the next name.
|
|
379
|
+
*
|
|
380
|
+
* `link()` never follows or overwrites a pre-existing symlink at `name`, and the
|
|
381
|
+
* hard link inherits `tmp`'s already-pinned mode — so no post-publish
|
|
382
|
+
* pathname-based chmod (which a symlink swap could redirect outside the store)
|
|
383
|
+
* is needed.
|
|
384
|
+
*/
|
|
385
|
+
function publishBackup(tmp, name, data, mode) {
|
|
386
|
+
try {
|
|
387
|
+
linkSync(tmp, name);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
catch (err) {
|
|
391
|
+
// EEXIST: the name already exists. Reuse it iff it is a regular file with
|
|
392
|
+
// these exact bytes (and a repairable mode); otherwise it is unusable.
|
|
393
|
+
if (isRecord(err) && err.code === "EEXIST") {
|
|
394
|
+
return reuseCanonicalBackup(name, data, mode);
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
/** Best-effort unlink that swallows ENOENT and any other error. */
|
|
400
|
+
function removeQuietly(path) {
|
|
401
|
+
try {
|
|
402
|
+
rmSync(path, { force: true });
|
|
403
|
+
}
|
|
404
|
+
catch {
|
|
405
|
+
/* best-effort cleanup */
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Does the path ENTRY itself exist, without following a final symlink? Uses
|
|
410
|
+
* `lstatSync`, so a DANGLING symlink (link present, target absent) counts as
|
|
411
|
+
* "exists" — distinguishing a broken/unreadable profiles path from a genuinely
|
|
412
|
+
* absent one when a `readFileSync` returns ENOENT.
|
|
413
|
+
*/
|
|
414
|
+
function pathEntryExists(path) {
|
|
415
|
+
try {
|
|
416
|
+
lstatSync(path);
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
catch (err) {
|
|
420
|
+
// Only an ENOENT from `lstatSync` PROVES the entry is genuinely absent.
|
|
421
|
+
// Any OTHER failure (ENOTDIR, EACCES, EIO…) means the path is present but
|
|
422
|
+
// unstattable — e.g. on Windows a data-directory path that is a regular
|
|
423
|
+
// file can yield ENOENT from `readFileSync` yet ENOTDIR from `lstatSync`.
|
|
424
|
+
// Treat those as "exists/unreadable" so `loadProfiles` routes them to the
|
|
425
|
+
// protected throw path instead of seeding over an unreadable store.
|
|
426
|
+
if (isRecord(err) && err.code === "ENOENT")
|
|
427
|
+
return false;
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Reuse an existing content-addressed canonical backup iff it is a REGULAR file
|
|
433
|
+
* whose bytes match `data`. Opens with O_NOFOLLOW and operates on the resulting
|
|
434
|
+
* fd (fstat/read/fchmod), so a symlink squatting the canonical name is rejected
|
|
435
|
+
* (ELOOP) and can never redirect the read or a chmod to a target outside the
|
|
436
|
+
* profile store. Re-tightens a stale/world-readable mode on the same fd (an
|
|
437
|
+
* older run may have created it 0644 before profiles.json was locked to 0600).
|
|
438
|
+
*/
|
|
439
|
+
function reuseCanonicalBackup(backup, data, mode) {
|
|
440
|
+
let fd;
|
|
441
|
+
try {
|
|
442
|
+
// O_NONBLOCK so a canonical name pre-created as a FIFO (or other blocking
|
|
443
|
+
// special file) can NEVER hang this open indefinitely waiting for a peer
|
|
444
|
+
// — with O_NONBLOCK the open returns at once, `fstatSync` sees a
|
|
445
|
+
// non-regular entry, and we reject it below. For a regular file
|
|
446
|
+
// O_NONBLOCK is a no-op (reads are unaffected), and we never `readFileSync`
|
|
447
|
+
// a candidate that failed the `isFile()` gate, so a FIFO is never read.
|
|
448
|
+
fd = openSync(backup, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
449
|
+
const st = fstatSync(fd);
|
|
450
|
+
if (!st.isFile())
|
|
451
|
+
return false; // reject symlink target / non-regular / FIFO
|
|
452
|
+
// Reject a HARD-LINK squat: O_NOFOLLOW blocks symlinks but not a
|
|
453
|
+
// pre-existing hard link to a same-byte file OUTSIDE this directory —
|
|
454
|
+
// reusing it would report a foreign inode as our backup and let the
|
|
455
|
+
// `fchmodSync` below change that outside inode's permissions. A backup we
|
|
456
|
+
// published (link → unlink temp) settles at nlink === 1, so anything with
|
|
457
|
+
// extra links is not an independently owned copy. (A racer reusing the
|
|
458
|
+
// canonical name in the microscopic window before the publisher unlinks
|
|
459
|
+
// its temp may see nlink === 2 and fall through to the bounded `.dup`
|
|
460
|
+
// name — a spurious second file at worst, never an unbounded fan-out.)
|
|
461
|
+
if (st.nlink !== 1)
|
|
462
|
+
return false;
|
|
463
|
+
if (!readFileSync(fd).equals(data))
|
|
464
|
+
return false;
|
|
465
|
+
if ((st.mode & 0o777) !== mode) {
|
|
466
|
+
try {
|
|
467
|
+
pinFdMode(fd, mode);
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
// Could not re-tighten a stale/world-readable candidate. Do NOT
|
|
471
|
+
// claim it as a safe backup — reporting it as reused would leave
|
|
472
|
+
// credentials at a wider mode than promised. Return false so the
|
|
473
|
+
// caller publishes a freshly mode-pinned copy under the next name.
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
catch {
|
|
480
|
+
// ELOOP (symlink, blocked by O_NOFOLLOW), ENOENT, unreadable, etc.
|
|
481
|
+
return false;
|
|
482
|
+
}
|
|
483
|
+
finally {
|
|
484
|
+
if (fd !== undefined)
|
|
485
|
+
closeQuietly(fd);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
/** Best-effort fd close that swallows any error. */
|
|
489
|
+
function closeQuietly(fd) {
|
|
490
|
+
try {
|
|
491
|
+
closeSync(fd);
|
|
492
|
+
}
|
|
493
|
+
catch {
|
|
494
|
+
/* best-effort close */
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** Back up the corrupt file (best-effort) and throw a recoverable error. */
|
|
498
|
+
function throwCorruptProfiles(profilesPath, data, detail) {
|
|
499
|
+
const { backup, backedUp } = backupCorruptProfiles(profilesPath, data);
|
|
500
|
+
const backupNote = backedUp
|
|
501
|
+
? `Its contents were backed up to ${backup}. `
|
|
502
|
+
: `A backup could not be written, but the original file is still on disk untouched. `;
|
|
503
|
+
throw new Error(`profiles.json at ${profilesPath} is corrupt (${detail}). ${backupNote}` +
|
|
504
|
+
`Refusing to overwrite it to avoid destroying your saved profiles — ` +
|
|
505
|
+
`fix or remove the file, then re-run.`);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Load c8ctl profiles from profiles.json.
|
|
509
|
+
*
|
|
510
|
+
* A MISSING file legitimately means "no profiles yet" → `[]`. But a file that
|
|
511
|
+
* exists and cannot be read/parsed is NOT empty — it is corrupt or transiently
|
|
512
|
+
* unreadable (e.g. a torn write from a crash or two c8ctl processes racing).
|
|
513
|
+
* Returning `[]` in that case is catastrophic: the very next `saveProfiles`
|
|
514
|
+
* (via `addProfile`/`ensureDefaultProfile`/`removeProfile`) would overwrite the
|
|
515
|
+
* file with a truncated set, silently DESTROYING every saved profile. So we
|
|
516
|
+
* distinguish the two: absent → `[]`; present-but-unreadable → THROW (after
|
|
517
|
+
* preserving the original bytes in a content-addressed backup), so no caller can
|
|
518
|
+
* mistake corruption for emptiness and clobber the user's profiles.
|
|
187
519
|
*/
|
|
188
520
|
export function loadProfiles() {
|
|
189
521
|
const profilesPath = getProfilesPath();
|
|
190
|
-
|
|
191
|
-
|
|
522
|
+
let data;
|
|
523
|
+
let fd;
|
|
524
|
+
try {
|
|
525
|
+
// Open the PRIMARY profiles path NON-BLOCKING before reading: a
|
|
526
|
+
// profiles.json pre-created as a FIFO (or other blocking special file)
|
|
527
|
+
// would make a plain `readFileSync` hang startup FOREVER waiting for a
|
|
528
|
+
// writer. With O_NONBLOCK the open returns at once and the `fstatSync`
|
|
529
|
+
// gate below rejects a non-regular entry before any read — mirroring the
|
|
530
|
+
// canonical-backup reuse path. O_NONBLOCK is a no-op for regular files
|
|
531
|
+
// (reads are unaffected). We deliberately DO follow symlinks here (no
|
|
532
|
+
// O_NOFOLLOW): reading THROUGH a valid symlink to a regular file is the
|
|
533
|
+
// documented behaviour, and a DANGLING symlink still surfaces as ENOENT
|
|
534
|
+
// and is handled below.
|
|
535
|
+
fd = openSync(profilesPath, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
536
|
+
const st = fstatSync(fd);
|
|
537
|
+
if (!st.isFile()) {
|
|
538
|
+
// A non-regular entry (FIFO, device, socket, directory) squats the
|
|
539
|
+
// profiles path. This is NOT "no profiles yet" — refuse to proceed so
|
|
540
|
+
// a later save cannot replace it and silently lose the user's intent.
|
|
541
|
+
throw new Error(`profiles file at ${profilesPath} is not a regular file (e.g. a ` +
|
|
542
|
+
`FIFO, device, or directory). Refusing to treat it as empty (that ` +
|
|
543
|
+
`would overwrite your saved profiles) — fix or remove it, then ` +
|
|
544
|
+
`re-run.`);
|
|
545
|
+
}
|
|
546
|
+
data = readFileSync(fd);
|
|
547
|
+
}
|
|
548
|
+
catch (err) {
|
|
549
|
+
// A MISSING file (ENOENT) legitimately means "no profiles yet" → [].
|
|
550
|
+
// Any OTHER read error (permissions, a transiently inaccessible parent,
|
|
551
|
+
// an I/O fault) is NOT emptiness: `existsSync` can't tell these apart —
|
|
552
|
+
// it returns false for an unstattable path too — so we key strictly on
|
|
553
|
+
// ENOENT and route every other failure to the protected throw path, so
|
|
554
|
+
// seeding can never proceed from a false "empty" state and clobber the
|
|
555
|
+
// user's saved profiles.
|
|
556
|
+
if (isRecord(err) && err.code === "ENOENT") {
|
|
557
|
+
// `openSync` also returns ENOENT for a DANGLING symlink (the link
|
|
558
|
+
// exists, its target does not). That is NOT "no profiles yet": a
|
|
559
|
+
// later `saveProfiles` rename would replace the link and the user's
|
|
560
|
+
// intended target is lost. `lstatSync` inspects the path entry
|
|
561
|
+
// ITSELF without following it — if it succeeds, something is present
|
|
562
|
+
// (a broken symlink or other non-regular entry) and we must NOT treat
|
|
563
|
+
// it as empty. Only a genuinely absent entry (lstat also ENOENT)
|
|
564
|
+
// means "no profiles yet".
|
|
565
|
+
if (pathEntryExists(profilesPath)) {
|
|
566
|
+
throw new Error(`profiles file at ${profilesPath} exists but its contents could not ` +
|
|
567
|
+
`be read (e.g. a dangling symlink). Refusing to treat it as empty ` +
|
|
568
|
+
`(that would overwrite your saved profiles) — fix or remove it, ` +
|
|
569
|
+
`then re-run.`);
|
|
570
|
+
}
|
|
571
|
+
return [];
|
|
572
|
+
}
|
|
573
|
+
// A well-formed error we threw ourselves (the non-regular-entry gate
|
|
574
|
+
// above) carries no fs `code` — surface it verbatim rather than
|
|
575
|
+
// re-wrapping it as a raw read failure.
|
|
576
|
+
if (!(isRecord(err) && typeof err.code === "string")) {
|
|
577
|
+
throw err;
|
|
578
|
+
}
|
|
579
|
+
throw new Error(`Failed to read profiles at ${profilesPath}: ${err instanceof Error ? err.message : String(err)}. ` +
|
|
580
|
+
`Refusing to treat an unreadable profiles file as empty (that would ` +
|
|
581
|
+
`overwrite your saved profiles).`);
|
|
582
|
+
}
|
|
583
|
+
finally {
|
|
584
|
+
if (fd !== undefined)
|
|
585
|
+
closeQuietly(fd);
|
|
192
586
|
}
|
|
193
587
|
try {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
588
|
+
// Decode with FATAL UTF-8: a lenient `Buffer.toString("utf-8")` silently
|
|
589
|
+
// replaces malformed bytes with U+FFFD, so invalid bytes inside a quoted
|
|
590
|
+
// JSON value would still `JSON.parse` and validate as a healthy profile —
|
|
591
|
+
// letting a later save rewrite the file and lose the original bytes with
|
|
592
|
+
// no `.corrupt-*` backup. Route a decode failure through the same backup
|
|
593
|
+
// + throw path as a parse failure.
|
|
594
|
+
let text;
|
|
595
|
+
try {
|
|
596
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(data);
|
|
597
|
+
}
|
|
598
|
+
catch {
|
|
599
|
+
throwCorruptProfiles(profilesPath, data, "contains invalid UTF-8 byte sequences");
|
|
600
|
+
}
|
|
601
|
+
const parsed = JSON.parse(text);
|
|
602
|
+
const profiles = extractValidProfiles(parsed);
|
|
603
|
+
if (profiles === null) {
|
|
604
|
+
// Syntactically valid JSON, but not a valid profiles file. Treating
|
|
605
|
+
// this as "no profiles" would let a subsequent save clobber it, so
|
|
606
|
+
// back it up and throw exactly as for an unparseable file.
|
|
607
|
+
throwCorruptProfiles(profilesPath, data, 'does not contain a valid "profiles" array');
|
|
608
|
+
}
|
|
609
|
+
return profiles;
|
|
197
610
|
}
|
|
198
|
-
catch {
|
|
199
|
-
|
|
611
|
+
catch (err) {
|
|
612
|
+
if (err instanceof SyntaxError) {
|
|
613
|
+
// The file exists but is corrupt/torn. Preserve the original bytes in
|
|
614
|
+
// a backup (best-effort) so nothing is lost, then throw — never treat
|
|
615
|
+
// corruption as "no profiles", which would let a subsequent save
|
|
616
|
+
// clobber the file.
|
|
617
|
+
throwCorruptProfiles(profilesPath, data, `could not be parsed: ${err.message}`);
|
|
618
|
+
}
|
|
619
|
+
throw err;
|
|
200
620
|
}
|
|
201
621
|
}
|
|
202
622
|
/**
|
|
203
|
-
* Save c8ctl profiles to profiles.json
|
|
623
|
+
* Save c8ctl profiles to profiles.json ATOMICALLY.
|
|
624
|
+
*
|
|
625
|
+
* A bare `writeFileSync` truncates the target and then streams the new bytes, so
|
|
626
|
+
* a crash, a full disk, or a second c8ctl process reading mid-write can observe
|
|
627
|
+
* a TORN file — which `loadProfiles` then can't parse, and (pre-fix) treated as
|
|
628
|
+
* "no profiles", cascading into a total wipe. Writing to a same-directory temp
|
|
629
|
+
* file and `rename`-ing over the target makes the swap atomic: a reader sees
|
|
630
|
+
* either the whole old file or the whole new one, never a partial one.
|
|
204
631
|
*/
|
|
205
632
|
export function saveProfiles(profiles) {
|
|
206
633
|
const profilesPath = getProfilesPath();
|
|
207
634
|
const profilesFile = { profiles };
|
|
208
|
-
|
|
635
|
+
// Randomise the temp name and open it EXCLUSIVE + no-follow (O_EXCL |
|
|
636
|
+
// O_NOFOLLOW) below. The old `<pid>.<ms>.tmp` name was predictable, so in a
|
|
637
|
+
// writable/shared data directory an attacker could pre-create a symlink at
|
|
638
|
+
// that path; a default truncating open would then FOLLOW it and redirect the
|
|
639
|
+
// serialized credentials outside the profile store. A random name is
|
|
640
|
+
// unguessable and O_EXCL|O_NOFOLLOW refuses to open an existing entry
|
|
641
|
+
// (symlink included), so the temp file can neither be redirected nor
|
|
642
|
+
// clobbered by another entry.
|
|
643
|
+
const tmp = `${profilesPath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
|
|
644
|
+
// Preserve the existing file's permission bits (e.g. 0600) so an atomic
|
|
645
|
+
// replace never silently widens a user-protected profiles.json — it holds
|
|
646
|
+
// client secrets and passwords — to the umask default.
|
|
647
|
+
const mode = fileMode(profilesPath);
|
|
648
|
+
let fd;
|
|
649
|
+
try {
|
|
650
|
+
// Create-exclusive (O_EXCL) + no-follow (O_NOFOLLOW) with a restrictive
|
|
651
|
+
// mode up front (never the 0666 umask default): a crash between write and
|
|
652
|
+
// rename otherwise leaves a `.tmp` holding credentials with broader
|
|
653
|
+
// permissions, and O_EXCL never truncates/follows a pre-existing entry
|
|
654
|
+
// (symlink included). Write and re-tighten the exact source mode THROUGH
|
|
655
|
+
// THE FD via `fchmodSync` (create mode is umask-masked) — never a pathname
|
|
656
|
+
// `chmodSync`, which a symlink swapped in after the file closed could
|
|
657
|
+
// redirect outside the profile store. Close before the rename so Windows
|
|
658
|
+
// (which refuses to rename an open file) can swap it atomically.
|
|
659
|
+
fd = openSync(tmp, constants.O_WRONLY |
|
|
660
|
+
constants.O_CREAT |
|
|
661
|
+
constants.O_EXCL |
|
|
662
|
+
constants.O_NOFOLLOW, mode ?? 0o600);
|
|
663
|
+
writeFileSync(fd, JSON.stringify(profilesFile, null, 2));
|
|
664
|
+
if (mode !== undefined)
|
|
665
|
+
pinFdMode(fd, mode);
|
|
666
|
+
closeSync(fd);
|
|
667
|
+
fd = undefined;
|
|
668
|
+
renameSync(tmp, profilesPath);
|
|
669
|
+
}
|
|
670
|
+
catch (err) {
|
|
671
|
+
// Any failure — including the initial write (e.g. a full disk) — must not
|
|
672
|
+
// leave the temp file (or its fd) behind.
|
|
673
|
+
if (fd !== undefined)
|
|
674
|
+
closeQuietly(fd);
|
|
675
|
+
try {
|
|
676
|
+
rmSync(tmp, { force: true });
|
|
677
|
+
}
|
|
678
|
+
catch {
|
|
679
|
+
/* best effort */
|
|
680
|
+
}
|
|
681
|
+
throw err;
|
|
682
|
+
}
|
|
209
683
|
}
|
|
210
684
|
/**
|
|
211
685
|
* Get a c8ctl profile by name
|
|
@@ -439,9 +913,23 @@ export const DEFAULT_PROFILE_CONFIG = {
|
|
|
439
913
|
/**
|
|
440
914
|
* Ensure the default 'local' profile exists in profiles.json.
|
|
441
915
|
* If no 'local' profile is configured, creates one with the localhost defaults.
|
|
916
|
+
*
|
|
917
|
+
* Runs on virtually every command (via `loadSessionState`). It MUST NOT be the
|
|
918
|
+
* thing that destroys a user's profiles: if `getProfile` throws because
|
|
919
|
+
* profiles.json is present but unreadable/corrupt, seeding here would reload the
|
|
920
|
+
* (still-unreadable) file, see zero profiles, and overwrite it — wiping every
|
|
921
|
+
* saved profile. So on a read failure we warn and leave the file untouched
|
|
922
|
+
* rather than reseed. `loadProfiles` already preserved the original bytes.
|
|
442
923
|
*/
|
|
443
924
|
export function ensureDefaultProfile() {
|
|
444
|
-
|
|
925
|
+
let existing;
|
|
926
|
+
try {
|
|
927
|
+
existing = getProfile(DEFAULT_PROFILE);
|
|
928
|
+
}
|
|
929
|
+
catch (err) {
|
|
930
|
+
getLogger().warn(`Skipping default-profile seeding: ${err instanceof Error ? err.message : String(err)}`);
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
445
933
|
if (!existing) {
|
|
446
934
|
addProfile({ ...DEFAULT_PROFILE_CONFIG });
|
|
447
935
|
}
|