@nightloom/isy 1.0.3 → 1.0.4

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/src/mask-paths.ts CHANGED
@@ -102,8 +102,12 @@ const SEG = /[^\s"'`<>:;,|&$*?()[\]{}\\/]/;
102
102
  /**
103
103
  * A scheme makes it a URL, and `https://example.com/home/alice/x` names a page
104
104
  * rather than this machine. Matched first, so no path rule sees a hostname.
105
+ * The scheme is capped for the reason `redact.ts` caps the connection string's:
106
+ * unbounded, every letter of a long run with no `://` in it started a scan of
107
+ * the rest of the run. A longer scheme still matches on its tail, and the URL
108
+ * is still kept away from the path rules.
105
109
  */
106
- const URL_LIKE = /[A-Za-z][A-Za-z0-9+.-]*:\/\/[^\s"'`<>]*/;
110
+ const URL_LIKE = /[A-Za-z][A-Za-z0-9+.-]{0,31}:\/\/[^\s"'`<>]*/;
107
111
 
108
112
  /**
109
113
  * The two schemes that are the exception: they carry a path on this machine
package/src/parser.ts CHANGED
@@ -208,7 +208,7 @@ function pushLookup(state: ParseState, use: ToolUse): void {
208
208
  * A heredoc opener: `<<EOF`, `<<-EOF`, `<<'PY'`. The delimiter is what ends the
209
209
  * body, and quoting it changes nothing about where the body stops.
210
210
  */
211
- const HEREDOC = /<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/;
211
+ export const HEREDOC = /<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/;
212
212
 
213
213
  /**
214
214
  * The command with every heredoc body cut out.
package/src/redact.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { homedir } from "node:os";
2
2
  import { basename } from "node:path";
3
3
  import { fileLines } from "./lines.js";
4
- import { commandSegments, withoutHeredocs } from "./parser.js";
4
+ import { HEREDOC, commandSegments, withoutHeredocs } from "./parser.js";
5
5
  import {
6
6
  PATH_KEYS,
7
7
  REDACTION_MARKER,
@@ -49,10 +49,12 @@ const CAMEL_NAME = secretName("(?<=[a-z0-9])(?=[A-Z])");
49
49
 
50
50
  /**
51
51
  * What an assigned secret looks like: long enough to be one, and not code. A
52
- * number never is — usage counts sit under names like these in every record,
53
- * and stdout quotes them as JSON text.
52
+ * bare number under a quoted name is JSON's own (`"token": 1234567890`), where a
53
+ * secret would be a string; anywhere else a number is a password like any other
54
+ * — `DB_PASSWORD=1234567890123`, `MYSQL_ROOT_PASSWORD: 12345678`. The digit is
55
+ * looked for first so the lookbehind runs only where a number starts.
54
56
  */
55
- const SECRET_VALUE = String.raw`(?!\[ISY_REDACTED)(?!-?\d[\d.,eE+-]*(?![^\s"'{}()[\]<>$]))[^\s"'{}()[\]<>$]{8,}`;
57
+ const SECRET_VALUE = String.raw`(?!\[ISY_REDACTED)(?!(?=-?\d)(?<=["']\s*:\s*)-?\d[\d.,eE+-]*(?![^\s"'{}()[\]<>$]))[^\s"'{}()[\]<>$]{8,}`;
56
58
 
57
59
  export const BUILTIN_RULES: RedactionRule[] = [
58
60
  {
@@ -245,6 +247,102 @@ function readsDotenv(command: unknown): boolean {
245
247
  );
246
248
  }
247
249
 
250
+ /** Commands that write the file they are given: `tee` and PowerShell's own. */
251
+ const SHELL_WRITER = /^\s*(?:sudo\s+)?(?:tee|set-content|sc|add-content|ac|out-file)(?:\s|$)/i;
252
+
253
+ const unquoted = (word: string): string => word.replace(/["'`]/g, "");
254
+
255
+ /** The dotenv file a statement redirects or tees into, if it writes one. */
256
+ function dotenvTarget(statement: string): string | undefined {
257
+ for (const segment of commandSegments(statement)) {
258
+ for (const redirect of segment.matchAll(/>\|?\s*([^\s;&|<>]+)/g)) {
259
+ if (isDotenvPath(unquoted(redirect[1]!))) return unquoted(redirect[1]!);
260
+ }
261
+ if (!SHELL_WRITER.test(segment)) continue;
262
+ const named = segment.trim().split(/\s+/).slice(1).map(unquoted).find(isDotenvPath);
263
+ if (named) return named;
264
+ }
265
+ return undefined;
266
+ }
267
+
268
+ /**
269
+ * A line cut where `&&`, `||` or `;` end a statement outside quotes, the
270
+ * separators kept, and whether a quote is still open where the line ends.
271
+ */
272
+ function statements(line: string): { parts: string[]; open: boolean } {
273
+ const parts: string[] = [];
274
+ let start = 0;
275
+ let quote: string | undefined;
276
+ for (let i = 0; i < line.length; i += 1) {
277
+ const char = line[i]!;
278
+ if (quote !== undefined) {
279
+ if (char === quote && (quote === "'" || line[i - 1] !== "\\")) quote = undefined;
280
+ } else if (char === '"' || char === "'" || char === "`") {
281
+ quote = char;
282
+ } else if (char === ";" || ((char === "&" || char === "|") && line[i + 1] === char)) {
283
+ const width = char === ";" ? 1 : 2;
284
+ parts.push(line.slice(start, i), line.slice(i, i + width));
285
+ start = i + width;
286
+ i += width - 1;
287
+ }
288
+ }
289
+ parts.push(line.slice(start));
290
+ return { parts, open: quote !== undefined };
291
+ }
292
+
293
+ /** A write that closes a group or a substitution: `{ …; } > .env`, `(…) | tee .env`. */
294
+ const GROUP_WRITE = /[})]\s*(?:\d?>|&>|\|)/;
295
+
296
+ /**
297
+ * `cat > .env << 'EOF'`, `echo "KEY=…" >> .env.local`, `… | tee .env`: a shell
298
+ * call that writes a dotenv file holds what it writes, so unlike a read the
299
+ * command is cut too. Only what carries the values goes — the statement that
300
+ * writes, with the heredoc it feeds, and a heredoc body that names the file
301
+ * itself (`python - <<PY` opening `.env`) — and the statement keeps its target,
302
+ * so `sed -i` beside it is still an edit and the parse still sees a write.
303
+ */
304
+ function cutDotenvWrites(command: string): string {
305
+ if (!command.includes(".env")) return command;
306
+ const lines = command.split("\n");
307
+ const kept: string[] = [];
308
+ let written: string | undefined;
309
+ let spread = false;
310
+ for (let i = 0; i < lines.length; i += 1) {
311
+ let writes = false;
312
+ const { parts, open } = statements(lines[i]!);
313
+ if (open) spread = true;
314
+ kept.push(
315
+ parts
316
+ .map((part) => {
317
+ const target = dotenvTarget(part);
318
+ if (target === undefined) return part;
319
+ writes = true;
320
+ written ??= target;
321
+ if (GROUP_WRITE.test(part)) spread = true;
322
+ return `${/^\s*/.exec(part)![0]}${marker("dotenv")} > ${target}`;
323
+ })
324
+ .join(""),
325
+ );
326
+ const terminator = HEREDOC.exec(lines[i]!)?.[2];
327
+ if (terminator === undefined) continue;
328
+ let end = lines.findIndex((line, j) => j > i && line.trim() === terminator);
329
+ if (end === -1) end = lines.length;
330
+ const body = lines.slice(i + 1, end);
331
+ if (writes) {
332
+ i = end;
333
+ continue;
334
+ }
335
+ const namesDotenv = body.some((line) => line.split(/[\s"'`()=,;|&<>]+/).some(isDotenvPath));
336
+ kept.push(...(namesDotenv ? [marker("dotenv")] : body), ...lines.slice(end, end + 1));
337
+ i = end;
338
+ }
339
+ // A group or a quoted string over several lines holds the values outside the
340
+ // statement that names the file (`{ echo A=1; echo B=2; } > .env`), so the
341
+ // whole command goes, and only the write is left for the parse to see.
342
+ if (written !== undefined && spread) return `${marker("dotenv")} > ${written}`;
343
+ return kept.join("\n");
344
+ }
345
+
248
346
  function cutDotenvInput(input: Record<string, unknown>, context: RedactContext): void {
249
347
  for (const field of EDIT_INPUT_FIELDS) {
250
348
  if (typeof input[field] === "string") {
@@ -265,8 +363,15 @@ function scanDotenv(record: Record<string, unknown>, context: RedactContext): vo
265
363
  if (block.type === "tool_use") {
266
364
  const input = isObject(block.input) ? block.input : undefined;
267
365
  if (!input) continue;
268
- if ((block.name === "Bash" || block.name === "PowerShell") && readsDotenv(input.command)) {
366
+ if (block.name === "Bash" || block.name === "PowerShell") {
367
+ const command = input.command;
368
+ const cut = typeof command === "string" ? cutDotenvWrites(command) : command;
369
+ if (cut === command && !readsDotenv(command)) continue;
269
370
  if (typeof block.id === "string") context.envToolUseIds.add(block.id);
371
+ if (cut !== command) {
372
+ input.command = cut;
373
+ count(context, "dotenv", 1);
374
+ }
270
375
  continue;
271
376
  }
272
377
  if (!isDotenvPath(input.file_path) && !isDotenvPath(input.notebook_path)) continue;
@@ -344,9 +449,22 @@ function learnRoot(record: Record<string, unknown>, context: RedactContext): voi
344
449
  * `bridge-session` and `artifact-autoreact-ledger` are cut the same way: one
345
450
  * names the account and organisation that own the session's bridge, the other
346
451
  * the account. The parser reads none of them past their type, so only the
347
- * fields that name them go.
452
+ * fields that name them go — and the ones that say a subagent made the request
453
+ * (`subagents.ts:asSidechain`), or it is counted as the session's own.
348
454
  */
349
- const REQUEST_RECORD_KEYS = new Set(["type", "uuid", "parentUuid", "sessionId", "timestamp", "version", "shapeHash", "hash"]);
455
+ const REQUEST_RECORD_KEYS = new Set([
456
+ "type",
457
+ "uuid",
458
+ "parentUuid",
459
+ "sessionId",
460
+ "timestamp",
461
+ "version",
462
+ "shapeHash",
463
+ "hash",
464
+ "isSidechain",
465
+ "parentToolUseID",
466
+ "agentId",
467
+ ]);
350
468
  const STRIPPED_RECORD_TYPES = new Set([
351
469
  "api-request",
352
470
  "api-request-shape",