pi-crew 0.5.5 → 0.5.7

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +153 -0
  2. package/README.md +17 -1
  3. package/docs/architecture.md +2 -0
  4. package/docs/migration-v0.4-v0.5.md +19 -2
  5. package/docs/pi-crew-v0.5.5-audit-fix-plan.md +133 -0
  6. package/package.json +7 -5
  7. package/src/benchmark/benchmark-runner.ts +45 -0
  8. package/src/benchmark/feedback-loop.ts +5 -0
  9. package/src/config/config.ts +38 -4
  10. package/src/config/defaults.ts +5 -0
  11. package/src/config/suggestions.ts +8 -0
  12. package/src/extension/async-notifier.ts +10 -1
  13. package/src/extension/cross-extension-rpc.ts +1 -1
  14. package/src/extension/notification-router.ts +18 -0
  15. package/src/extension/register.ts +13 -17
  16. package/src/extension/registration/subagent-tools.ts +1 -1
  17. package/src/extension/team-tool/anchor.ts +201 -0
  18. package/src/extension/team-tool/api.ts +2 -1
  19. package/src/extension/team-tool/auto-summarize.ts +154 -0
  20. package/src/extension/team-tool/run.ts +37 -2
  21. package/src/extension/team-tool.ts +44 -2
  22. package/src/hooks/registry.ts +1 -3
  23. package/src/observability/event-bus.ts +13 -4
  24. package/src/observability/event-to-metric.ts +0 -2
  25. package/src/runtime/anchor-manager.ts +473 -0
  26. package/src/runtime/async-runner.ts +8 -4
  27. package/src/runtime/auto-summarize.ts +350 -0
  28. package/src/runtime/background-runner.ts +2 -1
  29. package/src/runtime/budget-tracker.ts +354 -0
  30. package/src/runtime/chain-runner.ts +507 -0
  31. package/src/runtime/child-pi.ts +24 -6
  32. package/src/runtime/crash-recovery.ts +5 -4
  33. package/src/runtime/crew-agent-records.ts +32 -1
  34. package/src/runtime/custom-tools/irc-tool.ts +13 -0
  35. package/src/runtime/custom-tools/submit-result-tool.ts +3 -2
  36. package/src/runtime/delivery-coordinator.ts +10 -3
  37. package/src/runtime/dynamic-script-runner.ts +482 -0
  38. package/src/runtime/handoff-manager.ts +589 -0
  39. package/src/runtime/hidden-handoff.ts +424 -0
  40. package/src/runtime/live-agent-manager.ts +20 -4
  41. package/src/runtime/live-session-runtime.ts +39 -4
  42. package/src/runtime/manifest-cache.ts +2 -1
  43. package/src/runtime/model-resolver.ts +16 -4
  44. package/src/runtime/phase-tracker.ts +373 -0
  45. package/src/runtime/pipeline-runner.ts +514 -0
  46. package/src/runtime/retry-runner.ts +354 -0
  47. package/src/runtime/sandbox.ts +252 -0
  48. package/src/runtime/scheduler.ts +7 -2
  49. package/src/runtime/subagent-manager.ts +1 -1
  50. package/src/runtime/task-graph.ts +11 -1
  51. package/src/runtime/task-runner.ts +15 -1
  52. package/src/runtime/team-runner.ts +4 -3
  53. package/src/schema/team-tool-schema.ts +31 -0
  54. package/src/skills/discover-skills.ts +5 -0
  55. package/src/state/active-run-registry.ts +19 -3
  56. package/src/state/contracts.ts +9 -0
  57. package/src/state/crew-init.ts +3 -3
  58. package/src/state/decision-ledger.ts +26 -32
  59. package/src/state/event-log-rotation.ts +2 -2
  60. package/src/state/event-log.ts +17 -4
  61. package/src/state/mailbox.ts +35 -1
  62. package/src/state/run-cache.ts +18 -8
  63. package/src/tools/safe-bash-extension.ts +1 -0
  64. package/src/tools/safe-bash.ts +153 -20
  65. package/src/ui/overlays/mailbox-detail-overlay.ts +13 -2
  66. package/src/ui/powerbar-publisher.ts +1 -0
  67. package/src/ui/transcript-cache.ts +13 -0
  68. package/src/utils/bm25-search.ts +16 -8
  69. package/src/utils/env-filter.ts +8 -5
  70. package/src/utils/redaction.ts +169 -15
  71. package/src/utils/sse-parser.ts +10 -1
  72. package/src/worktree/cleanup.ts +6 -1
  73. package/workflows/chain.workflow.md +252 -0
  74. package/workflows/pipeline.workflow.md +27 -0
