@misconfig/cli 1.0.1 → 1.3.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/engine/mask.js CHANGED
@@ -1,13 +1,18 @@
1
- // Secret masking, shared by the browser and the Worker.
1
+ // Secret masking. Likely secrets are replaced with «masked:<kind>:<length>» as
2
+ // files are read, before any check runs, so they never appear in results.
3
+ // Line breaks are kept, so line numbers still match the original file.
2
4
  //
3
- // Likely secrets are replaced with «masked:<kind>:<length>» before a file
4
- // leaves the browser. Line breaks are kept, so line numbers still match the
5
- // original file. The Worker runs the same masking again as a backstop.
5
+ // Every pattern here runs on untrusted files, so each one is written to take
6
+ // time in proportion to the text: no nested or adjacent open-ended repeats.
6
7
 
7
8
  const MARK_OPEN = "«masked:";
8
9
 
10
+ const KEY_BEGIN = /-----BEGIN [A-Z0-9 ]{0,40}PRIVATE KEY-----/g;
11
+ const KEY_END = /-----END [A-Z0-9 ]{0,40}PRIVATE KEY-----/y;
12
+
13
+ // Patterns with a leading group keep that character: it stops a match from
14
+ // starting in the middle of a longer token.
9
15
  const TOKEN_PATTERNS = [
10
- ["private-key", /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g],
11
16
  ["anthropic-key", /\bsk-ant-[A-Za-z0-9_-]{20,}/g],
12
17
  ["openai-key", /\bsk-(?:proj-|svcacct-|admin-)?[A-Za-z0-9_-]{20,}/g],
13
18
  ["github-token", /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})/g],
@@ -19,14 +24,18 @@ const TOKEN_PATTERNS = [
19
24
  ["groq-key", /\bgsk_[A-Za-z0-9]{20,}/g],
20
25
  ["huggingface-token", /\bhf_[A-Za-z0-9]{30,}/g],
21
26
  ["npm-token", /\bnpm_[A-Za-z0-9]{36}\b/g],
22
- ["jwt", /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/g],
27
+ ["jwt", /(^|[^A-Za-z0-9_-])(eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,})/g],
23
28
  ];
24
29
 
25
30
  // key = value, "key": "value", KEY=value, --api-key value ...
26
31
  const ASSIGNMENT =
27
- /((?:api[_-]?key|apikey|access[_-]?key|secret[_-]?key|access[_-]?token|auth[_-]?token|refresh[_-]?token|client[_-]?secret|token|secret|password|passwd|pwd|private[_-]?key)["']?\s*(?:[:=]|\s)\s*["']?)([^\s"'`,;{}()<>[\]«]{12,})/gi;
32
+ /((?:api[_-]?key|apikey|access[_-]?key|secret[_-]?key|access[_-]?token|auth[_-]?token|refresh[_-]?token|client[_-]?secret|token|secret|password|passwd|pwd|private[_-]?key)["']?(?:\s*[:=]\s*|\s+)["']?)([^\s"'`,;{}()<>[\]«»]{12,})/gi;
28
33
  const BEARER = /(\bBearer\s+)([A-Za-z0-9._~+/=-]{16,})/g;
