@kaynooo/cli-env 0.9.0 → 0.10.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/ev.js CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env -S bun --no-env-file
2
2
  // @bun
3
3
 
4
4
  // src/ev.ts
5
- import process11 from "process";
5
+ import process12 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process6 from "process";
8
8
  import { parseArgs } from "util";
@@ -825,7 +825,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
825
825
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
826
826
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
827
827
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
828
- if (error instanceof UsageError)
828
+ if (error instanceof Exit && error.code === 2)
829
829
  noteUsageError(error.message);
830
830
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
831
831
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
@@ -2522,8 +2522,154 @@ ${showUsage}`);
2522
2522
  return 0;
2523
2523
  }
2524
2524
 
2525
+ // src/commands/redact.ts
2526
+ import process11 from "process";
2527
+
2528
+ // src/redactor.ts
2529
+ var DIFF_START = /^(?:diff --git |@@ )/;
2530
+ var NEW_FILE = /^\+\+\+ (?:b\/)?(.+)$/;
2531
+ var HUNK = /^@@ -\d+(?:,\d+)? \+(\d+)/;
2532
+ var MAX_PENDING = 1 << 18;
2533
+ var OVERLAP = 1 << 13;
2534
+
2535
+ class Redactor {
2536
+ known;
2537
+ pending = "";
2538
+ inKey = false;
2539
+ hidden = false;
2540
+ constructor(known) {
2541
+ this.known = known;
2542
+ }
2543
+ push(chunk) {
2544
+ const end = chunk.lastIndexOf(`
2545
+ `);
2546
+ this.pending += chunk;
2547
+ if (end === -1)
2548
+ return this.pending.length > MAX_PENDING ? this.cut() : "";
2549
+ const split = this.pending.length - chunk.length + end + 1;
2550
+ const complete = this.pending.slice(0, split);
2551
+ this.pending = this.pending.slice(split);
2552
+ return complete.split(`
2553
+ `).slice(0, -1).map((line) => this.line(line)).filter((line) => line !== undefined).map((line) => `${line}
2554
+ `).join("");
2555
+ }
2556
+ cut() {
2557
+ const limit = this.pending.length - OVERLAP;
2558
+ const space = this.pending.lastIndexOf(" ", limit);
2559
+ const at = space > 0 ? space + 1 : limit;
2560
+ const head = this.pending.slice(0, at);
2561
+ this.pending = this.pending.slice(at);
2562
+ return this.line(head) ?? "";
2563
+ }
2564
+ end() {
2565
+ const rest = this.pending;
2566
+ this.pending = "";
2567
+ return rest === "" ? "" : this.line(rest) ?? "";
2568
+ }
2569
+ line(line) {
2570
+ if (this.inKey) {
2571
+ const end = PEM_END.exec(line);
2572
+ if (end === null) {
2573
+ if (this.hidden)
2574
+ return;
2575
+ this.hidden = true;
2576
+ return KEY_MASK;
2577
+ }
2578
+ this.inKey = false;
2579
+ return (end.index > 0 && !this.hidden ? KEY_MASK : "") + this.line(line.slice(end.index));
2580
+ }
2581
+ const body = openKeyBody(line);
2582
+ if (body !== undefined) {
2583
+ this.inKey = true;
2584
+ this.hidden = body !== "";
2585
+ }
2586
+ return maskLine(line, this.known);
2587
+ }
2588
+ }
2589
+ function check(text, known) {
2590
+ const findings = [];
2591
+ const isDiff = text.split(`
2592
+ `).some((line) => DIFF_START.test(line));
2593
+ let file = "stdin";
2594
+ let number = 0;
2595
+ let inKey = false;
2596
+ for (const [index, raw] of text.split(`
2597
+ `).entries()) {
2598
+ let line = raw;
2599
+ if (isDiff) {
2600
+ const hunk = HUNK.exec(raw);
2601
+ if (hunk !== null) {
2602
+ number = Number(hunk[1]) - 1;
2603
+ continue;
2604
+ }
2605
+ const named = NEW_FILE.exec(raw);
2606
+ if (named !== null) {
2607
+ file = named[1] ?? file;
2608
+ continue;
2609
+ }
2610
+ if (raw.startsWith("diff --git ")) {
2611
+ file = "(unnamed)";
2612
+ continue;
2613
+ }
2614
+ if (raw.startsWith("-") || raw.startsWith("\\"))
2615
+ continue;
2616
+ number++;
2617
+ if (!raw.startsWith("+"))
2618
+ continue;
2619
+ line = raw.slice(1);
2620
+ } else {
2621
+ number = index + 1;
2622
+ }
2623
+ if (inKey) {
2624
+ const end = PEM_END.exec(line);
2625
+ inKey = end === null;
2626
+ if (end === null)
2627
+ continue;
2628
+ line = line.slice(end.index);
2629
+ }
2630
+ const found = findSecrets(line, known).map((match) => match.kind);
2631
+ const body = openKeyBody(line);
2632
+ if (body !== undefined) {
2633
+ inKey = true;
2634
+ if (body === "")
2635
+ found.push("private key");
2636
+ }
2637
+ for (const kind of found)
2638
+ findings.push({ file, line: number, kind });
2639
+ }
2640
+ return findings;
2641
+ }
2642
+
2643
+ // src/commands/redact.ts
2644
+ var usage4 = `redact [--check]
2645
+
2646
+ Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
2647
+ --check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.
2648
+
2649
+ Exits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`;
2650
+ var options3 = {
2651
+ check: { type: "boolean", description: "list where secrets are instead of masking them, exit 1 when found" },
2652
+ "no-env": { type: "boolean", description: "do not search for the values of the .env* files" }
2653
+ };
2654
+ async function redact(args) {
2655
+ const parsed = parse(args, usage4, options3, { replaces: "printing a command's secrets into a log or a transcript", examples: [["docker compose config | redact", "the resolved compose file, secrets masked"], ["git diff --cached | redact --check", "refuse a commit carrying a secret"]] });
2656
+ const known = parsed.values["no-env"] === true ? undefined : await loadKnown();
2657
+ if (parsed.values.check === true) {
2658
+ const findings = check(await readInput("-"), known);
2659
+ for (const { file, line, kind } of findings)
2660
+ console.log(`${file}:${line}: ${kind}`);
2661
+ return findings.length === 0 ? 0 : 1;
2662
+ }
2663
+ const redactor = new Redactor(known);
2664
+ const decoder = new TextDecoder;
2665
+ for await (const chunk of Bun.stdin.stream())
2666
+ process11.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })));
2667
+ process11.stdout.write(redactor.push(decoder.decode()) + redactor.end());
2668
+ return 0;
2669
+ }
2670
+
2525
2671
  // src/ev.ts
2526
- process11.exitCode = await runCli({
2672
+ process12.exitCode = await runCli({
2527
2673
  name: "ev",
2528
2674
  about: ".env files listed and edited with secrets masked",
2529
2675
  replaces: "cat .env, grep on env files and editing them by hand",
@@ -2540,6 +2686,7 @@ process11.exitCode = await runCli({
2540
2686
  diff: { run: diff, summary: "keys set in one file and not the other" },
2541
2687
  missing: { run: missing, summary: "keys of .env.example that .env lacks" },
2542
2688
  pins: { run: pins, summary: "the env vars reaching outside that an app's tests leave to a developer's env" },
2689
+ redact: { run: redact, summary: "stdin copied to stdout with secrets masked, or --check to list where they are" },
2543
2690
  audit: { run: audit2, summary: ".env files others can read or git tracks, compose files writing secrets out" }
2544
2691
  },
2545
2692
  aliases: { ls: "list" }
package/dist/redact.js CHANGED
@@ -1,8 +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
- import process10 from "process";
5
+ import process11 from "process";
6
6
  // ../kit/src/args.ts
7
7
  import process6 from "process";
8
8
  import { parseArgs } from "util";
@@ -825,7 +825,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
825
825
  const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
826
826
  const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
827
827
  const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
828
- if (error instanceof UsageError)
828
+ if (error instanceof Exit && error.code === 2)
829
829
  noteUsageError(error.message);
830
830
  const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
831
831
  report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
@@ -1047,6 +1047,9 @@ function isSecretName(name) {
1047
1047
  var {Glob } = globalThis.Bun;
1048
1048
  var SESSION_FILES = new Glob("*.json");
1049
1049
  var ACCOUNT_DIRS = new Glob("*/sessions");
1050
+ // src/commands/redact.ts
1051
+ import process10 from "process";
1052
+
1050
1053
  // src/files.ts
1051
1054
  import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from "fs/promises";
1052
1055
  import { basename, dirname as dirname3, join as join3 } from "path";
@@ -1609,7 +1612,7 @@ function check(text, known) {
1609
1612
  return findings;
1610
1613
  }
1611
1614
 
1612
- // src/redact.ts
1615
+ // src/commands/redact.ts
1613
1616
  var usage2 = `redact [--check]