@@ -1,50 +1,144 @@
1
1
  /**
2
2
  * Safe Bash Tool for pi-crew
3
3
  * Wraps bash with dangerous command blocking
4
+ * Uses linear-time scanning to prevent ReDoS attacks
4
5
  */
5
6
 
6
7
  import { Type } from "@sinclair/typebox";
7
8
 
8
- // Dangerous command patterns to block
9
+ // Backward-compatible pattern array (kept for getPatterns API)
10
+ // IMPORTANT: Line 8 (rm pattern with nested quantifiers) has been replaced
11
+ // with linear-time checking in isDangerous() to prevent ReDoS attacks.
9
12
  const DANGEROUS_PATTERNS = [
10
- // rm -rf / or rm -rf ~ (catastrophic root/home deletion)
11
- /\brm\s+(-[a-zA-Z]*[rf][a-zA-Z]*\s*)+(\/|~)(\s*$)/,
12
- /\brm\s+(-[a-zA-Z]*[rf][a-zA-Z]*\s*)+(\/|~)($|\s)/,
13
- // Privilege escalation
13
+ // NOTE: rm patterns handled by matchesDangerousRm() for linear-time safety
14
14
  /\bsudo\b/,
15
15
  /\bsu\s+root\b/,
16
- // Filesystem destruction
17
16
  /\bmkfs\b/,
18
17
  /\bdd\s+if=/,
19
- // Fork bomb
20
18
  /^:\s*\(\s*\)\s*\{.*\|.*&.*\}\s*;.*$/,
21
- // Device writing
22
19
  />\s*\/dev\/[sh]d[a-z]/,
23
20
  /\bchmod\s+(-[a-zA-Z]+\s+)?777\s+\//,
24
21
  /\bchown\s+(-[a-zA-Z]+\s+)?root/,
25
- // Pipe to shell (download and execute)
26
22
  /\bcurl\s.*\|\s*(ba)?sh/i,
27
23
  /\bwget\s.*\|\s*(ba)?sh/i,
28
- // System shutdown/reboot
29
24
  /\bshutdown\b/,
30
25
  /\breboot\b/,
31
26
  /\binit\s+0\b/,
32
- // Kill critical processes
33
27
  /\bkill\s+-9\s+1\b/,
34
28
  /\bkillall\b/,
35
- // Encoded commands
36
29
  /\|\s*base64\s+-d/,
37
30
  /\|\s*python.*-c/,
38
31
  /\|\s*perl.*-e/,
39
32
  /\|\s*ruby.*-e/,
40
- // Network to shell
41
- /\bbash\s+-i\s+>\s*\&/,
33
+ /\bbash\s+-i\s*>\s*\&/,
42
34
  /\bexec\s+.*bash/,
43
- // /etc/passwd manipulation
44
35
  /\becho\s+.*>\s*\/etc\/passwd/,
45
36
  /\bcat\s+.*>\s*\/etc\/passwd/,
46
37
  ];
47
38
 
