neurastream 0.2.26 → 0.2.28
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 +33 -1
- package/package.json +1 -1
- package/src/adapters/omp/doctor.ts +10 -0
- package/src/adapters/omp/index.ts +62 -6
- package/src/adapters/omp/launch.ts +30 -1
- package/src/cli/dispatch.ts +29 -0
- package/src/core/config.ts +30 -0
- package/src/core/notify.ts +6 -1
- package/src/core/owner-ui.ts +382 -72
- package/src/core/secret-redact.ts +317 -0
- package/templates/config.yml +6 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// Secrets never reach the model, the chat or the session log (plan 20261025_2204, incident
|
|
2
|
+
// I-20261025-197). Detection is by CONTENT, not by file name: gitleaks-style shapes plus the exact
|
|
3
|
+
// values this machine holds (env vars, ~/.npmrc, dotenv files under the work roots, notify webhook
|
|
4
|
+
// URLs). Every hit becomes `[REDACTED:<type>]`; the value itself never appears in the returned
|
|
5
|
+
// text, in the hit list or in a log line — the hits carry a type and a count only.
|
|
6
|
+
//
|
|
7
|
+
// Pure TypeScript, no harness and no agent: the harness adapter (tool_result), notify.ts (outbound
|
|
8
|
+
// posts), `neurastream redact` and doctor all call this one module.
|
|
9
|
+
|
|
10
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { join } from "node:path";
|
|
13
|
+
import type { Config } from "./config.ts";
|
|
14
|
+
|
|
15
|
+
/** One owner-defined shape: the marker type and its regex source. */
|
|
16
|
+
export interface SecretPattern {
|
|
17
|
+
type: string;
|
|
18
|
+
regex: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface RedactHit {
|
|
22
|
+
type: string;
|
|
23
|
+
count: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RedactResult {
|
|
27
|
+
text: string;
|
|
28
|
+
hits: RedactHit[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Rule {
|
|
32
|
+
/** Named in `[REDACTED:<type>]` and in the hit summary. */
|
|
33
|
+
type: string;
|
|
34
|
+
regex: RegExp;
|
|
35
|
+
/** Leading capture groups kept verbatim (a key name, an assignment prefix) before the marker. */
|
|
36
|
+
keep?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Shape rules in application order: a structural prefix (`_authToken=`, a URL's user part) is
|
|
40
|
+
// recognised before the bare token shapes, and the generic assignment rule runs after both so a
|
|
41
|
+
// token keeps its own type name. Every regex is linear: no nested quantifier over the same text.
|
|
42
|
+
// The `(?!\[REDACTED:)` lookahead keeps a rule from matching a value an earlier rule already
|
|
43
|
+
// replaced — `OPENAI_API_KEY=[REDACTED:sk-key]` must not become a generic hit.
|
|
44
|
+
const RULES: Rule[] = [
|
|
45
|
+
{ type: "private-key", regex: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z0-9 ]*PRIVATE KEY-----/g },
|
|
46
|
+
{ type: "npmrc-auth-token", regex: /(_authToken\s*=\s*)(?!\[REDACTED:)\S+/g, keep: 1 },
|
|
47
|
+
{ type: "npmrc-password", regex: /(_password\s*=\s*)(?!\[REDACTED:)\S+/g, keep: 1 },
|
|
48
|
+
{ type: "url-password", regex: /\b([a-z][a-z0-9+.-]*:\/\/[^\s/@:]+:)(?!\[REDACTED:)[^\s/@]+(?=@)/gi, keep: 1 },
|
|
49
|
+
{ type: "aws-secret-access-key", regex: /(aws_secret_access_key\s*[:=]\s*['"]?)(?!\[REDACTED:)[A-Za-z0-9/+=]{16,}/gi, keep: 1 },
|
|
50
|
+
{ type: "aws-access-key-id", regex: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g },
|
|
51
|
+
{ type: "github-token", regex: /\b(?:gh[pousr]_[A-Za-z0-9]{16,}|github_pat_[A-Za-z0-9_]{16,})\b/g },
|
|
52
|
+
{ type: "gitlab-token", regex: /\bglpat-[A-Za-z0-9_-]{16,}\b/g },
|
|
53
|
+
{ type: "npm-token", regex: /\bnpm_[A-Za-z0-9]{36}\b/g },
|
|
54
|
+
{ type: "sk-key", regex: /\bsk-(?:ant-|or-|proj-)?[A-Za-z0-9_-]{16,}\b/g },
|
|
55
|
+
{ type: "google-api-key", regex: /\bAIza[0-9A-Za-z_-]{35}\b/g },
|
|
56
|
+
{ type: "slack-token", regex: /\bxox[abpr]-[A-Za-z0-9-]{10,}\b/g },
|
|
57
|
+
{ type: "discord-webhook", regex: /\bhttps?:\/\/(?:ptb\.|canary\.)?discord(?:app)?\.com\/api\/webhooks\/\d+\/[A-Za-z0-9_-]+/g },
|
|
58
|
+
{ type: "stripe-key", regex: /\b[rs]k_(?:live|test)_[A-Za-z0-9]{10,}\b/g },
|
|
59
|
+
{ type: "jwt", regex: /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g },
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
// Runs after the shapes above and after the owner's own patterns, so a specific shape names the
|
|
63
|
+
// value first; only what nothing else recognised becomes `generic-secret`. The key name may be
|
|
64
|
+
// a multi-part name (`DB_PASSWORD=`, `MY_APP_TOKEN:`): the identifier is kept whole, so the reader still
|
|
65
|
+
// sees which key was hidden, and only the value goes.
|
|
66
|
+
const GENERIC_RULE: Rule = {
|
|
67
|
+
type: "generic-secret",
|
|
68
|
+
regex: /([A-Za-z0-9_.-]*(?:password|passwd|secret|token|api[_-]?key|access[_-]?key|client[_-]?secret))(\s*[:=]\s*)(['"]?)(?!\[REDACTED:)([^\s'",;]{8,})/gi,
|
|
69
|
+
keep: 3,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** The external proxy that can sit on the wire as a second net (plan 20261025_2204, P3). */
|
|
73
|
+
export const PROXY_BIN = "secretgate";
|
|
74
|
+
|
|
75
|
+
/** Whether the proxy binary is on PATH. Never spawned here: launch and doctor only need to know. */
|
|
76
|
+
export function proxyOnPath(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
77
|
+
for (const dir of (env.PATH ?? "").split(":")) {
|
|
78
|
+
if (dir === "") continue;
|
|
79
|
+
try {
|
|
80
|
+
if (statSync(join(dir, PROXY_BIN)).isFile()) return true;
|
|
81
|
+
} catch {}
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** A known value shorter than this is not redacted: it would shred ordinary text. */
|
|
87
|
+
const MIN_KNOWN_LEN = 8;
|
|
88
|
+
|
|
89
|
+
/** Env var names whose values count as secrets. */
|
|
90
|
+
const SECRET_ENV_NAME = /KEY|TOKEN|SECRET|PASSWORD|PASS|AUTH|CREDENTIAL|WEBHOOK/i;
|
|
91
|
+
|
|
92
|
+
/** A dotenv file at the top of a repo: `.` + `env` and anything after it (`.env`, `.env.local`). */
|
|
93
|
+
const DOTENV_NAME = /^\.env/;
|
|
94
|
+
|
|
95
|
+
/** Files larger than this are not scanned for values: no env file or webhook file is that big. */
|
|
96
|
+
const MAX_FILE_BYTES = 1024 * 1024;
|
|
97
|
+
|
|
98
|
+
function bump(counts: Map<string, number>, type: string): void {
|
|
99
|
+
counts.set(type, (counts.get(type) ?? 0) + 1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function escapeRegExp(value: string): string {
|
|
103
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const extraCache = new Map<string, RegExp>();
|
|
107
|
+
|
|
108
|
+
/** Owner-defined shapes, built once per process; a bad regex cannot reach here (config.check). */
|
|
109
|
+
function extraRules(extra: SecretPattern[]): Rule[] {
|
|
110
|
+
const out: Rule[] = [];
|
|
111
|
+
for (const p of extra) {
|
|
112
|
+
if (typeof p?.type !== "string" || p.type === "" || typeof p.regex !== "string" || p.regex === "") continue;
|
|
113
|
+
let re = extraCache.get(p.regex);
|
|
114
|
+
if (!re) {
|
|
115
|
+
try {
|
|
116
|
+
re = new RegExp(p.regex, "g");
|
|
117
|
+
} catch {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
extraCache.set(p.regex, re);
|
|
121
|
+
}
|
|
122
|
+
out.push({ type: p.type, regex: re });
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function applyRule(text: string, rule: Rule, counts: Map<string, number>): string {
|
|
128
|
+
const keep = rule.keep ?? 0;
|
|
129
|
+
return text.replace(rule.regex, (...args: unknown[]) => {
|
|
130
|
+
const prefix = args
|
|
131
|
+
.slice(1, 1 + keep)
|
|
132
|
+
.map(g => (typeof g === "string" ? g : ""))
|
|
133
|
+
.join("");
|
|
134
|
+
bump(counts, rule.type);
|
|
135
|
+
return `${prefix}[REDACTED:${rule.type}]`;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Exact values, deduped, longest first so a value that contains another is matched whole. */
|
|
140
|
+
function knownPattern(values: Iterable<string>): RegExp | null {
|
|
141
|
+
const seen = new Set<string>();
|
|
142
|
+
for (const value of values) {
|
|
143
|
+
const text = value.trim();
|
|
144
|
+
if (text.length >= MIN_KNOWN_LEN) seen.add(text);
|
|
145
|
+
}
|
|
146
|
+
if (seen.size === 0) return null;
|
|
147
|
+
const pattern = [...seen]
|
|
148
|
+
.sort((a, b) => b.length - a.length || (a < b ? -1 : a > b ? 1 : 0))
|
|
149
|
+
.map(escapeRegExp)
|
|
150
|
+
.join("|");
|
|
151
|
+
return new RegExp(pattern, "g");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Replaces every secret in `text` with `[REDACTED:<type>]` and reports what it hid. `known` holds
|
|
156
|
+
* exact values (see `loadKnownSecrets`); `extra` holds the owner's own shapes. Running it twice on
|
|
157
|
+
* the same text is stable: a marker matches no rule.
|
|
158
|
+
*/
|
|
159
|
+
export function redactSecrets(text: string, known: Iterable<string> = [], extra: SecretPattern[] = []): RedactResult {
|
|
160
|
+
if (text === "") return { text, hits: [] };
|
|
161
|
+
const counts = new Map<string, number>();
|
|
162
|
+
let out = text;
|
|
163
|
+
for (const rule of RULES) out = applyRule(out, rule, counts);
|
|
164
|
+
for (const rule of extraRules(extra)) out = applyRule(out, rule, counts);
|
|
165
|
+
out = applyRule(out, GENERIC_RULE, counts);
|
|
166
|
+
const knownRe = knownPattern(known);
|
|
167
|
+
if (knownRe) {
|
|
168
|
+
out = out.replace(knownRe, () => {
|
|
169
|
+
bump(counts, "known-value");
|
|
170
|
+
return "[REDACTED:known-value]";
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return { text: out, hits: [...counts].map(([type, count]) => ({ type, count })) };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The one line the adapter adds to a tool result when it hid anything. Types only, never values. */
|
|
177
|
+
export function redactSummary(hits: RedactHit[]): string {
|
|
178
|
+
const total = hits.reduce((n, h) => n + h.count, 0);
|
|
179
|
+
return `secret-redact: hid ${total} value(s) (${hits.map(h => h.type).join(", ")})`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// --- known values ---
|
|
183
|
+
|
|
184
|
+
interface CachedFile {
|
|
185
|
+
mtimeMs: number;
|
|
186
|
+
size: number;
|
|
187
|
+
values: string[];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
type FileKind = "npmrc" | "env" | "text";
|
|
191
|
+
|
|
192
|
+
const fileCache = new Map<string, CachedFile>();
|
|
193
|
+
let discovered: { roots: string; atMs: number; files: string[] } | null = null;
|
|
194
|
+
/** The work-root dotenv list is a directory walk, not a file read: rescan at this interval. */
|
|
195
|
+
const DISCOVERY_TTL_MS = 60_000;
|
|
196
|
+
const MAX_REPO_DIRS = 500;
|
|
197
|
+
|
|
198
|
+
/** Forgets every cached file and the discovered dotenv list (tests switch sandboxes). */
|
|
199
|
+
export function clearKnownSecretsCache(): void {
|
|
200
|
+
fileCache.clear();
|
|
201
|
+
discovered = null;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** `_authToken` and `_password` values of an npmrc. */
|
|
205
|
+
function npmrcValues(text: string): string[] {
|
|
206
|
+
const out: string[] = [];
|
|
207
|
+
for (const line of text.split("\n")) {
|
|
208
|
+
const m = /^\s*(?:\/\/[^\s=]*:)?_(?:authToken|password)\s*=\s*(\S+)/i.exec(line);
|
|
209
|
+
if (m?.[1]) out.push(m[1]);
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** `KEY=value` values of an env-style file (quotes stripped, comments ignored). */
|
|
215
|
+
function envValues(text: string): string[] {
|
|
216
|
+
const out: string[] = [];
|
|
217
|
+
for (const line of text.split("\n")) {
|
|
218
|
+
const m = /^\s*(?:export\s+)?[A-Za-z_][A-Za-z0-9_]*\s*=\s*(.+)$/.exec(line);
|
|
219
|
+
if (!m?.[1]) continue;
|
|
220
|
+
let value = m[1].trim();
|
|
221
|
+
if (value.length >= 2 && (value[0] === '"' || value[0] === "'") && value.endsWith(value[0])) value = value.slice(1, -1);
|
|
222
|
+
const hash = value.search(/\s#/);
|
|
223
|
+
if (hash !== -1) value = value.slice(0, hash).trim();
|
|
224
|
+
if (value !== "") out.push(value);
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function fileValues(path: string, kind: FileKind): string[] {
|
|
230
|
+
const key = `${kind}\0${path}`;
|
|
231
|
+
let size: number;
|
|
232
|
+
let mtimeMs: number;
|
|
233
|
+
try {
|
|
234
|
+
const st = statSync(path);
|
|
235
|
+
if (!st.isFile() || st.size > MAX_FILE_BYTES) return [];
|
|
236
|
+
size = st.size;
|
|
237
|
+
mtimeMs = st.mtimeMs;
|
|
238
|
+
} catch {
|
|
239
|
+
fileCache.delete(key);
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
const hit = fileCache.get(key);
|
|
243
|
+
if (hit && hit.mtimeMs === mtimeMs && hit.size === size) return hit.values;
|
|
244
|
+
let text: string;
|
|
245
|
+
try {
|
|
246
|
+
text = readFileSync(path, "utf8");
|
|
247
|
+
} catch {
|
|
248
|
+
return hit?.values ?? [];
|
|
249
|
+
}
|
|
250
|
+
const values = kind === "npmrc" ? npmrcValues(text) : kind === "env" ? envValues(text) : [text.trim()];
|
|
251
|
+
fileCache.set(key, { mtimeMs, size, values });
|
|
252
|
+
return values;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Dotenv files at the top of `dir` (its own, not its subdirectories'). */
|
|
256
|
+
function envFilesIn(dir: string): string[] {
|
|
257
|
+
let entries;
|
|
258
|
+
try {
|
|
259
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
260
|
+
} catch {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
return entries.filter(e => e.isFile() && DOTENV_NAME.test(e.name)).map(e => join(dir, e.name));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Every top-level dotenv file under a work root, plus each repo directory's own. */
|
|
267
|
+
function workRootEnvFiles(roots: string[]): string[] {
|
|
268
|
+
const key = roots.join("\0");
|
|
269
|
+
const nowMs = Date.now();
|
|
270
|
+
if (discovered && discovered.roots === key && nowMs - discovered.atMs < DISCOVERY_TTL_MS) return discovered.files;
|
|
271
|
+
const files = new Set<string>();
|
|
272
|
+
for (const root of roots) {
|
|
273
|
+
for (const file of envFilesIn(root)) files.add(file);
|
|
274
|
+
let repos;
|
|
275
|
+
try {
|
|
276
|
+
repos = readdirSync(root, { withFileTypes: true });
|
|
277
|
+
} catch {
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
let seen = 0;
|
|
281
|
+
for (const entry of repos) {
|
|
282
|
+
if (!entry.isDirectory() || entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
283
|
+
if (seen++ >= MAX_REPO_DIRS) break;
|
|
284
|
+
for (const file of envFilesIn(join(root, entry.name))) files.add(file);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const list = [...files];
|
|
288
|
+
discovered = { roots: key, atMs: nowMs, files: list };
|
|
289
|
+
return list;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** The notify webhook files: the URL in one is a secret like any other (config holds only paths). */
|
|
293
|
+
function notifyFiles(cfg: Config): string[] {
|
|
294
|
+
return [cfg.notify.discord_webhook_file, cfg.notify.slack_webhook_file].filter(f => typeof f === "string" && f !== "");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The exact values this machine holds, so a secret is hidden wherever it appears whatever the file
|
|
299
|
+
* is called: env vars whose name looks like a credential, `~/.npmrc`, `secrets.known_value_files`,
|
|
300
|
+
* every top-level dotenv file under `paths.work_roots`, and the notify webhook files. File contents
|
|
301
|
+
* are cached per process and re-read when a file's mtime or size changes.
|
|
302
|
+
*/
|
|
303
|
+
export function loadKnownSecrets(cfg: Config, env: NodeJS.ProcessEnv = process.env): string[] {
|
|
304
|
+
const out = new Set<string>();
|
|
305
|
+
const add = (value: string): void => {
|
|
306
|
+
const text = value.trim();
|
|
307
|
+
if (text.length >= MIN_KNOWN_LEN) out.add(text);
|
|
308
|
+
};
|
|
309
|
+
for (const [name, value] of Object.entries(env)) {
|
|
310
|
+
if (typeof value === "string" && SECRET_ENV_NAME.test(name)) add(value);
|
|
311
|
+
}
|
|
312
|
+
for (const value of fileValues(join(homedir(), ".npmrc"), "npmrc")) add(value);
|
|
313
|
+
for (const file of cfg.secrets.known_value_files) for (const value of fileValues(file, "env")) add(value);
|
|
314
|
+
for (const file of workRootEnvFiles(cfg.paths.work_roots)) for (const value of fileValues(file, "env")) add(value);
|
|
315
|
+
for (const file of notifyFiles(cfg)) for (const value of fileValues(file, "text")) add(value);
|
|
316
|
+
return [...out];
|
|
317
|
+
}
|
package/templates/config.yml
CHANGED
|
@@ -186,6 +186,12 @@ notify:
|
|
|
186
186
|
slack_webhook_file: "" # file holding the Slack webhook URL; "" = off
|
|
187
187
|
prefix: "" # optional label prepended to each posted reply, e.g. "Main:"
|
|
188
188
|
|
|
189
|
+
secrets: # content redaction (src/core/secret-redact.ts); a hit prints as [REDACTED:<type>]
|
|
190
|
+
redact: true # every tool result, every notify post and `neurastream redact` go through the redactor
|
|
191
|
+
known_value_files: ["~/.npmrc"] # files whose KEY=value lines are secret values; every top-level `.env…` file under paths.work_roots is read too
|
|
192
|
+
extra_patterns: [] # your own shapes, e.g. [{type: "my-token", regex: "myapp_[A-Za-z0-9]{32}"}]
|
|
193
|
+
proxy: "none" # "none" | "secretgate": launch the harness as `secretgate wrap -- omp` (README: Secrets)
|
|
194
|
+
|
|
189
195
|
priority:
|
|
190
196
|
sleep_sec: 120
|
|
191
197
|
|