@kaynooo/cli-env 0.8.1 → 0.9.1
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/README.md +6 -4
- package/dist/ev.js +200 -76
- package/dist/redact.js +91 -28
- package/package.json +7 -4
- package/src/audit.ts +167 -0
- package/src/commands/audit.ts +135 -0
- package/src/commands/compare.ts +77 -0
- package/src/commands/edit.ts +181 -0
- package/src/commands/pins.ts +98 -0
- package/src/commands/read.ts +193 -0
- package/src/dotenv.ts +8 -4
- package/src/ev.ts +31 -0
- package/src/files.ts +242 -0
- package/src/fix.ts +294 -0
- package/src/guard-dumps.ts +114 -0
- package/src/guard-names.ts +107 -0
- package/src/guard-paths.ts +80 -0
- package/src/guard-pipes.ts +76 -0
- package/src/guard-search.ts +181 -0
- package/src/guard.ts +51 -0
- package/src/mask.ts +264 -0
- package/src/pins.ts +268 -0
- package/src/redact.ts +43 -0
- package/src/redactor.ts +166 -0
package/dist/redact.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/redact.ts
|
|
5
5
|
import process10 from "process";
|
|
6
|
-
|
|
7
6
|
// ../kit/src/args.ts
|
|
8
7
|
import process6 from "process";
|
|
9
8
|
import { parseArgs } from "util";
|
|
@@ -366,17 +365,23 @@ var FULL_STOP = /\.$/;
|
|
|
366
365
|
function usageLine(usage, colors = paint(process4.stdout)) {
|
|
367
366
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
368
367
|
}
|
|
369
|
-
function helpText(usage, options, colors = paint(process4.stdout), { examples = [], replaces } = {}) {
|
|
370
|
-
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exampleRows(examples, colors)].join(`
|
|
368
|
+
function helpText(usage, options, colors = paint(process4.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
369
|
+
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
371
370
|
`);
|
|
372
371
|
}
|
|
372
|
+
function exitRows(exits, colors = paint(process4.stdout)) {
|
|
373
|
+
if (exits.length === 0)
|
|
374
|
+
return [];
|
|
375
|
+
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
376
|
+
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
377
|
+
}
|
|
373
378
|
function replacesRows(replaces, colors = paint(process4.stdout)) {
|
|
374
379
|
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
375
380
|
}
|
|
376
381
|
function wantsHelpJson(env = process4.env) {
|
|
377
382
|
return env.KIT_HELP === "json";
|
|
378
383
|
}
|
|
379
|
-
function description(usage, { examples = [], replaces } = {}) {
|
|
384
|
+
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
380
385
|
const lines = usage.replace(USAGE_PREFIX, "").split(`
|
|
381
386
|
`);
|
|
382
387
|
const first = lines[0];
|
|
@@ -387,7 +392,9 @@ function description(usage, { examples = [], replaces } = {}) {
|
|
|
387
392
|
synopsis: colon === -1 ? first : first.slice(0, colon),
|
|
388
393
|
summary: colon === -1 ? fallback : first.slice(colon + 2),
|
|
389
394
|
...replaces === undefined ? {} : { replaces },
|
|
390
|
-
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] })
|
|
395
|
+
examples: examples.map((example) => typeof example === "string" ? { line: example } : { line: example[0], note: example[1] }),
|
|
396
|
+
...exits === undefined ? {} : { exits: exits.map(([code, meaning]) => ({ code, meaning })) },
|
|
397
|
+
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
391
398
|
};
|
|
392
399
|
}
|
|
393
400
|
function exampleRows(examples, colors = paint(process4.stdout)) {
|
|
@@ -512,7 +519,7 @@ function parse(args, usage, options, settings = {}) {
|
|
|
512
519
|
const { own, rest } = settings.trailing === true ? splitTrailing(args, all, settings.positionals?.length ?? 0) : splitRest(args, settings.rest === true);
|
|
513
520
|
let parsed;
|
|
514
521
|
try {
|
|
515
|
-
parsed = parseArgs({ args: joinDashValues(withBare(own, all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
522
|
+
parsed = parseArgs({ args: joinDashValues(withBare(withAliases(own, all), all), all), options: all, allowPositionals: true, allowNegative: settings.allowNegative === true, strict: true });
|
|
516
523
|
} catch (error) {
|
|
517
524
|
const message = parseError(error);
|
|
518
525
|
throw new UsageError(message, nearestOption(message, all, settings.allowNegative === true));
|
|
@@ -520,7 +527,7 @@ function parse(args, usage, options, settings = {}) {
|
|
|
520
527
|
const values = parsed.values;
|
|
521
528
|
applyOutput("color" in output ? values.color : undefined, "plain" in output ? values.plain : undefined);
|
|
522
529
|
if (values.help === true) {
|
|
523
|
-
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings)) : helpText(usage, listing, undefined, settings));
|
|
530
|
+
console.log(wantsHelpJson() ? JSON.stringify(description(usage, settings, listing)) : helpText(usage, listing, undefined, settings));
|
|
524
531
|
throw new HelpShown;
|
|
525
532
|
}
|
|
526
533
|
checkChoices(parsed.values, all);
|
|
@@ -562,6 +569,18 @@ function listed(options, allowNegative) {
|
|
|
562
569
|
return [...option.description === undefined ? [] : [[name, option]], [`no-${name}`, { type: "boolean", description: option.negative }]];
|
|
563
570
|
}));
|
|
564
571
|
}
|
|
572
|
+
function withAliases(args, options) {
|
|
573
|
+
const named = new Map(Object.entries(options).flatMap(([name, { aliases = [] }]) => aliases.map((alias) => [`--${alias}`, `--${name}`])));
|
|
574
|
+
if (named.size === 0)
|
|
575
|
+
return args;
|
|
576
|
+
const separator = args.indexOf("--");
|
|
577
|
+
const own = separator === -1 ? args : args.slice(0, separator);
|
|
578
|
+
return [...own.map((arg) => {
|
|
579
|
+
const at = arg.indexOf("=");
|
|
580
|
+
const current = named.get(at === -1 ? arg : arg.slice(0, at));
|
|
581
|
+
return current === undefined ? arg : `${current}${at === -1 ? "" : arg.slice(at)}`;
|
|
582
|
+
}), ...separator === -1 ? [] : args.slice(separator)];
|
|
583
|
+
}
|
|
565
584
|
function withBare(args, options) {
|
|
566
585
|
const bare = new Map(Object.entries(options).flatMap(([name, { short, optional }]) => optional === undefined ? [] : [[`--${name}`, optional], ...short === undefined ? [] : [[`-${short}`, optional]]]));
|
|
567
586
|
if (bare.size === 0)
|
|
@@ -652,6 +671,8 @@ var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x
|
|
|
652
671
|
var RESET_STYLE = `${ESC}[0m`;
|
|
653
672
|
var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
654
673
|
// ../kit/src/cli.ts
|
|
674
|
+
import { readFileSync, realpathSync } from "fs";
|
|
675
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
655
676
|
import process8 from "process";
|
|
656
677
|
|
|
657
678
|
// ../kit/src/usage.ts
|
|
@@ -948,10 +969,22 @@ async function printCandidates(found) {
|
|
|
948
969
|
console.log(line);
|
|
949
970
|
return 0;
|
|
950
971
|
}
|
|
972
|
+
function packageVersion(entry = Bun.main) {
|
|
973
|
+
for (let dir = dirname2(realpathSync(entry));; dir = dirname2(dir)) {
|
|
974
|
+
try {
|
|
975
|
+
const { version } = JSON.parse(readFileSync(join2(dir, "package.json"), "utf8"));
|
|
976
|
+
if (typeof version === "string")
|
|
977
|
+
return version;
|
|
978
|
+
} catch {}
|
|
979
|
+
if (dirname2(dir) === dir)
|
|
980
|
+
return "0.0.0";
|
|
981
|
+
}
|
|
982
|
+
}
|
|
951
983
|
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
984
|
+
const version = cli.version ?? packageVersion();
|
|
952
985
|
if (argv[0] === COMPLETE)
|
|
953
986
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
954
|
-
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(
|
|
987
|
+
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
955
988
|
if (cli.usage !== false) {
|
|
956
989
|
try {
|
|
957
990
|
recordUsage(usageEntry(cli.name, commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv), argv, code));
|
|
@@ -978,7 +1011,7 @@ async function runMain({ name, version, run, redact, usage }, argv = process8.ar
|
|
|
978
1011
|
if (argv[0] === COMPLETE)
|
|
979
1012
|
return printCandidates(leafCandidates(run, argv.slice(1)));
|
|
980
1013
|
if (argv.length === 1 && (argv[0] === "--version" || argv[0] === "-v")) {
|
|
981
|
-
console.log(version);
|
|
1014
|
+
console.log(version ?? packageVersion());
|
|
982
1015
|
return 0;
|
|
983
1016
|
}
|
|
984
1017
|
let code;
|
|
@@ -987,10 +1020,22 @@ async function runMain({ name, version, run, redact, usage }, argv = process8.ar
|
|
|
987
1020
|
} catch (error) {
|
|
988
1021
|
code = reportThrown(name, error, redact, wantsJson(argv));
|
|
989
1022
|
}
|
|
990
|
-
if (usage !== false)
|
|
991
|
-
|
|
1023
|
+
if (usage !== false) {
|
|
1024
|
+
try {
|
|
1025
|
+
recordUsage(usageEntry(name, undefined, argv, code));
|
|
1026
|
+
} catch {}
|
|
1027
|
+
}
|
|
992
1028
|
return code;
|
|
993
1029
|
}
|
|
1030
|
+
// ../kit/src/compose.ts
|
|
1031
|
+
async function captureOutput(argv, { cwd, env } = {}) {
|
|
1032
|
+
const run = Bun.spawn(argv, { ...cwd === undefined ? {} : { cwd }, env: { ...process.env, ...env }, stdin: "ignore", stdout: "pipe", stderr: "pipe" });
|
|
1033
|
+
const [stdout, stderr, code] = await Promise.all([run.stdout.text(), run.stderr.text(), run.exited]);
|
|
1034
|
+
return { code, stdout, stderr };
|
|
1035
|
+
}
|
|
1036
|
+
async function compose(args, { flags = [], ...options } = {}) {
|
|
1037
|
+
return captureOutput(["docker", "compose", ...flags, ...args], options);
|
|
1038
|
+
}
|
|
994
1039
|
// ../kit/src/secrets.ts
|
|
995
1040
|
var SECRET_PART = /token|secret|passw|credential|private|dsn|apikey|accesskey|key$/i;
|
|
996
1041
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
@@ -1002,12 +1047,9 @@ function isSecretName(name) {
|
|
|
1002
1047
|
var {Glob } = globalThis.Bun;
|
|
1003
1048
|
var SESSION_FILES = new Glob("*.json");
|
|
1004
1049
|
var ACCOUNT_DIRS = new Glob("*/sessions");
|
|
1005
|
-
// package.json
|
|
1006
|
-
var version = "0.8.1";
|
|
1007
|
-
|
|
1008
1050
|
// src/files.ts
|
|
1009
1051
|
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from "fs/promises";
|
|
1010
|
-
import { basename, dirname as
|
|
1052
|
+
import { basename, dirname as dirname3, join as join3 } from "path";
|
|
1011
1053
|
|
|
1012
1054
|
// src/dotenv.ts
|
|
1013
1055
|
var ASSIGNMENT = /^\s*(?:export\s+)?([A-Z_][\w.-]*)\s*=(.*)$/i;
|
|
@@ -1090,14 +1132,16 @@ function entries(file) {
|
|
|
1090
1132
|
}
|
|
1091
1133
|
return map;
|
|
1092
1134
|
}
|
|
1135
|
+
function escapeDouble(value) {
|
|
1136
|
+
return value.replaceAll("\\", "\\\\").replaceAll('"', "\\\"").replaceAll("\r", "\\r").replaceAll(`
|
|
1137
|
+
`, "\\n").replaceAll("$", "\\$");
|
|
1138
|
+
}
|
|
1093
1139
|
function formatAssignment(key, value) {
|
|
1094
1140
|
if (value === "" || !NEEDS_QUOTES.test(value))
|
|
1095
1141
|
return `${key}=${value}`;
|
|
1096
1142
|
if (RAW_ONLY.test(value) && !value.includes("'") && !LINE_BREAK.test(value))
|
|
1097
1143
|
return `${key}='${value.replaceAll("$", "\\$")}'`;
|
|
1098
|
-
|
|
1099
|
-
`, "\\n").replaceAll("$", "\\$");
|
|
1100
|
-
return `${key}="${escaped}"`;
|
|
1144
|
+
return `${key}="${escapeDouble(value)}"`;
|
|
1101
1145
|
}
|
|
1102
1146
|
function setLine(file, key, value, raw) {
|
|
1103
1147
|
const index = file.lines.findLastIndex((line) => line.key === key);
|
|
@@ -1166,6 +1210,9 @@ var WEBHOOK_FORMATS = [
|
|
|
1166
1210
|
["healthchecks.io ping URL", /^hc-ping\.com\/([\w-]+)/di]
|
|
1167
1211
|
];
|
|
1168
1212
|
var OPAQUE_SEGMENT = /^(?:(?=[\w-]{20,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\w-]+|(?=[A-Za-z\d]{32,}$)(?=.*[A-Za-z])(?=.*\d).+)$/;
|
|
1213
|
+
function isSecretKey(key) {
|
|
1214
|
+
return isSecretName(key);
|
|
1215
|
+
}
|
|
1169
1216
|
function hint(value) {
|
|
1170
1217
|
const shown = Math.min(3, Math.floor(value.length / 4));
|
|
1171
1218
|
return `${value.slice(0, shown)}***(${value.length})`;
|
|
@@ -1298,6 +1345,7 @@ var KNOWN_DEPTH = 3;
|
|
|
1298
1345
|
var SKIPPED = new Set(["node_modules", ".git", "vendor"]);
|
|
1299
1346
|
var LOCK_WAIT_MS = 5000;
|
|
1300
1347
|
var LOCK_STALE_MS = 30000;
|
|
1348
|
+
var REAP_STALE_MS = 5000;
|
|
1301
1349
|
async function isFileEntry(entry, path) {
|
|
1302
1350
|
if (entry.isFile())
|
|
1303
1351
|
return true;
|
|
@@ -1313,7 +1361,7 @@ async function envFiles(folder = process.cwd()) {
|
|
|
1313
1361
|
const found = await readdir(folder, { withFileTypes: true });
|
|
1314
1362
|
const names = [];
|
|
1315
1363
|
for (const entry of found) {
|
|
1316
|
-
if (ENV_FILE.test(entry.name) && await isFileEntry(entry,
|
|
1364
|
+
if (ENV_FILE.test(entry.name) && await isFileEntry(entry, join3(folder, entry.name)))
|
|
1317
1365
|
names.push(entry.name);
|
|
1318
1366
|
}
|
|
1319
1367
|
return names.sort();
|
|
@@ -1337,7 +1385,7 @@ async function writeAtomic(path, text) {
|
|
|
1337
1385
|
if (error.code !== "ENOENT")
|
|
1338
1386
|
throw error;
|
|
1339
1387
|
}
|
|
1340
|
-
const temp =
|
|
1388
|
+
const temp = join3(dirname3(target), `.${basename(target)}.${process.pid}.${crypto.randomUUID()}.tmp`);
|
|
1341
1389
|
try {
|
|
1342
1390
|
await writeFile(temp, text, { mode: 384, flag: "wx" });
|
|
1343
1391
|
if (owner !== undefined)
|
|
@@ -1370,12 +1418,30 @@ async function withLocks(paths, fn) {
|
|
|
1370
1418
|
await rm(lock, { force: true });
|
|
1371
1419
|
}
|
|
1372
1420
|
}
|
|
1421
|
+
async function reap(lock) {
|
|
1422
|
+
const guard = `${lock}.reap`;
|
|
1423
|
+
try {
|
|
1424
|
+
await writeFile(guard, String(process.pid), { flag: "wx", mode: 384 });
|
|
1425
|
+
} catch {
|
|
1426
|
+
try {
|
|
1427
|
+
if (Date.now() - (await stat(guard)).mtimeMs > REAP_STALE_MS)
|
|
1428
|
+
await rm(guard, { force: true });
|
|
1429
|
+
} catch {}
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
try {
|
|
1433
|
+
if (Date.now() - (await stat(lock)).mtimeMs > LOCK_STALE_MS)
|
|
1434
|
+
await rm(lock, { force: true });
|
|
1435
|
+
} catch {} finally {
|
|
1436
|
+
await rm(guard, { force: true });
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1373
1439
|
async function acquire(path) {
|
|
1374
1440
|
let real = path;
|
|
1375
1441
|
try {
|
|
1376
1442
|
real = await realpath(path);
|
|
1377
1443
|
} catch {}
|
|
1378
|
-
const lock =
|
|
1444
|
+
const lock = join3(dirname3(real), `.${basename(real)}.lock`);
|
|
1379
1445
|
const deadline = Date.now() + LOCK_WAIT_MS;
|
|
1380
1446
|
while (true) {
|
|
1381
1447
|
try {
|
|
@@ -1385,10 +1451,7 @@ async function acquire(path) {
|
|
|
1385
1451
|
if (error.code !== "EEXIST")
|
|
1386
1452
|
throw error;
|
|
1387
1453
|
}
|
|
1388
|
-
|
|
1389
|
-
if (Date.now() - (await stat(lock)).mtimeMs > LOCK_STALE_MS)
|
|
1390
|
-
await rm(lock, { force: true });
|
|
1391
|
-
} catch {}
|
|
1454
|
+
await reap(lock);
|
|
1392
1455
|
if (Date.now() > deadline)
|
|
1393
1456
|
fail(`${path} is locked by another ev run (${lock})`, 1);
|
|
1394
1457
|
await Bun.sleep(25);
|
|
@@ -1413,7 +1476,7 @@ async function envFilesBelow(folder, depth = 0) {
|
|
|
1413
1476
|
return found;
|
|
1414
1477
|
}
|
|
1415
1478
|
for (const entry of children) {
|
|
1416
|
-
const path =
|
|
1479
|
+
const path = join3(folder, entry.name);
|
|
1417
1480
|
if (entry.isDirectory() && depth < KNOWN_DEPTH && !SKIPPED.has(entry.name))
|
|
1418
1481
|
found.push(...await envFilesBelow(path, depth + 1));
|
|
1419
1482
|
else if (ENV_FILE.test(entry.name) && await isFileEntry(entry, path))
|
|
@@ -1573,4 +1636,4 @@ async function run(args) {
|
|
|
1573
1636
|
process10.stdout.write(redactor.push(decoder.decode()) + redactor.end());
|
|
1574
1637
|
return 0;
|
|
1575
1638
|
}
|
|
1576
|
-
process10.exitCode = await runMain({ name: "redact",
|
|
1639
|
+
process10.exitCode = await runMain({ name: "redact", run });
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.1",
|
|
5
5
|
"description": "ev lists and edits .env files with secrets masked, redact masks secrets in any piped output.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -15,13 +15,16 @@
|
|
|
15
15
|
"keywords": ["env", "dotenv", "secrets", "redact", "mask"],
|
|
16
16
|
"exports": {
|
|
17
17
|
"./dotenv": "./src/dotenv.ts",
|
|
18
|
-
"./
|
|
18
|
+
"./files": "./src/files.ts",
|
|
19
|
+
"./guard": "./src/guard.ts",
|
|
20
|
+
"./mask": "./src/mask.ts",
|
|
21
|
+
"./redactor": "./src/redactor.ts"
|
|
19
22
|
},
|
|
20
23
|
"bin": {
|
|
21
24
|
"ev": "./dist/ev.js",
|
|
22
25
|
"redact": "./dist/redact.js"
|
|
23
26
|
},
|
|
24
|
-
"files": ["dist", "src
|
|
27
|
+
"files": ["dist", "src"],
|
|
25
28
|
"engines": {
|
|
26
29
|
"bun": ">=1.4.3"
|
|
27
30
|
},
|
|
@@ -34,6 +37,6 @@
|
|
|
34
37
|
"redact": "$npm_execpath src/redact.ts"
|
|
35
38
|
},
|
|
36
39
|
"devDependencies": {
|
|
37
|
-
"@kaynooo/cli-kit": "0.
|
|
40
|
+
"@kaynooo/cli-kit": "0.21.0"
|
|
38
41
|
}
|
|
39
42
|
}
|
package/src/audit.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { Dirent } from 'node:fs'
|
|
2
|
+
import { readdir, stat } from 'node:fs/promises'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import { isSecretName } from '@kaynooo/cli-kit'
|
|
5
|
+
import { $ } from 'bun'
|
|
6
|
+
import { ENV_FILE, isFileEntry, TEMPLATE } from './files'
|
|
7
|
+
import { urlPasswords } from './mask'
|
|
8
|
+
|
|
9
|
+
export interface Exposure {
|
|
10
|
+
path: string
|
|
11
|
+
// Readable by an account other than its owner: any account, its group or an ACL entry.
|
|
12
|
+
readable: boolean
|
|
13
|
+
// Set when it is readable only through its group or an ACL, not by any account.
|
|
14
|
+
group?: boolean
|
|
15
|
+
tracked: boolean
|
|
16
|
+
// The keys a compose file writes out, for a compose file only.
|
|
17
|
+
secrets?: string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const MAX_DEPTH = 5
|
|
21
|
+
const SKIPPED = new Set(['node_modules', '.git', 'vendor'])
|
|
22
|
+
const COMPOSE_FILE = /^(?:docker-)?compose[\w.-]*\.ya?ml$/
|
|
23
|
+
// An environment entry, `KEY: value` or `- KEY=value`, with its value written out.
|
|
24
|
+
const ENTRY = /^\s*(?:-\s*)?["']?([A-Z0-9_]+)["']?\s*[:=]\s*["']?([^"'\s#]+)/
|
|
25
|
+
// A setting about a secret (its lifetime, the file or header holding it), not the secret.
|
|
26
|
+
const SETTING_SUFFIX = /_(?:TTL|FILE|PATH|DIR|NAME|TIMEOUT|EXPIRY|EXPIRES|LIFETIME|LENGTH|ENABLED|ALGORITHM|HEADER|PREFIX|COUNT|MAX|MIN)$/i
|
|
27
|
+
const ACL_READER = /^(?:user|group):[^:]+:r/
|
|
28
|
+
// A switch like MYSQL_RANDOM_ROOT_PASSWORD: 1, not a secret.
|
|
29
|
+
const FLAG = /^(?:[01]|yes|no|true|false)$/i
|
|
30
|
+
|
|
31
|
+
// A compose value written out rather than read from a .env, the environment or a secrets file: a secret-named value or a URL password.
|
|
32
|
+
export function isInlineSecret(name: string, value: string): boolean {
|
|
33
|
+
if (value === '' || value.startsWith('$') || value.startsWith('/run/secrets/') || FLAG.test(value))
|
|
34
|
+
return false
|
|
35
|
+
|
|
36
|
+
return (isSecretName(name) && !SETTING_SUFFIX.test(name)) || urlPasswords(value).some(password => !password.startsWith('$'))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The names of the secrets a compose file writes out.
|
|
40
|
+
export function inlineSecrets(compose: string): string[] {
|
|
41
|
+
const names = compose.split('\n').flatMap((line) => {
|
|
42
|
+
const [, name = '', value = ''] = ENTRY.exec(line) ?? []
|
|
43
|
+
|
|
44
|
+
return isInlineSecret(name, value) ? [name] : []
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return [...new Set(names)]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Any account can read it: the file lets others read and every folder above it lets them through.
|
|
51
|
+
export async function openToOthers(path: string): Promise<boolean> {
|
|
52
|
+
if (((await stat(path)).mode & 0o004) === 0)
|
|
53
|
+
return false
|
|
54
|
+
|
|
55
|
+
for (let dir = dirname(path); dir !== '/'; dir = dirname(dir)) {
|
|
56
|
+
if (((await stat(dir)).mode & 0o001) === 0)
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Whether the group bit lets in a group other than the owner's primary one, which only the owner belongs to on a system with private groups. */
|
|
64
|
+
export function groupReads(mode: number, gid: number, ownerGroup: number): boolean {
|
|
65
|
+
return (mode & 0o040) !== 0 && gid !== ownerGroup && gid !== process.getgid?.()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The primary group of an account, the account's own id when the system does not tell.
|
|
69
|
+
function primaryGroup(uid: number): number {
|
|
70
|
+
if (uid === process.getuid?.())
|
|
71
|
+
return process.getgid?.() ?? uid
|
|
72
|
+
|
|
73
|
+
const run = Bun.spawnSync(['id', '-g', String(uid)], { stdout: 'pipe', stderr: 'ignore' })
|
|
74
|
+
const gid = Number(run.stdout.toString().trim())
|
|
75
|
+
|
|
76
|
+
return run.exitCode === 0 && Number.isInteger(gid) ? gid : uid
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Accounts other than the owner can read it through a group that is not the owner's own, or through a named user or group of its ACL, and every folder above lets them through.
|
|
80
|
+
export async function sharedWith(path: string): Promise<boolean> {
|
|
81
|
+
const info = await stat(path)
|
|
82
|
+
const shared = groupReads(info.mode, info.gid, primaryGroup(info.uid))
|
|
83
|
+
|
|
84
|
+
if (!shared && (Bun.which('getfacl') === null || !await hasAclReader(path)))
|
|
85
|
+
return false
|
|
86
|
+
|
|
87
|
+
for (let dir = dirname(path); dir !== '/'; dir = dirname(dir)) {
|
|
88
|
+
if (((await stat(dir)).mode & 0o011) === 0)
|
|
89
|
+
return false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return true
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function hasAclReader(path: string): Promise<boolean> {
|
|
96
|
+
const acl = await $`getfacl --absolute-names --omit-header -- ${path}`.quiet().nothrow()
|
|
97
|
+
|
|
98
|
+
return acl.exitCode === 0 && acl.stdout.toString().split('\n').some(line => ACL_READER.test(line))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Exit 1 is git saying the file is not tracked, and a folder outside any repository tracks nothing; anything else is git failing, which must not read as untracked.
|
|
102
|
+
async function tracked(path: string): Promise<boolean> {
|
|
103
|
+
const run = await $`git ls-files --error-unmatch -- ${path}`.cwd(dirname(path)).quiet().nothrow()
|
|
104
|
+
const error = run.stderr.toString()
|
|
105
|
+
|
|
106
|
+
if (run.exitCode === 0)
|
|
107
|
+
return true
|
|
108
|
+
|
|
109
|
+
if (run.exitCode === 1 || error.includes('not a git repository'))
|
|
110
|
+
return false
|
|
111
|
+
|
|
112
|
+
throw new Error(`git could not tell whether ${path} is tracked: ${error.trim() || `exit ${run.exitCode}`}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// The .env* and compose files under root, down to MAX_DEPTH folders, skipping dependency and git folders and following no link; a folder it cannot read goes to `unread`.
|
|
116
|
+
async function* candidates(root: string, unread: string[], depth = 0): AsyncGenerator<string> {
|
|
117
|
+
let found: Dirent[]
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
found = await readdir(root, { withFileTypes: true })
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
unread.push(root)
|
|
124
|
+
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const entry of found) {
|
|
129
|
+
const path = join(root, entry.name)
|
|
130
|
+
|
|
131
|
+
if (entry.isDirectory() && depth < MAX_DEPTH && !SKIPPED.has(entry.name))
|
|
132
|
+
yield* candidates(path, unread, depth + 1)
|
|
133
|
+
else if (!TEMPLATE.test(entry.name) && (ENV_FILE.test(entry.name) || COMPOSE_FILE.test(entry.name)) && await isFileEntry(entry, path))
|
|
134
|
+
yield path
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// The .env files any account can read or git tracks, and the compose files writing secrets out that any account can read: tracked dev composes with throwaway passwords are left out as too common.
|
|
139
|
+
export async function audit(roots: string[]): Promise<{ exposed: Exposure[], unread: string[] }> {
|
|
140
|
+
const exposed: Exposure[] = []
|
|
141
|
+
const unread: string[] = []
|
|
142
|
+
|
|
143
|
+
for (const root of roots) {
|
|
144
|
+
for await (const path of candidates(root, unread)) {
|
|
145
|
+
const others = await openToOthers(path)
|
|
146
|
+
const readable = others || await sharedWith(path)
|
|
147
|
+
const group = readable && !others ? true : undefined
|
|
148
|
+
const name = path.slice(path.lastIndexOf('/') + 1)
|
|
149
|
+
|
|
150
|
+
if (ENV_FILE.test(name)) {
|
|
151
|
+
const isTracked = await tracked(path)
|
|
152
|
+
|
|
153
|
+
if (readable || isTracked)
|
|
154
|
+
exposed.push({ path, readable, ...group && { group }, tracked: isTracked })
|
|
155
|
+
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const secrets = readable ? inlineSecrets(await Bun.file(path).text()) : []
|
|
160
|
+
|
|
161
|
+
if (secrets.length > 0)
|
|
162
|
+
exposed.push({ path, readable, ...group && { group }, tracked: false, secrets })
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { exposed: exposed.sort((a, b) => a.path.localeCompare(b.path)), unread }
|
|
167
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Exposure } from '../audit'
|
|
2
|
+
import type { ComposeFix } from '../fix'
|
|
3
|
+
import { stat } from 'node:fs/promises'
|
|
4
|
+
import { relative, resolve } from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { fail, jsonOption, parse, printJson } from '@kaynooo/cli-kit'
|
|
7
|
+
import { shellQuote } from '@kaynooo/cli-kit/shell'
|
|
8
|
+
import { audit as findExposed } from '../audit'
|
|
9
|
+
import { fixCompose, shutOthersOut } from '../fix'
|
|
10
|
+
|
|
11
|
+
const usage = `ev audit [dir...] [--fix]
|
|
12
|
+
|
|
13
|
+
Lists the .env files any account on the machine can read or git tracks, and the compose files any account can read that write a secret out (a secret-named value or a URL password) instead of reading it from a .env, never printing a value.
|
|
14
|
+
Looks under each folder (the current one by default) down to 5 levels, skipping node_modules, vendor and .git, and leaves .example, .sample, .template and .dist files out. A folder it cannot read is named on stderr.
|
|
15
|
+
With --fix, moves each secret a listed compose file writes out into the .env beside it as \${KEY}, puts it back when \`docker compose config\` resolves differently after, and shuts other accounts out of both files and of every listed .env; a key the .env already sets to another value stays put.
|
|
16
|
+
Exits 0 when it finds none (with --fix, when none is left), 1 when it finds one, 2 on a usage error.`
|
|
17
|
+
|
|
18
|
+
const options = {
|
|
19
|
+
json: jsonOption('the findings and the folders it could not read'),
|
|
20
|
+
fix: { type: 'boolean', description: 'move inline compose secrets into a .env and shut other accounts out' },
|
|
21
|
+
} as const
|
|
22
|
+
|
|
23
|
+
const MAX_UNREAD_LISTED = 3
|
|
24
|
+
|
|
25
|
+
const CHECKS: Record<NonNullable<ComposeFix['check']>, string> = {
|
|
26
|
+
'same': 'docker compose config resolves the same',
|
|
27
|
+
'rolled back': 'docker compose config resolved differently, so everything was put back',
|
|
28
|
+
'no docker': 'not checked against docker compose config: docker is not installed',
|
|
29
|
+
'fails before': 'not checked against docker compose config: it already failed on the file',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function isFolder(path: string): Promise<boolean> {
|
|
33
|
+
try {
|
|
34
|
+
return (await stat(path)).isDirectory()
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function shown(path: string): string {
|
|
42
|
+
return path.startsWith(`${process.cwd()}/`) ? relative(process.cwd(), path) : path
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Fixes what it can of each finding and prints what it did, never a value, unless the caller prints JSON.
|
|
46
|
+
async function fixAll(exposed: Exposure[], json: boolean): Promise<{ fixed: ComposeFix[], left: boolean }> {
|
|
47
|
+
const fixed: ComposeFix[] = []
|
|
48
|
+
let left = false
|
|
49
|
+
|
|
50
|
+
for (const { path, readable, group, tracked, secrets } of exposed) {
|
|
51
|
+
const fix = secrets === undefined ? undefined : await fixCompose(path, group === true)
|
|
52
|
+
const open = fix?.open ?? (readable && !await shutOthersOut(path, group === true) ? [path] : [])
|
|
53
|
+
|
|
54
|
+
if (fix !== undefined)
|
|
55
|
+
fixed.push(fix)
|
|
56
|
+
|
|
57
|
+
left ||= tracked || open.length > 0 || (fix?.left.length ?? 0) > 0
|
|
58
|
+
|
|
59
|
+
if (json)
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
const lines = [
|
|
63
|
+
...fix === undefined || fix.moved.length === 0 ? [] : [`moved ${fix.moved.join(', ')} into ${shown(fix.env)}`],
|
|
64
|
+
...fix?.left.map(({ key, why }) => `left ${key} in place: ${why}`) ?? [],
|
|
65
|
+
...fix?.check === undefined ? [] : [CHECKS[fix.check]],
|
|
66
|
+
...open.map(file => `another account owns ${file === path ? 'it' : shown(file)}, so it stays readable: sudo chmod go-rwx ${shellQuote(shown(file))}`),
|
|
67
|
+
...readable && !open.includes(path) ? ['now unreadable by other accounts'] : [],
|
|
68
|
+
...tracked ? [`tracked by git: git rm --cached ${shellQuote(shown(path))}, then ignore it`] : [],
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
console.log(`${shown(path)}\n ${lines.join('\n ')}`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return { fixed, left }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function audit(args: string[]): Promise<number> {
|
|
78
|
+
const parsed = parse(args, usage, options)
|
|
79
|
+
|
|
80
|
+
const roots = (parsed.positionals.length > 0 ? parsed.positionals : ['.']).map(dir => resolve(dir))
|
|
81
|
+
|
|
82
|
+
for (const dir of roots) {
|
|
83
|
+
if (!await isFolder(dir))
|
|
84
|
+
fail(`${dir} is not a folder`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const { exposed, unread } = await findExposed(roots)
|
|
88
|
+
const json = parsed.values.json === true
|
|
89
|
+
|
|
90
|
+
// Container data folders owned by another account often run to dozens, so past a few they are only counted.
|
|
91
|
+
if (!json && unread.length > MAX_UNREAD_LISTED) {
|
|
92
|
+
console.error(`could not read ${unread.length} folders, so they were not audited (--json lists them)`)
|
|
93
|
+
}
|
|
94
|
+
else if (!json) {
|
|
95
|
+
for (const dir of unread)
|
|
96
|
+
console.error(`could not read ${dir}, so it was not audited`)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (parsed.values.fix === true && (json || exposed.length > 0)) {
|
|
100
|
+
const { fixed, left } = await fixAll(exposed, json)
|
|
101
|
+
|
|
102
|
+
if (json)
|
|
103
|
+
printJson({ fixed, exposed: (await findExposed(roots)).exposed, unread })
|
|
104
|
+
|
|
105
|
+
return left ? 1 : 0
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (json) {
|
|
109
|
+
printJson({ exposed, unread })
|
|
110
|
+
|
|
111
|
+
return exposed.length > 0 ? 1 : 0
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (exposed.length === 0) {
|
|
115
|
+
console.log('no exposed .env file nor compose file writing a secret out')
|
|
116
|
+
|
|
117
|
+
return 0
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const uid = process.getuid?.()
|
|
121
|
+
|
|
122
|
+
for (const { path, readable, group, tracked, secrets } of exposed) {
|
|
123
|
+
// Only the owner or root can change a file's mode.
|
|
124
|
+
const sudo = uid !== undefined && uid !== 0 && (await stat(path)).uid !== uid ? 'sudo ' : ''
|
|
125
|
+
const problems = [
|
|
126
|
+
...secrets === undefined ? [] : [`writes out ${secrets.join(', ')}: ev audit --fix moves the values into a .env beside it`],
|
|
127
|
+
...readable ? [group === true ? `readable by its group or an ACL: ${sudo}chmod go-rwx ${shellQuote(shown(path))}` : `readable by any account: ${sudo}chmod o-rwx ${shellQuote(shown(path))}`] : [],
|
|
128
|
+
...tracked ? [`tracked by git: git rm --cached ${shellQuote(shown(path))}, then ignore it`] : [],
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
console.log(`${shown(path)}\n ${problems.join('\n ')}`)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return 1
|
|
135
|
+
}
|