1614
1617
 
1615
1618
  Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
@@ -1620,7 +1623,7 @@ var options = {
1620
1623
  check: { type: "boolean", description: "list where secrets are instead of masking them, exit 1 when found" },
1621
1624
  "no-env": { type: "boolean", description: "do not search for the values of the .env* files" }
1622
1625
  };
1623
- async function run(args) {
1626
+ async function redact(args) {
1624
1627
  const parsed = parse(args, usage2, options, { replaces: "printing a command's secrets into a log or a transcript", examples: [["docker compose config | redact", "the resolved compose file, secrets masked"], ["git diff --cached | redact --check", "refuse a commit carrying a secret"]] });
1625
1628
  const known = parsed.values["no-env"] === true ? undefined : await loadKnown();
1626
1629
  if (parsed.values.check === true) {
@@ -1636,4 +1639,6 @@ async function run(args) {
1636
1639
  process10.stdout.write(redactor.push(decoder.decode()) + redactor.end());
1637
1640
  return 0;
1638
1641
  }
1639
- process10.exitCode = await runMain({ name: "redact", run });
1642
+
1643
+ // src/redact.ts
1644
+ process11.exitCode = await runMain({ name: "redact", run: redact });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-env",
3
3
  "type": "module",
4
- "version": "0.9.0",
4
+ "version": "0.10.0",
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",
@@ -37,6 +37,6 @@
37
37
  "redact": "$npm_execpath src/redact.ts"
38
38
  },
39
39
  "devDependencies": {
40
- "@kaynooo/cli-kit": "0.20.0"
40
+ "@kaynooo/cli-kit": "0.21.1"
41
41
  }
42
42
  }
@@ -0,0 +1,39 @@
1
+ import process from 'node:process'
2
+ import { parse, readInput } from '@kaynooo/cli-kit'
3
+ import { loadKnown } from '../files'
4
+ import { check, Redactor } from '../redactor'
5
+
6
+ const usage = `redact [--check]
7
+
8
+ Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
9
+ --check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.\n\nExits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`
10
+
11
+ const options = {
12
+ 'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
13
+ 'no-env': { type: 'boolean', description: 'do not search for the values of the .env* files' },
14
+ } as const
15
+
16
+ export async function redact(args: string[]): Promise<number> {
17
+ const parsed = parse(args, usage, options, { replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
18
+
19
+ const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
20
+
21
+ if (parsed.values.check === true) {
22
+ const findings = check(await readInput('-'), known)
23
+
24
+ for (const { file, line, kind } of findings)
25
+ console.log(`${file}:${line}: ${kind}`)
26
+
27
+ return findings.length === 0 ? 0 : 1
28
+ }
29
+
30
+ const redactor = new Redactor(known)
31
+ const decoder = new TextDecoder()
32
+
33
+ for await (const chunk of Bun.stdin.stream())
34
+ process.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })))
35
+
36
+ process.stdout.write(redactor.push(decoder.decode()) + redactor.end())
37
+
38
+ return 0
39
+ }
package/src/ev.ts CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env -S bun --no-env-file
2
2
 