39
+ /**
40
+ * Linear-time check if command contains a dangerous rm pattern like "rm -rf /" or "rm -rf ~"
41
+ * Replaces O(n²) regex backtracking with O(n) string scanning
42
+ */
43
+ function matchesDangerousRm(command: string): boolean {
44
+ let pos = 0;
45
+ const len = command.length;
46
+ // Find "rm" at word boundary
47
+ while (pos < len) {
48
+ const rmIdx = command.indexOf("rm", pos);
49
+ if (rmIdx === -1) return false;
50
+ // Check word boundary before "rm"
51
+ if (rmIdx > 0 && /\w/.test(command[rmIdx - 1])) {
52
+ pos = rmIdx + 1;
53
+ continue;
54
+ }
55
+ // Must be followed by whitespace
56
+ const afterRm = rmIdx + 2;
57
+ if (afterRm >= len || /\s/.test(command[afterRm])) {
58
+ // Found "rm " - now check for -rf flags followed by / or ~
59
+ let p = afterRm + 1;
60
+ while (p < len) {
61
+ // Skip whitespace
62
+ while (p < len && /\s/.test(command[p])) p++;
63
+ if (p >= len) break;
64
+ // Check for flag
65
+ if (command[p] !== "-") break;
66
+ p++;
67
+ let hasR = false, hasF = false;
68
+ while (p < len && /[a-zA-Z]/.test(command[p])) {
69
+ if (command[p] === "r" || command[p] === "R") hasR = true;
70
+ if (command[p] === "f" || command[p] === "F") hasF = true;
71
+ p++;
72
+ }
73
+ if (!hasR && !hasF) break; // Flag must have r or f
74
+ // Skip whitespace after flag
75
+ while (p < len && /\s/.test(command[p])) p++;
76
+ }
77
+ // Now check if followed by / or ~ (end or whitespace)
78
+ if (p < len && (command[p] === "/" || command[p] === "~")) {
79
+ const afterSlash = p + 1;
80
+ if (afterSlash >= len || /\s/.test(command[afterSlash]) || command[afterSlash] === ";") {
81
+ return true; // Dangerous!
82
+ }
83
+ }
84
+ }
85
+ pos = rmIdx + 1;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ /**
91
+ * Linear-time check for fork bomb pattern: :() { ... | ... & ... } ; ...
92
+ */
93
+ function matchesForkBomb(command: string): boolean {
94
+ // Must start with :
95
+ const trimmed = command.trimStart();
96
+ if (!trimmed.startsWith(":")) return false;
97
+ // Find () after :
98
+ const parenIdx = trimmed.indexOf("()");
99
+ if (parenIdx === -1 || parenIdx > 10) return false; // : must be close to ()
100
+ // Find { after ()
101
+ const braceIdx = trimmed.indexOf("{", parenIdx);
102
+ if (braceIdx === -1 || braceIdx > parenIdx + 5) return false;
103
+ // Find } closing brace
104
+ const closeBrace = trimmed.indexOf("}", braceIdx);
105
+ if (closeBrace === -1) return false;
106
+ // Check content between braces for | and &
107
+ const content = trimmed.slice(braceIdx + 1, closeBrace);
108
+ if (content.includes("|") && content.includes("&")) return true;
109
+ return false;
110
+ }
111
+
112
+ /**
113
+ * Check for encoded command patterns (pipe to shell)
114
+ */
115
+ function matchesEncodedPipe(command: string): boolean {
116
+ const lower = command.toLowerCase();
117
+ const pipeIdx = lower.indexOf("|");
118
+ if (pipeIdx === -1) return false;
119
+ const afterPipe = lower.slice(pipeIdx + 1).trimStart();
120
+ if (afterPipe.startsWith("base64") || afterPipe.startsWith("python") || afterPipe.startsWith("perl") || afterPipe.startsWith("ruby")) {
121
+ // Check if followed by -d or -c or -e
122
+ const rest = afterPipe.slice(6).trimStart();
123
+ if (rest.startsWith("-d") || rest.startsWith("-c") || rest.startsWith("-e")) return true;
124
+ }
125
+ return false;
126
+ }
127
+
128
+ /**
129
+ * Check if command contains a specific dangerous substring
130
+ */
131
+ function containsDangerous(command: string, pattern: string): boolean {
132
+ return command.indexOf(pattern) !== -1;
133
+ }
134
+
135
+ /**
136
+ * Check if command starts with dangerous prefix
137
+ */
138
+ function startsWithDangerous(command: string, pattern: string): boolean {
139
+ return command.trimStart().startsWith(pattern);
140
+ }
141
+
48
142
  export interface SafeBashOptions {
49
143
  /** Enable/disable safe mode. Default: true */
50
144
  enabled?: boolean;
@@ -75,9 +169,47 @@ export function isDangerous(command: string, options: SafeBashOptions = {}): str
75
169
  }
76
170
  }
77
171
 
78
- // Check dangerous patterns
79
- const allPatterns = [...DANGEROUS_PATTERNS, ...additionalPatterns];
80
- for (const pattern of allPatterns) {
172
+ // Use linear-time scanning functions for critical patterns
173
+ if (matchesDangerousRm(normalized)) {
174
+ return "Command blocked by safe_bash: dangerous rm pattern detected";
175
+ }
176
+ if (matchesForkBomb(normalized)) {
177
+ return "Command blocked by safe_bash: fork bomb pattern detected";
178
+ }
179
+ if (matchesEncodedPipe(normalized)) {
180
+ return "Command blocked by safe_bash: encoded pipe to shell detected";
181
+ }
182
+
183
+ // Check remaining patterns using regex (these are safe from ReDoS)
184
+ for (const pattern of DANGEROUS_PATTERNS) {
185
+ if (pattern.test(normalized)) {
186
+ return `Command blocked by safe_bash: matches dangerous pattern \`${pattern}\``;
187
+ }
188
+ }
189
+
190
+ // Additional shell injection checks using regex for non-critical patterns
191
+ // Block command substitution $(...)
192
+ if (/\$\([^)]*\)/.test(command)) {
193
+ return "Command blocked by safe_bash: command substitution $(...) is not allowed";
194
+ }
195
+ // Block backtick substitution
196
+ const backtickRe = /`[^`]*`/;
197
+ if (backtickRe.test(command)) {
198
+ return "Command blocked by safe_bash: backtick substitution is not allowed";
199
+ }
200
+ // Block here-docs <<
201
+ if (/<<\s*['"]?[\w-]+['"]?/.test(command) || /\$<<\s*['"]?[\w-]+['"]?/.test(command)) {
202
+ return "Command blocked by safe_bash: here-doc is not allowed";
203
+ }
204
+ // Block ${...} variable expansion containing shell metacharacters (pipes, redirects, &&/||)
205
+ const varExpRe = /\$\{([^}]*)\}/;
206
+ const varMatch = command.match(varExpRe);
207
+ if (varMatch && /[|&;<>]/.test(varMatch[1])) {
208
+ return "Command blocked by safe_bash: variable expansion with shell metacharacters is not allowed";
209
+ }
210
+
211
+ // Check additional patterns (user-provided regex)
212
+ for (const pattern of additionalPatterns) {
81
213
  if (pattern.test(normalized)) {
82
214
  return `Command blocked by safe_bash: matches dangerous pattern \`${pattern}\``;
