pi-crew 0.9.19 → 0.9.20

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.
@@ -7,7 +7,7 @@ import { DEFAULT_CHILD_PI } from "../config/defaults.ts";
7
7
  import { registerChildProcess, unregisterChildProcess } from "../extension/crew-cleanup.ts";
8
8
  import type { WorkerExitStatus } from "../state/types.ts";
9
9
  import { WINDOWS_ESSENTIAL_ENV_VARS } from "../utils/env-allowlist.ts";
10
- import { sanitizeEnvSecrets } from "../utils/env-filter.ts";
10
+ import { buildScopedAllowList, sanitizeEnvSecrets } from "../utils/env-filter.ts";
11
11
  import { logInternalError } from "../utils/internal-error.ts";
12
12
  import { redactJsonLine, redactSecretString } from "../utils/redaction.ts";
13
13
  import { resolveRealContainedPath } from "../utils/safe-paths.ts";
@@ -254,7 +254,53 @@ export interface ChildPiRunResult {
254
254
  intermediateFindings?: string;
255
255
  }
256
256
 
257
- export function buildChildPiSpawnOptions(cwd: string, env: NodeJS.ProcessEnv): SpawnOptions {
257
+ // Base allowlist of non-provider env vars always passed to child workers.
258
+ // Provider API keys are injected dynamically via buildScopedAllowList() only
259
+ // when a model is assigned to the task (per-task key scoping).
260
+ const BASE_ALLOWLIST: string[] = [
261
+ "PATH",
262
+ "HOME",
263
+ "USER",
264
+ "SHELL",
265
+ "TERM",
266
+ "LANG",
267
+ "LC_ALL",
268
+ "LC_COLLATE",
269
+ "LC_CTYPE",
270
+ "LC_MESSAGES",
271
+ "LC_MONETARY",
272
+ "LC_NUMERIC",
273
+ "LC_TIME",
274
+ "XDG_CONFIG_HOME",
275
+ "XDG_DATA_HOME",
276
+ "XDG_CACHE_HOME",
277
+ "XDG_RUNTIME_DIR",
278
+ // Windows essentials — see WINDOWS_ESSENTIAL_ENV_VARS (src/utils/env-allowlist.ts).
279
+ ...WINDOWS_ESSENTIAL_ENV_VARS,
280
+ "NVM_BIN",
281
+ "NVM_DIR",
282
+ "NVM_INC",
283
+ "NODE_DISABLE_COLORS",
284
+ "NODE_EXTRA_CA_CERTS",
285
+ "NPM_CONFIG_REGISTRY",
286
+ "NPM_CONFIG_USERCONFIG",
287
+ "NPM_CONFIG_GLOBALCONFIG",
288
+ "PI_CREW_DEPTH",
289
+ "PI_CREW_MAX_DEPTH",
290
+ "PI_CREW_INHERIT_PROJECT_CONTEXT",
291
+ "PI_CREW_INHERIT_SKILLS",
292
+ "PI_CREW_KIND",
293
+ "PI_CREW_PARENT_PID",
294
+ "PI_TEAMS_DEPTH",
295
+ "PI_TEAMS_MAX_DEPTH",
296
+ "PI_TEAMS_INHERIT_PROJECT_CONTEXT",
297
+ "PI_TEAMS_INHERIT_SKILLS",
298
+ "PI_TEAMS_PI_BIN",
299
+ "PI_TEAMS_MOCK_CHILD_PI",
300
+ "PI_CREW_ALLOW_MOCK",
301
+ ];
302
+
303
+ export function buildChildPiSpawnOptions(cwd: string, env: NodeJS.ProcessEnv, model?: string): SpawnOptions {
258
304
  // SECURITY FIX (Issue #1): Validate cwd before passing to spawn.
259
305
  // If cwd comes from an untrusted source (user input, workspace config), a malicious cwd
260
306
  // could cause the child process to operate in an attacker-controlled directory,
@@ -284,81 +330,12 @@ export function buildChildPiSpawnOptions(cwd: string, env: NodeJS.ProcessEnv): S
284
330
  // IMPORTANT: preserve model provider API keys — they are needed by the child Pi to call the LLM.
285
331
  // Also preserve essential non-secret vars (PATH, HOME, USER, etc.) so the child process can function.
286
332
  // Bug #12 fix: essential env vars (PATH, HOME, etc.) are always preserved so child can find npm/node.
287
- const filteredEnv = sanitizeEnvSecrets(env, {
288
- allowList: [
289
- /*
290
- * SECURITY WARNING: All model provider API keys below are passed to EVERY child worker.
291
- * If any child is compromised (e.g. via prompt injection), all listed keys are exposed.
292
- * This is a deliberate trade-off: multi-provider setups require the child Pi process to
293
- * authenticate with whichever provider the model routes to. Reducing keys per-child
294
- * would break multi-provider functionality. Mitigations:
295
- * - sanitizeEnvSecrets strips all env vars NOT on this list.
296
- * - Do NOT add wildcards ("*_API_KEY") — only explicit, intended provider keys.
297
- * - Consider per-task key scoping if the architecture allows it in the future.
298
- *
299
- * MAINTENANCE REQUIREMENT: When new secret env vars are added to the Pi ecosystem,
300
- * they MUST be explicitly added to this allowlist to be passed to child processes.
301
- * A CI check should fail if a secret-like env var (matching patterns like *_API_KEY,
302
- * *_TOKEN, *_SECRET) is detected in the codebase but not present in this list.
303
- */
304
- // NOTE: Model provider API keys are NOT needed here — child Pi uses the same
305
- // config file as parent Pi. Passing keys via env is a security risk.
306
- "PATH",
307
- "HOME",
308
- "USER",
309
- "SHELL",
310
- "TERM",
311
- "LANG",
312
- // FIX: Replaced broad wildcards (LC_*, XDG_*, NVM_*, NODE_*, npm_*) with
313
- // specific names. Previously NPM_TOKEN, NODE_ENV=production, NVM_RC_VERSION
314
- // all leaked through wildcards.
315
- "LC_ALL",
316
- "LC_COLLATE",
317
- "LC_CTYPE",
318
- "LC_MESSAGES",
319
- "LC_MONETARY",
320
- "LC_NUMERIC",
321
- "LC_TIME",
322
- "XDG_CONFIG_HOME",
323
- "XDG_DATA_HOME",
324
- "XDG_CACHE_HOME",
325
- "XDG_RUNTIME_DIR",
326
- // Windows essentials — see WINDOWS_ESSENTIAL_ENV_VARS (src/utils/env-allowlist.ts).
327
- ...WINDOWS_ESSENTIAL_ENV_VARS,
328
- "NVM_BIN",
329
- "NVM_DIR",
330
- "NVM_INC",
331
- // NODE_PATH is intentionally omitted from the allowlist.
332
- // NODE_PATH can reveal user environment information (e.g., NVM paths under $HOME)
333
- // and the validation at lines 286-298 only filters to standard system prefixes.
334
- // Removing it entirely is cleaner than best-effort filtering.
335
- "NODE_DISABLE_COLORS",
336
- "NODE_EXTRA_CA_CERTS",
337
- "NPM_CONFIG_REGISTRY",
338
- "NPM_CONFIG_USERCONFIG",
339
- "NPM_CONFIG_GLOBALCONFIG",
340
- // FIX: Replace PI_CREW_*/PI_TEAMS_* wildcards with explicit list of
341
- // safe vars. Wildcards are fragile — any new secret var would leak.
342
- // Only non-secret execution-control vars that children legitimately need.
343
- "PI_CREW_DEPTH",
344
- "PI_CREW_MAX_DEPTH",
345
- "PI_CREW_INHERIT_PROJECT_CONTEXT",
346
- "PI_CREW_INHERIT_SKILLS",
347
- // PI_CREW_KIND marks this process as a crew sub-agent (vs the user's main session).
348
- // doctor --zombies matches it to safely list orphaned sub-agents only.
349
- "PI_CREW_KIND",
350
- // PI_CREW_PARENT_PID is needed by child-pi's parent-guard (uses
351
- // process.kill(pid, 0) liveness check). The PID is not a secret.
352
- "PI_CREW_PARENT_PID",
353
- "PI_TEAMS_DEPTH",
354
- "PI_TEAMS_MAX_DEPTH",
355
- "PI_TEAMS_INHERIT_PROJECT_CONTEXT",
356
- "PI_TEAMS_INHERIT_SKILLS",
357
- "PI_TEAMS_PI_BIN",
358
- "PI_TEAMS_MOCK_CHILD_PI",
359
- "PI_CREW_ALLOW_MOCK",
360
- ],
361
- });
333
+ //
334
+ // PER-TASK KEY SCOPING: when a model is provided, only the env keys for that
335
+ // provider are injected (via buildScopedAllowList). When no model is given,
336
+ // only BASE_ALLOWLIST system vars pass through no provider keys leak.
337
+ const allowList = model ? buildScopedAllowList(BASE_ALLOWLIST, [model]) : BASE_ALLOWLIST;
338
+ const filteredEnv = sanitizeEnvSecrets(env, { allowList });
362
339
  // FIX: Removed delete workarounds — with explicit allowlist, these vars
363
340
  // are no longer auto-leaked. The wildcard approach was fragile.
364
341
 
@@ -900,10 +877,14 @@ export async function runChildPi(input: ChildPiRunInput): Promise<ChildPiRunResu
900
877
  const child = spawn(
901
878
  spawnSpec.command,
902
879
  spawnSpec.args,
903
- buildChildPiSpawnOptions(input.cwd, {
904
- ...process.env,
905
- ...built.env,
906
- }),
880
+ buildChildPiSpawnOptions(
881
+ input.cwd,
882
+ {
883
+ ...process.env,
884
+ ...built.env,
885
+ },
886
+ input.model,
887
+ ),
907
888
  );
908
889
  if (child.pid) {
909
890
  activeChildProcesses.set(child.pid, child);
@@ -1,4 +1,5 @@
1
1
  import * as crypto from "node:crypto";
2
+ import { isHmacEnabled, extractSignaturePayload, verifyRpcSignature } from "../extension/rpc-hmac.ts";
2
3
 
3
4
  export interface EventBus {
4
5
  on(event: string, handler: (data: unknown) => void): () => void;
@@ -47,6 +48,25 @@ function handleRpc<P extends { requestId: string }>(
47
48
  if (!/^[a-zA-Z0-9_-]+$/.test(params.requestId)) {
48
49
  throw new Error("Security: invalid requestId format");
49
50
  }
51
+ // SECURITY: HMAC signature verification (when enabled).
52
+ if (isHmacEnabled()) {
53
+ const sigPayload = extractSignaturePayload(params);
54
+ if (!sigPayload) {
55
+ throw new Error("[pi-crew HMAC] Missing HMAC signature in RPC request.");
56
+ }
57
+ // Strip HMAC fields before verification (HMAC was signed over original params)
58
+ const originalParams = { ...params };
59
+ delete (originalParams as Record<string, unknown>).hmacVersion;
60
+ delete (originalParams as Record<string, unknown>).hmacOrigin;
61
+ delete (originalParams as Record<string, unknown>).hmacTimestamp;
62
+ delete (originalParams as Record<string, unknown>).hmacChannel;
63
+ delete (originalParams as Record<string, unknown>).hmacNonce;
64
+ delete (originalParams as Record<string, unknown>).hmacSignature;
65
+ const verification = verifyRpcSignature(sigPayload, originalParams);
66
+ if (!verification.valid) {
67
+ throw new Error(`[pi-crew HMAC] ${verification.reason}`);
68
+ }
69
+ }
50
70
  try {
51
71
  const data = await fn(params);
52
72
  const reply: { success: true; data?: unknown } = { success: true };
@@ -72,9 +92,8 @@ export function registerCrewRpcHandlers(deps: RpcDeps): RpcHandle {
72
92
  // operations that create or terminate child processes. Any subscriber on
73
93
  // the shared event bus can emit these events. In a multi-extension
74
94
  // environment, this means a malicious extension could spawn/stop agents.
75
- // Mitigation (H-2): require an unguessable per-process token in addition to
76
- // the legacy `source` identifier. Log all invocations for audit. A full fix
77
- // still requires event-bus-level origin signing.
95
+ // Mitigations: HMAC origin signing (see rpc-hmac.ts) + per-process token
96
+ // (H-2) + legacy `source` identifier.
78
97
  const CREW_RPC_SOURCE = "pi-crew";
79
98
  const EXPECTED_TOKEN = getCrewRpcToken();
80
99
 
@@ -14,7 +14,7 @@
14
14
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
15
15
  import { createBashTool } from "@earendil-works/pi-coding-agent";
16
16
  import { Type } from "@sinclair/typebox";
17
- import { isDangerous } from "./safe-bash.ts";
17
+ import { checkCommand } from "./safe-bash.ts";
18
18
 
19
19
  export default function safeBashExtension(pi: ExtensionAPI): void {
20
20
  const cwd = process.cwd();
@@ -35,7 +35,7 @@ export default function safeBashExtension(pi: ExtensionAPI): void {
35
35
  ),
36
36
  }),
37
37
  async execute(toolCallId, params, signal, onUpdate, ctx) {
38
- const danger = isDangerous(params.command);
38
+ const danger = checkCommand(params.command);
39
39
  if (danger) {
40
40
  return {
41
41
  details: {},
@@ -403,3 +403,103 @@ export const SAFE_BASH_PRESETS = {
403
403
  allowPatterns: [],
404
404
  },
405
405
  };
406
+
407
+ /**
408
+ * === Whitelist Mode (opt-in, additive) ===
409
+ *
410
+ * A deny-by-default mode that checks the first token of a command against an
411
+ * explicit allowlist of read-only utilities. Enabled via
412
+ * `PI_CREW_SAFE_BASH_MODE=whitelist`. When NOT enabled, the legacy blacklist
413
+ * `isDangerous()` path is used unchanged.
414
+ */
415
+
416
+ /** Commands permitted under whitelist mode (read-only utilities only). */
417
+ const WHITELISTED_COMMANDS = new Set<string>([
418
+ "ls",
419
+ "cat",
420
+ "head",
421
+ "tail",
422
+ "wc",
423
+ "grep",
424
+ "find",
425
+ "echo",
426
+ "pwd",
427
+ "date",
428
+ "whoami",
429
+ "uname",
430
+ "df",
431
+ "du",
432
+ "file",
433
+ "stat",
434
+ ]);
435
+
436
+ /**
437
+ * Shell operators/metacharacters that could chain or substitute a command past
438
+ * the first token. Their presence anywhere in the raw command causes the
439
+ * whitelist check to reject, so e.g. `ls; rm file` cannot smuggle `rm` through
440
+ * under a permitted first token.
441
+ */
442
+ const SHELL_METACHARACTER_RE = /[|;&`()<>]|\$\(/;
443
+
444
+ /**
445
+ * Simple shell tokenizer: extract the first token (command name) of a command,
446
+ * respecting single and double quotes. Leading whitespace is skipped. Quote
447
+ * characters are not included in the returned token (`"ls"` → `ls`). An
448
+ * UNMATCHED quote is treated as malformed input and returns an empty string,
449
+ * causing `isAllowedWhitelist()` to reject the command (unmatched quotes can
450
+ * indicate malformed injection attempts).
451
+ */
452
+ function shellFirstToken(command: string): string {
453
+ let i = 0;
454
+ const len = command.length;
455
+ while (i < len && /\s/.test(command[i])) i++;
456
+ let token = "";
457
+ while (i < len) {
458
+ const ch = command[i];
459
+ if (/\s/.test(ch)) break;
460
+ if (ch === "'" || ch === '"') {
461
+ const quote = ch;
462
+ i++;
463
+ while (i < len && command[i] !== quote) {
464
+ token += command[i];
465
+ i++;
466
+ }
467
+ // Unmatched quote → malformed input, reject by returning empty string.
468
+ if (i >= len) return "";
469
+ i++; // skip closing quote
470
+ continue;
471
+ }
472
+ token += ch;
473
+ i++;
474
+ }
475
+ return token;
476
+ }
477
+
478
+ /**
479
+ * Whitelist check (deny-by-default). Returns true only when the command's first
480
+ * token is in the allowlist AND no shell operators/metacharacters are present.
481
+ * Quoted first tokens (e.g. `"ls" -la`) are resolved before checking.
482
+ */
483
+ export function isAllowedWhitelist(command: string): boolean {
484
+ if (command.trim() === "") return false;
485
+ if (SHELL_METACHARACTER_RE.test(command)) return false;
486
+ const firstToken = shellFirstToken(command);
487
+ if (firstToken === "") return false;
488
+ return WHITELISTED_COMMANDS.has(firstToken);
489
+ }
490
+
491
+ /** Current safe-bash mode, controlled by the `PI_CREW_SAFE_BASH_MODE` env var. */
492
+ export function getSafeBashMode(): "blacklist" | "whitelist" {
493
+ return process.env.PI_CREW_SAFE_BASH_MODE === "whitelist" ? "whitelist" : "blacklist";
494
+ }
495
+
496
+ /**
497
+ * Unified command check that dispatches on the active mode.
498
+ * @returns Error message if the command should be blocked, null if allowed.
499
+ */
500
+ export function checkCommand(command: string, options: SafeBashOptions = {}): string | null {
501
+ if (getSafeBashMode() === "whitelist") {
502
+ return isAllowedWhitelist(command) ? null : "Command blocked by safe_bash whitelist: command not in allowlist";
503
+ }
504
+ return isDangerous(command, options);
505
+ }
@@ -21,10 +21,59 @@ const KNOWN_PROVIDER_KEYS = new Set([
21
21
  "ZERODEV_API_KEY",
22
22
  ]);
23
23
 
24
+ // Map from provider prefix (lowercase) to env var names.
25
+ // Used by providerEnvKeys() to scope API keys per task.
26
+ const PROVIDER_ENV_KEY_MAP: Record<string, string[]> = {
27
+ minimax: ["MINIMAX_API_KEY", "MINIMAX_GROUP_ID"],
28
+ openai: ["OPENAI_API_KEY", "OPENAI_ORG_ID"],
29
+ anthropic: ["ANTHROPIC_API_KEY"],
30
+ google: ["GOOGLE_API_KEY", "GOOGLE_GENERATIVE_LANGUAGE_API_KEY"],
31
+ gemini: ["GOOGLE_API_KEY", "GOOGLE_GENERATIVE_LANGUAGE_API_KEY"],
32
+ azure: ["AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT"],
33
+ "azure-openai": ["AZURE_OPENAI_API_KEY", "AZURE_OPENAI_ENDPOINT"],
34
+ aws: ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION"],
35
+ bedrock: ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_REGION"],
36
+ zai: ["ZEU_API_KEY"],
37
+ zerodev: ["ZERODEV_API_KEY"],
38
+ };
39
+
24
40
  function isKnownProviderKey(key: string): boolean {
25
41
  return KNOWN_PROVIDER_KEYS.has(key);
26
42
  }
27
43
 
44
+ /**
45
+ * Extract provider prefix from a model ID string and return the corresponding
46
+ * env var names. Returns empty array for unknown/custom providers or invalid input.
47
+ *
48
+ * @param modelId - Model in "provider/modelId" format (e.g. "openai/gpt-4o")
49
+ * @returns Array of env var names needed for this provider
50
+ */
51
+ export function providerEnvKeys(modelId: string | undefined): string[] {
52
+ if (!modelId) return [];
53
+ const separatorIndex = modelId.indexOf("/");
54
+ if (separatorIndex <= 0) return [];
55
+ const provider = modelId.substring(0, separatorIndex).toLowerCase();
56
+ return PROVIDER_ENV_KEY_MAP[provider] ?? [];
57
+ }
58
+
59
+ /**
60
+ * Build a scoped allowlist that includes system vars + only the provider keys
61
+ * relevant to the assigned model (and its fallback chain).
62
+ *
63
+ * @param baseAllowList - Non-provider system vars to always include
64
+ * @param models - Model IDs to include provider keys for (primary + fallbacks)
65
+ * @returns Combined allowlist with scoped provider keys
66
+ */
67
+ export function buildScopedAllowList(baseAllowList: string[], models: (string | undefined)[]): string[] {
68
+ const providerKeys = new Set<string>();
69
+ for (const model of models) {
70
+ for (const key of providerEnvKeys(model)) {
71
+ providerKeys.add(key);
72
+ }
73
+ }
74
+ return [...baseAllowList, ...providerKeys];
75
+ }
76
+
28
77
  export interface SanitizeEnvOptions {
29
78
  /** Allow-list of env var names to preserve. Supports trailing glob, e.g. `"PI_*"`. */
30
79
  allowList?: string[];