@wirecat/cli-core 0.441.0 → 0.442.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/CHANGELOG.md CHANGED
@@ -7,6 +7,37 @@ shape of this file.
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ## 0.442.0 — 26.10.2026
11
+
12
+ ### Security
13
+
14
+ - **A run record and a kept error message now find a secret by what it looks like, not only by the flag's name.**
15
+ The secret is replaced by `[redacted]` inside the word and the words around it stay:
16
+ - a token in a known format: GitHub (`ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`, `github_pat_`), OpenAI and
17
+ Anthropic (`sk-`), Slack (`xoxb-` and the like), an AWS access key id (`AKIA`, `ASIA`), a Google API key
18
+ (`AIza`), a Stripe secret or restricted key (`sk_live_`, `rk_live_` and the `_test_` ones; a `pk_` key is public
19
+ and stays), an npm token (`npm_`), a Telegram bot token, a JWT, and a PEM private key block;
20
+ - a `Bearer` or `Basic` credential;
21
+ - the value of a pair whose name says it is a secret, `password=…`, `API_TOKEN=…`, `client_secret:…`,
22
+ `"accessToken":"…"`, the name kept;
23
+ - a random-looking string of 20 or more characters: mixed upper and lower case with a digit or symbol, entropy of
24
+ at least 3.5 bits a character, case changing often and cut into short pieces. Words, camelCase names, UUIDs,
25
+ hex hashes, run ids, dates, paths and URLs without a password in them stay.
26
+
27
+ A profile name or a mistyped command word that looks like a secret is now `[redacted]` even with `record_input`
28
+ off.
29
+
30
+ - **A config that will not load no longer lets a run record keep the typed words.** When `recordRuns` or
31
+ `commandRuns` could not read the CLI's settings, it fell back to the defaults, so `record_input` counted as on and
32
+ the command line was kept as typed, even from a config that says `record_input: false`. A failed settings read now
33
+ masks every value that may be content, as cli-core's own commands already did.
34
+
35
+ ### Changed — may break callers
36
+
37
+ - **`recordRuns` keeps the command line in the run record by itself.** A CLI with its own program (cli-memo,
38
+ drive-cli) kept no `argv` unless it passed the typed words through `run()`; the words its `parseAsync` is given are
39
+ now kept, secrets hidden, and masked when `settings()` returns `recordInput: false`.
40
+
10
41
  ## 0.441.0 — 26.10.2026
11
42
 
12
43
  ### Added