83
215
  }
@@ -142,8 +274,9 @@ export function createSafeBash(options: SafeBashOptions = {}) {
142
274
  * These can be used in allowPatterns for specific use cases
143
275
  */
144
276
  export const COMMON_SAFE_PATTERNS = {
145
- // Safe rm with specific paths
146
- safeRm: /\brm\s+(-[a-zA-Z]*f[a-zA-Z]*\s+)?((?![\/~])\/)?(tmp|cache|node_modules|dist|build)\//,
277
+ // FIX: Stricter regex target must be exactly tmp/, cache/, node_modules/, dist/, or build/
278
+ // (with optional ./ prefix). Rejects path traversal (./../../../other) and absolute paths.
279
+ safeRm: /rm\s+(-[a-zA-Z]*f[a-zA-Z]*\s+)?(?:\.\/)?(?:tmp|cache|node_modules|dist|build)\/[a-zA-Z0-9._/-]+$/,
147
280
  // Safe git operations
148
281
  safeGit: /\bgit\s+(clone|pull|push|commit|add|status|diff|log|branch|checkout|merge|rebase)/,
149
282
  // Safe npm/yarn/pnpm
@@ -20,6 +20,8 @@ export class MailboxDetailOverlay {
20
20
  private side: "inbox" | "outbox" = "inbox";
21
21
  private selected = 0;
22
22
  private expanded = false;
23
+ private lastRefreshedTaskCount = 0;
24
+ private needsRefresh = true;
23
25
 
24
26
  constructor(opts: { runId: string; cwd: string; done: (action: MailboxAction | undefined) => void; theme?: unknown }) {
25
27
  this.runId = opts.runId;
@@ -32,6 +34,12 @@ export class MailboxDetailOverlay {
32
34
  private refresh(): void {
33
35
  const loaded = loadRunManifestById(this.cwd, this.runId);
34
36
  if (!loaded) return;
37
+ // Track task count changes to trigger re-render
38
+ const taskCount = loaded.tasks.length;
39
+ if (taskCount !== this.lastRefreshedTaskCount) {
40
+ this.lastRefreshedTaskCount = taskCount;
41
+ this.needsRefresh = true;
42
+ }
35
43
  const delivery = readDeliveryState(loaded.manifest).messages;
36
44
  const applyDelivery = (message: MailboxMessage): MailboxMessage => ({ ...message, status: delivery[message.id] ?? message.status });
37
45
  const taskIds = loaded.tasks.map((task) => task.id);
@@ -49,11 +57,14 @@ export class MailboxDetailOverlay {
49
57
  }
50
58
 
51
59
  invalidate(): void {
52
- this.refresh();
60
+ this.needsRefresh = true;
53
61
  }
54
62
 
55
63
  render(width: number): string[] {
56
- this.refresh();
64
+ if (this.needsRefresh) {
65
+ this.refresh();
66
+ this.needsRefresh = false;
67
+ }
57
68
  const inner = Math.max(40, width - 4);
58
69
  const col = Math.max(18, Math.floor((inner - 3) / 2));
59
70
  const lines = [
@@ -294,6 +294,7 @@ export function requestPowerbarUpdate(
294
294
 
295
295
  /** Dispose the powerbar coalescer. Call during extension cleanup. */
296
296
  export function disposePowerbarCoalescer(): void {
297
+ powerbarCoalescer.flush();
297
298
  powerbarCoalescer.dispose();
298
299
  }
299
300
 
@@ -19,6 +19,7 @@ export interface TranscriptReadOptions {
19
19
 
20
20
  const TRANSCRIPT_CACHE_TTL_MS = 500;
21
21
  const DEFAULT_TAIL_BYTES = 256 * 1024;
22
+ const MAX_CACHE_SIZE = 100;
22
23
  const transcriptCache = new Map<string, TranscriptCacheEntry>();
23
24
 
24
25
  function cacheKey(path: string, options: Required<Pick<TranscriptReadOptions, "full">> & { maxTailBytes: number }): string {
@@ -85,6 +86,18 @@ export function readTranscriptLinesCached(path: string, parse: (text: string) =>
85
86
  truncated: read.truncated,
86
87
  };
87
88
  transcriptCache.set(key, entry);
89
+ // Evict oldest entry if cache exceeds max size
90
+ if (transcriptCache.size > MAX_CACHE_SIZE) {
91
+ let oldestKey: string | null = null;
92
+ let oldestParsedAt = Infinity;
93
+ for (const [k, v] of transcriptCache.entries()) {
94
+ if (v.parsedAt < oldestParsedAt) {
95
+ oldestParsedAt = v.parsedAt;
96
+ oldestKey = k;
97
+ }
98
+ }
99
+ if (oldestKey) transcriptCache.delete(oldestKey);
100
+ }
88
101
  return lines;
89
102
  } catch {
90
103
  return previous?.lines ?? [];
@@ -46,17 +46,17 @@ export class BM25Search<T extends SearchDocument> {
46
46
  }
47
47
 
48
48
  /**
49
- * Compute document frequency for a query term using substring matching,
50
- * consistent with the regex-based tf computation in search().
49
+ * Compute document frequency for a query term using indexOf for better performance.
50
+ * Uses linear-time substring matching instead of regex to avoid ReDoS.
51
51
  */
52
52
  private df(term: string): number {
53
- const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
54
- const re = new RegExp(escaped, "g");
53
+ const termLower = term.toLowerCase();
55
54
  let count = 0;
56
55
  for (const doc of this.documents) {
57
56
  for (const field of Object.keys(this.fieldWeights)) {
58
57
  const text = (doc.fields[field] ?? "").toLowerCase();
59
- if (re.test(text)) {
58
+ // Use indexOf for linear-time substring search
59
+ if (text.includes(termLower)) {
60
60
  count++;
61
61
  break;
62
62
  }
@@ -81,11 +81,19 @@ export class BM25Search<T extends SearchDocument> {
81
81
  let fieldScore = 0;
82
82
 
83
83
  for (const term of queryTerms) {
84
- const escaped = term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
85
- const tf = (textLower.match(new RegExp(escaped, "g")) ?? []).length;
84
+ // Use indexOf for linear-time substring counting instead of regex
85
+ const termLower = term.toLowerCase();
86
+ let tf = 0;
87
+ let pos = 0;
88
+ while ((pos = textLower.indexOf(termLower, pos)) !== -1) {
89
+ tf++;
90
+ pos += termLower.length;
91
+ // Cap tf to prevent runaway on repeated patterns
92
+ if (tf > 100) break;
93
+ }
86
94
  if (tf === 0) continue;
87
95
 
88
- const df = this.df(term);
96
+ const df = this.df(termLower);
89
97
  if (df === 0) continue;
90
98
 
91
99
  const idf = Math.log((this.N - df + 0.5) / (df + 0.5) + 1);
@@ -1,4 +1,4 @@
1
- import { SECRET_KEY_PATTERN } from "./redaction.ts";
1
+ import { isSecretKey } from "./redaction.ts";
2
2
 
3
3
  export interface SanitizeEnvOptions {
4
4
  /** Allow-list of env var names to preserve. Supports trailing glob, e.g. `"PI_*"`. */
@@ -8,14 +8,17 @@ export interface SanitizeEnvOptions {
8
8
  /**
9
9
  * Strip env vars whose keys look like secrets before passing to child processes.
10
10
  *
11
- * Default mode (no allowList): deny-list using SECRET_KEY_PATTERN.
11
+ * Default mode (no allowList): deny-list using isSecretKey.
12
12
  * When allowList is provided, only keys matching the allow-list are preserved.
13
13
  */
14
14
  export function sanitizeEnvSecrets(env: NodeJS.ProcessEnv, options?: SanitizeEnvOptions): Record<string, string> {
15
15
  const filtered: Record<string, string> = {};
16
16
  if (options?.allowList && options.allowList.length > 0) {
17
17
  const matchers = options.allowList.map((p) => {
18
- if (p.endsWith("*")) return (k: string) => k.startsWith(p.slice(0, -1));
18
+ if (p.endsWith("*")) {
19
+ const prefix = p.slice(0, -1);
20
+ return (k: string) => k.startsWith(prefix) && k.length > prefix.length;
21
+ }
19
22
  return (k: string) => k === p;
20
23
  });
21
24
  for (const [key, value] of Object.entries(env)) {
@@ -24,7 +27,7 @@ export function sanitizeEnvSecrets(env: NodeJS.ProcessEnv, options?: SanitizeEnv
24
27
  return filtered;
25
28
  }
26
29
  for (const [key, value] of Object.entries(env)) {
27
- if (value !== undefined && !SECRET_KEY_PATTERN.test(key)) filtered[key] = value;
30
+ if (value !== undefined && !isSecretKey(key)) filtered[key] = value;
28
31
  }
29
32
  return filtered;
30
- }
33
+ }
@@ -1,26 +1,180 @@
1
- export const SECRET_KEY_PATTERN = /(?:^|[_.-])(token|api[-_]?key|password|passwd|secret|credential|authorization|private[-_]?key)(?:$|[_.-])/i;
2
- const INLINE_SECRET_PATTERN = /(^|[\s,{])(([A-Za-z0-9_.-]*(?:api[-_]?key|token|password|passwd|secret|credential|authorization|private[-_]?key)[A-Za-z0-9_.-]*)\s*[=:]\s*)([^\s,;"'}]+)/gi;
3
- const AUTH_HEADER_PATTERN = /\b(Authorization\s*:\s*(?:Bearer|Basic|Token)?\s*)([^\r\n]+)/gi;
4
- const BEARER_PATTERN = /\b(Bearer\s+)([A-Za-z0-9._~+/=-]{8,})\b/g;
5
- const PEM_PRIVATE_KEY_PATTERN = /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]{0,65536}?-----END [A-Z ]*PRIVATE KEY-----/g;
1
+ /**
2
+ * ReDoS-resistant pattern matching for secret detection.
3
+ * Uses linear-time scan instead of complex regex to prevent catastrophic backtracking.
4
+ */
5
+
6
+ // Pattern for PEM private keys (possessive quantifier prevents backtracking)
7
+ export const PEM_PRIVATE_KEY_PATTERN = /-----BEGIN [A-Z ]+PRIVATE KEY-----[\s\S]+?-----END [A-Z ]+PRIVATE KEY-----/g;
8
+
9
+ // Linear-time secret key detection
10
+ export function isSecretKey(keyName: string): boolean {
11
+ // Fast path: common secret key names
12
+ const lower = keyName.toLowerCase();
13
+ if (/^(token|apikey|api_key|password|secret|credential|authorization|privatekey|private_key)$/.test(lower)) {
14
+ return true;
15
+ }
16
+ // Linear scan for prefix characters followed by keywords
17
+ const prefixes = "_.-";
18
+ const keywords = ["token", "api", "key", "password", "passwd", "secret", "credential", "authorization", "private"];
19
+
20
+ for (let i = 0; i < keyName.length; i++) {
21
+ if (prefixes.includes(keyName[i])) {
22
+ const remaining = keyName.substring(i + 1).toLowerCase();
23
+ for (const kw of keywords) {
24
+ if (remaining.startsWith(kw)) {
25
+ const afterKw = remaining.substring(kw.length);
26
+ if (afterKw === "" || prefixes.includes(afterKw[0]) || /[a-zA-Z0-9]/.test(afterKw[0])) {
27
+ return true;
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ return false;
34
+ }
35
+
36
+ // Linear-time Authorization header redaction
37
+ export function redactAuthHeader(line: string): string {
38
+ const lower = line.toLowerCase();
39
+ const authIdx = lower.indexOf("authorization:");
40
+ if (authIdx === -1) return line;
41
+
42
+ // Verify word boundary - must be at start of line or preceded by whitespace/comma/brace
43
+ if (authIdx > 0) {
44
+ const before = line[authIdx - 1];
45
+ if (before !== ' ' && before !== ',' && before !== '{' && before !== '[' && before !== '"' && before !== '\r' && before !== '\n') {
46
+ return line; // Not a word boundary
47
+ }
48
+ }
49
+
50
+ // Check if this is followed by Bearer token (don't redact Bearer tokens separately)
51
+ // Look for "Bearer" after "authorization:"
52
+ const afterAuth = lower.substring(authIdx + 14).trimStart();
53
+ if (!afterAuth.startsWith('bearer ')) {
54
+ // No Bearer token, this is a regular Authorization header - redact it
55
+ let end = authIdx + 14;
56
+ while (end < line.length && line[end] !== "\r" && line[end] !== "\n") {
57
+ end++;
58
+ }
59
+ return line.substring(0, end) + " ***" + (end < line.length ? line.substring(end) : "");
60
+ }
61
+
62
+ // It's a Bearer token format - don't redact here, let redactBearerTokens handle it
63
+ return line;
64
+ }
65
+
66
+ // Linear-time Bearer token redaction
67
+ export function redactBearerTokens(line: string): string {
68
+ const upper = line.toUpperCase();
69
+ const result: string[] = [];
70
+ let i = 0;
71
+
72
+ while (i < line.length) {
73
+ if (upper.startsWith("BEARER ", i)) {
74
+ // Check word boundary: preceded by start, space, comma, brace, or newline
75
+ if (i > 0) {
76
+ const before = line[i - 1];
77
+ if (before !== ' ' && before !== ',' && before !== '{' && before !== '[' && before !== '"' && before !== '\r' && before !== '\n') {
78
+ result.push(line[i]);
79
+ i++;
80
+ continue;
81
+ }
82
+ }
83
+
84
+ // Found "Bearer " - now find the token
85
+ const bearerPrefix = line.substring(i, i + 7); // "Bearer "
86
+ let j = i + 7;
87
+ let tokenLen = 0;
88
+ while (j < line.length && tokenLen < 200 && /[A-Za-z0-9._~+/-]/.test(line[j])) {
89
+ j++;
90
+ tokenLen++;
91
+ }
92
+
93
+ if (tokenLen >= 8) {
94
+ // Replace with Bearer + *** (redact the token)
95
+ result.push(bearerPrefix + "***");
96
+ i = j;
97
+ continue;
98
+ }
99
+ }
100
+ result.push(line[i]);
101
+ i++;
102
+ }
103
+
104
+ return result.join("");
105
+ }
6
106
 
7
107
  function isRecord(value: unknown): value is Record<string, unknown> {
8
108
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
9
- // Exclude built-in types whose Object.entries() would produce empty arrays.
10
109
  if (value instanceof Date || value instanceof RegExp || value instanceof Error || value instanceof Map || value instanceof Set) return false;
11
110
  return true;
12
111
  }
13
112
 
14
- function isSecretKey(keyName: string): boolean {
15
- return SECRET_KEY_PATTERN.test(keyName) || /^(token|apiKey|api_key|password|secret|credential|authorization|privateKey|private_key)$/i.test(keyName);
113
+ export function redactSecretString(value: string): string {
114
+ let result = value;
115
+
116
+ // Replace PEM private keys
117
+ result = result.replace(PEM_PRIVATE_KEY_PATTERN, "***");
118
+
119
+ // Replace Authorization headers (non-Bearer format)
120
+ result = redactAuthHeader(result);
121
+
122
+ // Replace Bearer tokens
123
+ result = redactBearerTokens(result);
124
+
125
+ // Replace inline secrets: key=value or key:value patterns
126
+ result = redactInlineSecrets(result);
127
+
128
+ return result;
16
129
  }
17
130
 
18
- export function redactSecretString(value: string): string {
19
- return value
20
- .replace(PEM_PRIVATE_KEY_PATTERN, "***")
21
- .replace(AUTH_HEADER_PATTERN, "$1***")
22
- .replace(BEARER_PATTERN, "$1***")
23
- .replace(INLINE_SECRET_PATTERN, "$1$2***");
131
+ // Linear-time inline secret redaction: token=xxx, api_key=xxx, etc.
132
+ function redactInlineSecrets(value: string): string {
133
+ const result: string[] = [];
134
+ let i = 0;
135
+
136
+ while (i < value.length) {
137
+ // Look for pattern: word_chars + = or : + non-whitespace_value
138
+ // Check for secret key followed by = or :
139
+ let j = i;
140
+ let keyLen = 0;
141
+
142
+ // Collect key characters (alphanumeric, underscore, hyphen)
143
+ while (j < value.length && /[a-zA-Z0-9_-]/.test(value[j])) {
144
+ j++;
145
+ keyLen++;
146
+ }
147
+
148
+ if (keyLen > 0 && j < value.length && (value[j] === '=' || value[j] === ':')) {
149
+ const key = value.substring(i, i + keyLen);
150
+
151
+ // Check if this is a secret key
152
+ if (isSecretKey(key)) {
153
+ // Find the value (everything after = or : until space, comma, or end)
154
+ const sep = value[j];
155
+ let k = j + 1;
156
+ let valLen = 0;
157
+ while (k < value.length && valLen < 500 && value[k] !== ' ' && value[k] !== ',' && value[k] !== ';' && value[k] !== '"' && value[k] !== '"' && value[k] !== '\r' && value[k] !== '\n') {
158
+ k++;
159
+ valLen++;
160
+ }
161
+
162
+ // Only redact if there's actual content
163
+ if (valLen > 0) {
164
+ result.push(key);
165
+ result.push(sep);
166
+ result.push("***");
167
+ i = k;
168
+ continue;
169
+ }
170
+ }
171
+ }
172
+
173
+ result.push(value[i]);
174
+ i++;
175
+ }
176
+
177
+ return result.join("");
24
178
  }
25
179
 
26
180
  export function redactSecrets(value: unknown, keyName = ""): unknown {
@@ -41,4 +195,4 @@ export function redactJsonLine(line: string): string {
41
195
  } catch {
42
196
  return redactSecretString(line);
43
197
  }
44
- }
198
+ }
@@ -8,6 +8,9 @@ export interface ServerSentEvent {
8
8
  /** L1: Maximum number of raw lines before discarding an oversized event. */
9
9
  const MAX_EVENT_LINES = 1000;
10
10
 
11
+ /** L2: Maximum data size per line to prevent unbounded memory usage. */
12
+ const MAX_DATA_SIZE = 100000; // 100KB per line
13
+
11
14
  /** Read newline-delimited lines from a text ReadableStream, buffering partial chunks. */
12
15
  async function* readLines(
13
16
  stream: ReadableStream<string>,
@@ -97,13 +100,19 @@ export async function* readSseEvents(
97
100
 
98
101
  currentRaw.push(line);
99
102
 
100
- // L1: Guard against unbounded memory growth
103
+ // L1: Guard against unbounded memory growth (line count)
101
104
  if (currentRaw.length > MAX_EVENT_LINES) {
102
105
  const evt = flush();
103
106
  if (evt) yield evt;
104
107
  continue;
105
108
  }
106
109
 
110
+ // L2: Guard against unbounded memory growth (data size per line)
111
+ if (value.length > MAX_DATA_SIZE) {
112
+ // Truncate oversized data to prevent memory issues
113
+ value = value.slice(0, MAX_DATA_SIZE);
114
+ }
115
+
107
116
  if (field === "event") {
108
117
  currentEvent = value;
109
118
  } else if (field === "data") {
@@ -59,7 +59,12 @@ export function cleanupRunWorktrees(manifest: TeamRunManifest, options: { force?
59
59
  // Commit changes to a branch instead of just preserving the worktree
60
60
  try {
61
61
  execFileSync("git", ["add", "-A"], { cwd: worktreePath, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, LANG: "C", LC_ALL: "C" }, windowsHide: true });
62
- const safeDesc = entry.name.slice(0, 200);
62
+ let safeDesc = entry.name.slice(0, 200);
63
+ // SECURITY: Strip any newlines that could be injected via a malicious worktree name
64
+ // to prevent newline injection in git commit messages
65
+ if (safeDesc.includes("\n")) {
66
+ safeDesc = safeDesc.replace(/[\r\n]+/g, " ");
67
+ }
63
68
  execFileSync("git", ["commit", "-m", `pi-crew: ${safeDesc}`], { cwd: worktreePath, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], env: { ...process.env, LANG: "C", LC_ALL: "C" }, windowsHide: true });
64
69
  // Create branch in the main repo pointing to this worktree's HEAD
65
70
  try {