29
- const URL_PASSWORD = /(\b[a-z][a-z0-9+.-]*:\/\/[^\s/:@"']{1,64}:)([^\s/@"']{6,})(?=@)/gi;
34
+ // The user name can be empty, as in redis://:password@host.
35
+ const URL_PASSWORD = /(\b[a-z][a-z0-9+.-]{0,30}:\/\/[^\s/:@"']{0,64}:)([^\s/@"']{6,})(?=@)/gi;
36
+ // Passwords are often short and plain, so a value named as one is masked from
37
+ // six characters: --password value, password: value, "DB_PASSWORD": "value".
38
+ const PASSWORD = /((?:--?(?:password|passwd|passphrase)[ \t]+|(?:password|passwd|passphrase)["']?[ \t]*[:=][ \t]*)["']?)([^\s"'`,;{}()<>[\]«»]{6,})/gi;
30
39
  const URL_TOKEN = /([?&](?:token|key|api[_-]?key|apikey|access[_-]?token|auth|secret|sig|signature|password)=)([^&#\s"']{8,})/gi;
31
40
 
32
41
  const PLACEHOLDER =
@@ -58,10 +67,7 @@ export function looksLikeSecret(value) {
58
67
  const v = value.trim();
59
68
  if (isMasked(v)) return true;
60
69
  if (v.length < 12 || PLACEHOLDER.test(v)) return false;
61
- for (const [, re] of TOKEN_PATTERNS) {
62
- re.lastIndex = 0;
63
- if (re.test(v)) return true;
64
- }
70
+ if (hasToken(v)) return true;
65
71
  const classes = [/[a-z]/, /[A-Z]/, /[0-9]/, /[^A-Za-z0-9]/].filter((re) => re.test(v)).length;
66
72
  return v.length >= 16 && classes >= 2 && entropy(v) >= 3.3 && !/\s/.test(v);
67
73
  }
@@ -81,14 +87,45 @@ const WORDY = /^(?:[A-Za-z]+[0-9]{0,2}|[0-9]{1,4})$/;
81
87
  export function looksLikeToken(value) {
82
88
  if (typeof value !== "string") return false;
83
89
  const v = value.trim();
84
- if (isMasked(v)) return true;
90
+ if (isMasked(v) || hasToken(v)) return true;
91
+ if (!/[0-9]/.test(v) || !/[A-Za-z]/.test(v) || isPathLike(v)) return false;
92
+ // Names and versions, as in mcp-server-fetch==2025.4.7 or tool[cli]>=1.2.
93
+ if (v.split(/[-_.:@+=<>!~,[\]]/).every((part) => !part || WORDY.test(part))) return false;
94
+ return looksLikeSecret(v);
95
+ }
96
+
97
+ function hasToken(v) {
98
+ KEY_BEGIN.lastIndex = 0;
99
+ if (KEY_BEGIN.test(v)) return true;
85
100
  for (const [, re] of TOKEN_PATTERNS) {
86
101
  re.lastIndex = 0;
87
102
  if (re.test(v)) return true;
88
103
  }
89
- if (!/[0-9]/.test(v) || !/[A-Za-z]/.test(v) || isPathLike(v)) return false;
90
- if (v.split(/[-_.:@+]/).every((part) => !part || WORDY.test(part))) return false;
91
- return looksLikeSecret(v);
104
+ return false;
105
+ }
106
+
107
+ // Private key blocks as [start, end] spans. Found with indexOf, so a file full
108
+ // of BEGIN lines without an END can't slow the check down.
109
+ function privateKeySpans(text) {
110
+ const spans = [];
111
+ KEY_BEGIN.lastIndex = 0;
112
+ for (let m = KEY_BEGIN.exec(text); m; m = KEY_BEGIN.exec(text)) {
113
+ let at = m.index + m[0].length;
114
+ let end = -1;
115
+ for (let i = text.indexOf("-----END ", at); i >= 0; i = text.indexOf("-----END ", at)) {
116
+ KEY_END.lastIndex = i;
117
+ const e = KEY_END.exec(text);
118
+ if (e) {
119
+ end = i + e[0].length;
120
+ break;
121
+ }
122
+ at = i + 9;
123
+ }
124
+ if (end < 0) break;
125
+ spans.push([m.index, end]);
126
+ KEY_BEGIN.lastIndex = end;
127
+ }
128
+ return spans;
92
129
  }
93
130
 
94
131
  export function maskSecrets(text) {
@@ -98,13 +135,28 @@ export function maskSecrets(text) {
98
135
  count++;
99
136
  kinds[kind] = (kinds[kind] || 0) + 1;
100
137
  };
138
+ const hide = (kind, match) => {
139
+ note(kind);
140
+ const newlines = (match.match(/\n/g) || []).length;
141
+ return marker(kind, match.length) + "\n".repeat(newlines);
142
+ };
101
143
 
102
- let out = text;
144
+ let out = String(text);
145
+ const spans = privateKeySpans(out);
146
+ if (spans.length) {
147
+ let rebuilt = "";
148
+ let last = 0;
149
+ for (const [start, end] of spans) {
150
+ rebuilt += out.slice(last, start) + hide("private-key", out.slice(start, end));
151
+ last = end;
152
+ }
153
+ out = rebuilt + out.slice(last);
154
+ }
103
155
  for (const [kind, re] of TOKEN_PATTERNS) {
104
- out = out.replace(re, (match) => {
105
- note(kind);
106
- const newlines = (match.match(/\n/g) || []).length;
107
- return marker(kind, match.length) + "\n".repeat(newlines);
156
+ out = out.replace(re, (match, ...groups) => {
157
+ if (typeof groups[0] !== "string") return hide(kind, match);
158
+ const [lead, token] = groups;
159
+ return lead + hide(kind, token);
108
160
  });
109
161
  }
110
162
  const keepPrefix = (kind) => (match, prefix, value) => {
@@ -114,6 +166,7 @@ export function maskSecrets(text) {
114
166
  return prefix + marker(kind, value.length);
115
167
  };
116
168
  out = out.replace(URL_PASSWORD, keepPrefix("url-password"));
169
+ out = out.replace(PASSWORD, keepPrefix("password"));
117
170
  out = out.replace(URL_TOKEN, keepPrefix("url-token"));
118
171
  out = out.replace(BEARER, keepPrefix("bearer-token"));
119
172
  out = out.replace(ASSIGNMENT, keepPrefix("assigned-secret"));
@@ -0,0 +1,92 @@
1
+ // Files that settings name as instructions for an agent, beyond the usual
2
+ // names: Gemini CLI's context file names, Codex's fallback names for
3
+ // AGENTS.md, OpenCode's instructions list and Aider's read list. A project
4
+ // can use them to keep its instructions in a file the usual names don't
5
+ // cover, so the browser check, the command and the checks all read them as
6
+ // instructions. Shared by all three.
7
+
8
+ import { parseJSONC, parseTOML, parseYAML } from "./parse.js";
9
+ import { globMatch } from "./glob.js";
10
+
11
+ const names = (v) =>
12
+ (typeof v === "string" ? [v] : Array.isArray(v) ? v.filter((x) => typeof x === "string") : [])
13
+ .map((x) => x.trim())
14
+ .filter((x) => x && x.length <= 300)
15
+ .slice(0, 50);
16
+
17
+ // For each settings file: where it is, the agent, how to read it, the names
18
+ // it gives, and whether a name is a file name found anywhere ("name"), or a
19
+ // path or glob from the top of the project ("path").
20
+ const SOURCES = [
21
+ { re: /(^|\/)\.gemini\/settings\.json$/i, tool: "Gemini CLI", parse: parseJSONC, list: (j) => names(j?.context?.fileName ?? j?.contextFileName), match: "name" },
22
+ { re: /(^|\/)\.codex\/config\.toml$/i, tool: "Codex", parse: parseTOML, list: (t) => names(t?.project_doc_fallback_filenames), match: "name" },
23
+ { re: /^(?:\.opencode\/)?opencode\.jsonc?$/i, tool: "OpenCode", parse: parseJSONC, list: (j) => names(j?.instructions), match: "path" },
24
+ { re: /(^|\/)\.aider\.conf\.ya?ml$/i, tool: "Aider", parse: parseYAML, list: (y) => names(y?.read), match: "path" },
25
+ ];
26
+
27
+ export const NAMING_SETTINGS = SOURCES.map((s) => s.re);
28
+
29
+ // A name that points out of the project, or to a web address.
30
+ export const outsideName = (name) => /^(?:~|\/|[A-Za-z]:|\\\\)/.test(name) || /(^|[\\/])\.\.([\\/]|$)/.test(name);
31
+ export const webName = (name) => /^https?:\/\//i.test(name);
32
+
33
+ // What the settings files given as { path, content } name:
34
+ // [{ tool, from, name, outside, web, test(path) }].
35
+ export function namedInstructions(files) {
36
+ const out = [];
37
+ for (const f of Array.isArray(files) ? files : []) {
38
+ if (typeof f?.path !== "string" || typeof f.content !== "string") continue;
39
+ const source = SOURCES.find((s) => s.re.test(f.path));
40
+ if (!source) continue;
41
+ let value;
42
+ try {
43
+ value = source.parse(f.content);
44
+ } catch {
45
+ continue;
46
+ }
47
+ for (const name of source.list(value)) {
48
+ const plain = name.replace(/\\/g, "/").replace(/^\.\//, "");
49
+ const outside = outsideName(name);
50
+ const web = webName(name);
51
+ const test =
52
+ outside || web
53
+ ? () => false
54
+ : source.match === "name"
55
+ ? (path) => path.split("/").pop().toLowerCase() === plain.split("/").pop().toLowerCase()
56
+ : (path) => path.toLowerCase() === plain.toLowerCase() || (/[*?[]/.test(plain) && globMatch(plain, path));
57
+ out.push({ tool: source.tool, from: f.path, name, outside, web, test });
58
+ if (out.length >= 200) return out;
59
+ }
60
+ }
61
+ return out;
62
+ }
63
+
64
+ // The entries a collector found that the names pick out, without testing
65
+ // every name against every file: a name is looked up, an exact path too, and
66
+ // globs are tried only on text files. `pathOf` gives an entry's project path.
67
+ const TEXT = /\.(?:md|mdc|markdown|txt|rst|adoc)$/i;
68
+ export function pickNamed(named, entries, pathOf) {
69
+ if (!named.length) return [];
70
+ const byName = new Map();
71
+ const byPath = new Map();
72
+ for (const e of entries) {
73
+ const p = pathOf(e);
74
+ const base = p.split("/").pop().toLowerCase();
75
+ if (!byName.has(base)) byName.set(base, []);
76
+ byName.get(base).push(e);
77
+ byPath.set(p.toLowerCase(), e);
78
+ }
79
+ const picked = new Set();
80
+ for (const n of named.filter((x) => !x.outside && !x.web)) {
81
+ const plain = n.name.replace(/\\/g, "/").replace(/^\.\//, "");
82
+ if (/[*?[]/.test(plain)) {
83
+ for (const e of entries) if (TEXT.test(pathOf(e)) && n.test(pathOf(e))) picked.add(e);
84
+ } else {
85
+ for (const e of byName.get(plain.split("/").pop().toLowerCase()) ?? []) if (n.test(pathOf(e))) picked.add(e);
86
+ const exact = byPath.get(plain.toLowerCase());
87
+ if (exact && n.test(pathOf(exact))) picked.add(exact);
88
+ }
89
+ if (picked.size >= 50) break;
90
+ }
91
+ return [...picked];
92
+ }