@@ -18,7 +18,10 @@ export interface RunSettings {
18
18
  export declare const runSettings: (flag: boolean | undefined, saved?: SavedRunSettings) => RunSettings;
19
19
  export interface CommandRunOptions {
20
20
  app: AppIdentity;
21
- /** The owner's saved choice. One that throws — a config that will not load — leaves the defaults. */
21
+ /**
22
+ * The owner's saved choice. One that throws — a config that will not load — leaves the defaults, except that the
23
+ * typed words are masked: the broken config may be the one that says `record_input: false`.
24
+ */
22
25
  settings: () => SavedRunSettings;
23
26
  /**
24
27
  * The profile the command runs for; one that throws or is empty leaves the run without one. Given only by a CLI
@@ -21,7 +21,7 @@ const pathOf = (command) => {
21
21
  words.unshift(at.name());
22
22
  return words;
23
23
  };
24
- const tracker = (options) => {
24
+ const tracker = (options, typed) => {
25
25
  let recording;
26
26
  const open = (program, words, flag, format) => {
27
27
  recording = undefined;
@@ -33,7 +33,9 @@ const tracker = (options) => {
33
33
  try {
34
34
  saved = options.settings();
35
35
  }
36
- catch { }
36
+ catch {
37
+ saved = { recordInput: false };
38
+ }
37
39
  let profile;
38
40
  try {
39
41
  profile = options.profile?.() || undefined;
@@ -59,7 +61,7 @@ const tracker = (options) => {
59
61
  bin: options.app.command,
60
62
  command: words.length > 0 ? command : "",
61
63
  program: rootOf(program),
62
- ...(environment.argv ? { argv: environment.argv } : {}),
64
+ ...((environment.argv ?? typed) ? { argv: environment.argv ?? typed } : {}),
63
65
  env: env ?? process.env,
64
66
  recordInput: saved.recordInput !== false,
65
67
  }),
@@ -153,7 +155,8 @@ export const recordRuns = (program, options) => {
153
155
  });
154
156
  const parseAsync = program.parseAsync.bind(program);
155
157
  program.parseAsync = (argv, settings) => {
156
- const parse = { runs: tracker(options), started: false };
158
+ const args = settings?.from === "user" ? (argv ?? []) : (argv ?? process.argv).slice(2);
159
+ const parse = { runs: tracker(options, args), started: false };
157
160
  return parses.run(parse, async () => {
158
161
  try {
159
162
  const parsed = await parseAsync(argv, settings);
@@ -164,7 +167,6 @@ export const recordRuns = (program, options) => {
164
167
  if (!isHelpExit(error) && !wasSettled(error)) {
165
168
  try {
166
169
  if (!parse.started) {
167
- const args = settings?.from === "user" ? (argv ?? []) : (argv ?? process.argv).slice(2);
168
170
  const flag = args.includes("--no-record") ? false : args.includes("--record") ? true : undefined;
169
171
  const json = args.some((arg) => arg === "--json" || arg.startsWith("--json=") || arg === "--jsonl");
170
172
  const words = pathFromArgs(program, args, options.profile !== undefined);
@@ -39,7 +39,8 @@ export declare const INTERNATIONAL_PHONE: string;
39
39
  export declare const BARE_PHONE: string;
40
40
  /**
41
41
  * An error's sentence with what could be somebody's words or a secret taken out: anything in quotes, an email, a
42
- * phone number, a bot token, a bearer or a long opaque string, a URL's query, the home directory. A second line of
43
- * defence — errors here are written to name things, not quote them — so it errs toward masking.
42
+ * phone number, a secret `withoutSecrets` finds, a bot token, a bearer or a long opaque string, a URL's query, the
43
+ * home directory. A second line of defence — errors here are written to name things, not quote them — so it errs
44
+ * toward masking.
44
45
  */
45
46
  export declare const redactText: (text: string) => string;
@@ -1,6 +1,7 @@
1
1
  import { homedir } from "node:os";
2
2
  import { isCliFailure } from "../errors/failures.js";
3
3
  import { providerErrorKey } from "./events.js";
4
+ import { withoutSecrets } from "./secrets.js";
4
5
  const MESSAGE_AT_MOST = 500;
5
6
  const CAUSES_AT_MOST = 5;
6
7
  const FRAMES_AT_MOST = 15;
@@ -114,14 +115,14 @@ export const INTERNATIONAL_PHONE = /\+\d[\d\s()-]{6,}\d/.source;
114
115
  export const BARE_PHONE = /(^|[^\w-])\d{11,15}\b/.source;
115
116
  /**
116
117
  * An error's sentence with what could be somebody's words or a secret taken out: anything in quotes, an email, a
117
- * phone number, a bot token, a bearer or a long opaque string, a URL's query, the home directory. A second line of
118
- * defence — errors here are written to name things, not quote them — so it errs toward masking.
118
+ * phone number, a secret `withoutSecrets` finds, a bot token, a bearer or a long opaque string, a URL's query, the
119
+ * home directory. A second line of defence — errors here are written to name things, not quote them — so it errs
120
+ * toward masking.
119
121
  */
120
122
  export const redactText = (text) => {
121
123
  const home = homedir();
122
124
  let out = home && home !== "/" ? text.split(home).join("~") : text;
123
- out = out
124
- .replace(/"[^"\n]*"|“[^”\n]*”|«[^»\n]*»/g, '"[redacted]"')
125
+ out = withoutSecrets(out.replace(/"[^"\n]*"|“[^”\n]*”|«[^»\n]*»/g, '"[redacted]"'))
125
126
  .replace(/\b(bearer|basic|token|key|password|secret)(\s*[:=]\s*|\s+)[^\s'"]+/gi, "$1$2[redacted]")
126
127
  .replace(/[\w.+-]+@[\w-]+\.[\w.-]+/g, "[email]")
127
128
  .replace(new RegExp(INTERNATIONAL_PHONE, "g"), "[number]")
@@ -1,5 +1,6 @@
1
1
  import { isContentKey } from "@wirecat/cli-base/standards";
2
2
  import { BARE_PHONE, INTERNATIONAL_PHONE } from "./error-detail.js";
3
+ import { secretNamed, withoutSecrets } from "./secrets.js";
3
4
  export const REDACTED = "[redacted]";
4
5
  /**
5
6
  * The agent is read from what each one sets in the processes it starts: Claude Code `CLAUDECODE` and
@@ -49,26 +50,6 @@ export const secretInput = (input) => {
49
50
  secretInputs.add(input);
50
51
  return input;
51
52
  };
52
- const SECRET_WORDS = new Set([
53
- "authorization",
54
- "cookie",
55
- "credential",
56
- "credentials",
57
- "passphrase",
58
- "password",
59
- "secret",
60
- "token",
61
- "apikey",
62
- ]);
63
- // Undeclared flags carry no mark, so the name is the only signal; CLIs take secrets at a prompt (WC-SECR-5) and this
64
- // catches the one typed anyway.
65
- const secretNamed = (name) => {
66
- const words = name
67
- .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
68
- .split(/[\s_.-]+/)
69
- .map((word) => word.toLowerCase());
70
- return words.some((word, at) => SECRET_WORDS.has(word) || (word === "api" && words[at + 1] === "key"));
71
- };
72
53
  // A password inside a URL, or one unbroken run of 40 token characters, is a credential wherever it was typed.
73
54
  const secretShaped = (word) => /:\/\/[^/@\s:]+:[^/@\s]+@/.test(word) || /(^|[^A-Za-z0-9_-])[A-Za-z0-9_-]{40,}($|[^A-Za-z0-9_-])/.test(word);
74
55
  // Messaging CLIs promise a phone number reaches no log, so it goes even from text the record otherwise keeps.
@@ -88,7 +69,7 @@ export const maskArgv = (program, words, { keep = false } = {}) => {
88
69
  let positional = 0;
89
70
  let profileSeen = false;
90
71
  let lost = false;
91
- const raw = (word) => (keep && !secretShaped(word) ? withoutPhones(word) : REDACTED);
72
+ const raw = (word) => (keep && !secretShaped(word) ? withoutPhones(withoutSecrets(word, REDACTED)) : REDACTED);
92
73
  const keptValue = (name, value, input) => keep
93
74
  ? (input && secretInputs.has(input)) || secretNamed(name)
94
75
  ? REDACTED
@@ -151,7 +132,7 @@ export const maskArgv = (program, words, { keep = false } = {}) => {
151
132
  }
152
133
  if (current === program && !profileSeen) {
153
134
  profileSeen = true;
154
- argv.push(keep ? raw(word) : PROFILE.test(word) && withoutPhones(word) === word ? word : REDACTED);
135
+ argv.push(keep ? raw(word) : PROFILE.test(word) && withoutPhones(withoutSecrets(word)) === word ? word : REDACTED);
155
136
  continue;
156
137
  }
157
138
  if (current.commands.length > 0 && current.registeredArguments.length === 0) {
@@ -171,7 +152,7 @@ export const maskArgv = (program, words, { keep = false } = {}) => {
171
152
  const NUMBER = /^-?\d+(\.\d+)?$/;
172
153
  const PROFILE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
173
154
  /** A mistyped command word is kept so the typo can be seen; anything shaped like a sentence or an address is not. */
174
- const commandLike = (word) => (/^[a-z][a-z0-9-]{0,31}$/i.test(word) ? word : REDACTED);
155
+ const commandLike = (word) => /^[a-z][a-z0-9-]{0,31}$/i.test(word) && withoutSecrets(word) === word ? word : REDACTED;
175
156
  const maskedValue = (name, value, choices) => {
176
157
  if (isContentKey(name))
177
158
  return REDACTED;
@@ -0,0 +1,12 @@
1
+ export declare const secretNamed: (name: string) => boolean;
2
+ /**
3
+ * Whether a run of token characters is a random string rather than words: long, mixed case, high Shannon entropy,
4
+ * cut into short pieces (`Kz9vQ2mX`) where a name is cut into words (`InlineQueryResultMpeg4Gif`).
5
+ */
6
+ export declare const looksRandom: (run: string) => boolean;
7
+ /**
8
+ * Text with each secret inside it replaced by `mark` and the words around it kept: a token of a known vendor format,
9
+ * a private key, a `Bearer`/`Basic` credential, the value of a secret-named `name=value` or `name: value`, and a
10
+ * random-looking string of 20 or more printable characters (`looksRandom`).
11
+ */
12
+ export declare const withoutSecrets: (text: string, mark?: string) => string;
@@ -0,0 +1,88 @@
1
+ const SECRET_WORDS = new Set([
2
+ "authorization",
3
+ "cookie",
4
+ "credential",
5
+ "credentials",
6
+ "passphrase",
7
+ "password",
8
+ "secret",
9
+ "token",
10
+ "apikey",
11
+ ]);
12
+ // Undeclared flags carry no mark, so the name is the only signal; CLIs take secrets at a prompt (WC-SECR-5) and this
13
+ // catches the one typed anyway.
14
+ export const secretNamed = (name) => {
15
+ const words = name
16
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
17
+ .split(/[\s_.-]+/)
18
+ .map((word) => word.toLowerCase());
19
+ return words.some((word, at) => SECRET_WORDS.has(word) || (word === "api" && words[at + 1] === "key"));
20
+ };
21
+ const START = "(?<![A-Za-z0-9_-])";
22
+ const END = "(?![A-Za-z0-9_-])";
23
+ // Vendors print these prefixes so scanners can find their keys; a Stripe `pk_` key is public by design.
24
+ const KNOWN_TOKENS = new RegExp(`${[
25
+ "gh[pousr]_[A-Za-z0-9]{36,}",
26
+ "github_pat_[A-Za-z0-9_]{22,}",
27
+ "sk-[A-Za-z0-9_-]{20,}",
28
+ "xox[abprs]-[A-Za-z0-9-]{10,}",
29
+ "(?:AKIA|ASIA)[A-Z0-9]{16}",
30
+ "AIza[A-Za-z0-9_-]{35}",
31
+ "(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{10,}",
32
+ "npm_[A-Za-z0-9]{36}",
33
+ "eyJ[A-Za-z0-9_-]{5,}\\.eyJ[A-Za-z0-9_-]{5,}\\.[A-Za-z0-9_-]*",
34
+ ]
35
+ .map((pattern) => `${START}${pattern}${END}`)
36
+ .join("|")}|(?<!\\d)\\d{8,10}:[A-Za-z0-9_-]{35}${END}`, "g");
37
+ const PRIVATE_KEY = /-----BEGIN[A-Z ]* PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]* PRIVATE KEY-----|$)/g;
38
+ // Capitalised, as in a header, and not followed by a plain word: "the Basic plan" is somebody's words.
39
+ const SCHEME = /(?<![A-Za-z])(Bearer|Basic)(\s+)(?![A-Z]?[a-z]+(?![A-Za-z0-9]))[A-Za-z0-9._~+/=-]{8,}/g;
40
+ const NAMED_PAIR = /(?<![A-Za-z0-9])([A-Za-z0-9_.-]*(?:token|password|passphrase|secret|credential|cookie|authorization|api[_-]?key)[A-Za-z0-9_.-]*)(["']?\s*[:=]\s*["']?)((?:Bearer\s+|Basic\s+)?)([^\s"'&,;]+)/gi;
41
+ /**
42
+ * Whether a run of token characters is a random string rather than words: long, mixed case, high Shannon entropy,
43
+ * cut into short pieces (`Kz9vQ2mX`) where a name is cut into words (`InlineQueryResultMpeg4Gif`).
44
+ */
45
+ export const looksRandom = (run) => {
46
+ if (run.length < 20)
47
+ return false;
48
+ const classes = [/[a-z]/, /[A-Z]/, /\d/, /[^A-Za-z0-9]/].filter((kind) => kind.test(run)).length;
49
+ if (classes < 3)
50
+ return false;
51
+ let switches = 0;
52
+ for (let at = 1; at < run.length; at++) {
53
+ const [before, after] = [run[at - 1], run[at]];
54
+ if ((/[a-z]/.test(before) && /[A-Z]/.test(after)) || (/[A-Z]/.test(before) && /[a-z]/.test(after)))
55
+ switches += 1;
56
+ }
57
+ if (switches / run.length < 0.15)
58
+ return false;
59
+ const pieces = run.match(/[A-Z]?[a-z]+|[A-Z]+(?![a-z])|\d+/g) ?? [];
60
+ if (pieces.join("").length / Math.max(pieces.length, 1) >= 2.75)
61
+ return false;
62
+ return entropy(run) >= 3.5;
63
+ };
64
+ const entropy = (text) => {
65
+ const counts = new Map();
66
+ for (const char of text)
67
+ counts.set(char, (counts.get(char) ?? 0) + 1);
68
+ let bits = 0;
69
+ for (const count of counts.values())
70
+ bits -= (count / text.length) * Math.log2(count / text.length);
71
+ return bits;
72
+ };
73
+ /**
74
+ * Text with each secret inside it replaced by `mark` and the words around it kept: a token of a known vendor format,
75
+ * a private key, a `Bearer`/`Basic` credential, the value of a secret-named `name=value` or `name: value`, and a
76
+ * random-looking string of 20 or more printable characters (`looksRandom`).
77
+ */
78
+ export const withoutSecrets = (text, mark = "[redacted]") => text
79
+ .replace(PRIVATE_KEY, mark)
80
+ .replace(KNOWN_TOKENS, mark)
81
+ .replace(SCHEME, `$1$2${mark}`)
82
+ .replace(NAMED_PAIR, (whole, name, separator, scheme) => secretNamed(name) ? `${name}${separator}${scheme}${mark}` : whole)
83
+ .replace(/[!-~]{20,}/g, (token) =>
84
+ // A URL or a path is somebody's address, not a key; a credential in one is caught by the rules above.
85
+ token.includes("://") || /^(?:\/|~\/|\.\.?\/)/.test(token)
86
+ ? token
87
+ : token.replace(RUN, (run) => (looksRandom(run) ? mark : run)));
88
+ const RUN = /(?<![A-Za-z0-9_+/-])[A-Za-z0-9_+/-]{20,}/g;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.441.0",
3
+ "version": "0.442.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",