3
3
  import process from 'node:process'
4
4
  import { runCli } from '@kaynooo/cli-kit'
@@ -7,6 +7,7 @@ import { diff, missing } from './commands/compare'
7
7
  import { cp, mv, set, unset } from './commands/edit'
8
8
  import { pins } from './commands/pins'
9
9
  import { get, grep, list, show } from './commands/read'
10
+ import { redact } from './commands/redact'
10
11
 
11
12
  process.exitCode = await runCli({
12
13
  name: 'ev',
@@ -25,6 +26,7 @@ process.exitCode = await runCli({
25
26
  diff: { run: diff, summary: 'keys set in one file and not the other' },
26
27
  missing: { run: missing, summary: 'keys of .env.example that .env lacks' },
27
28
  pins: { run: pins, summary: 'the env vars reaching outside that an app\'s tests leave to a developer\'s env' },
29
+ redact: { run: redact, summary: 'stdin copied to stdout with secrets masked, or --check to list where they are' },
28
30
  audit: { run: audit, summary: '.env files others can read or git tracks, compose files writing secrets out' },
29
31
  },
30
32
  aliases: { ls: 'list' },
package/src/redact.ts CHANGED
@@ -1,43 +1,7 @@
1
- #!/usr/bin/env bun
1
+ #!/usr/bin/env -S bun --no-env-file
2
2
 
3
3
  import process from 'node:process'
4
- import { parse, readInput, runMain } from '@kaynooo/cli-kit'
5
- import { loadKnown } from './files'
6
- import { check, Redactor } from './redactor'
4
+ import { runMain } from '@kaynooo/cli-kit'
5
+ import { redact } from './commands/redact'
7
6
 
8
- const usage = `redact [--check]
9
-
10
- Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
11
- --check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.\n\nExits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`
12
-
13
- const options = {
14
- 'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
15
- 'no-env': { type: 'boolean', description: 'do not search for the values of the .env* files' },
16
- } as const
17
-
18
- async function run(args: string[]): Promise<number> {
19
- const parsed = parse(args, usage, options, { replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
20
-
21
- const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
22
-
23
- if (parsed.values.check === true) {
24
- const findings = check(await readInput('-'), known)
25
-
26
- for (const { file, line, kind } of findings)
27
- console.log(`${file}:${line}: ${kind}`)
28
-
29
- return findings.length === 0 ? 0 : 1
30
- }
31
-
32
- const redactor = new Redactor(known)
33
- const decoder = new TextDecoder()
34
-
35
- for await (const chunk of Bun.stdin.stream())
36
- process.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })))
37
-
38
- process.stdout.write(redactor.push(decoder.decode()) + redactor.end())
39
-
40
- return 0
41
- }
42
-
43
- process.exitCode = await runMain({ name: 'redact', run })
7
+ process.exitCode = await runMain({ name: 'redact', run: redact })