runtime-boundary-env.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Child-process environment builder — prevents runtime boundary state from
  3. * leaking across process generations.
  4. *
  5. * Modes:
  6. * - `spawn-daemon`: starting a new daemon instance; daemon identity must come
  7. * from args/files, never inherited env.
  8. * - `agent-runtime`: daemon starts an agent framework child; parent hints are
  9. * injected per spawn and must not be mistaken for child
  10. * daemon identity.
  11. *
  12. * @module
  13. */
  14. /**
  15. * Runtime-injected env vars that must NOT leak into repo/worktree daemon children.
  16. * These describe "who the parent runtime is", not "what the child should be".
  17. *
  18. * `MAVIS_ACCESS_TOKEN` is included as a defensive fallback: the supported entry
  19. * point for the user identity token is `__MAVIS_PARENT_ACCESS_TOKEN`
  20. * (see {@link PARENT_HINT_KEYS}). Stripping the unprefixed name here ensures
  21. * stale env vars from older Electron builds, manual exports, or grandchild
  22. * inheritance never silently take effect inside a fresh daemon.
  23. */
  24. export const MANAGED_RUNTIME_KEYS = [
  25. 'MAVIS_ACCESS_TOKEN',
  26. 'MAVIS_REGION',
  27. 'MAVIS_BUILD_ENV',
  28. '__MAVIS_DISABLE_SAFETY',
  29. 'MAVIS_SQLITE3_MODULE_PATH',
  30. 'ELECTRON_RUN_AS_NODE',
  31. ] as const;
  32. /** Legacy runtime identity vars. Daemon identity now travels via args/files. */
  33. export const LEGACY_RUNTIME_ENV_KEYS = [
  34. '__MAVIS_RUNTIME_PORT',
  35. '__MAVIS_RUNTIME_DATA_DIR',
  36. '__MAVIS_RUNTIME_PROFILE',
  37. '__MAVIS_RUNTIME_DISABLE_GIT_AUTO_CONFIG',
  38. '__MAVIS_RUNTIME_SKIP_PID_PORT',
  39. '__MAVIS_RUNTIME_MANAGED',
  40. '__MAVIS_RUNTIME_AGENT_NAME',
  41. '__MAVIS_RUNTIME_SESSION_ID',
  42. '__MAVIS_RUNTIME_DAEMON_URL',
  43. 'MAVIS_PORT',
  44. 'MINIMAX_DATA_DIR',
  45. 'MAVIS_DATA_DIR',
  46. 'MAVIS_PROFILE',
  47. 'MAVIS_SKIP_PID_PORT',
  48. 'MAVIS_MANAGED_RUNTIME',
  49. 'AGENTARCHON_PORT',
  50. 'AGENTARCHON_DATA_DIR',
  51. 'AGENTARCHON_PROFILE',
  52. ] as const;
  53. /** @deprecated Use LEGACY_RUNTIME_ENV_KEYS. Kept for test compatibility. */
  54. export const RUNTIME_IDENTITY_KEYS = LEGACY_RUNTIME_ENV_KEYS;
  55. /** Parent → direct-child hints. Strip before spawning a new daemon. */
  56. export const PARENT_HINT_KEYS = [
  57. '__MAVIS_PARENT_DAEMON_URL',
  58. '__MAVIS_PARENT_DATA_DIR',
  59. '__MAVIS_PARENT_SESSION_ID',
  60. '__MAVIS_PARENT_AGENT_NAME',
  61. '__MAVIS_PARENT_MANAGED',
  62. '__MAVIS_PARENT_ACCESS_TOKEN',
  63. '__MAVIS_PARENT_REAL_USER_ID',
  64. '__MAVIS_PARENT_USER_EMAIL',
  65. '__MAVIS_PARENT_USER_NAME',
  66. '__MAVIS_PARENT_SUB_USER_NAME',
  67. '__MAVIS_PARENT_AUTH_SECRET',
  68. ] as const;
  69. /**
  70. * ASR proxy auth env vars must not reach agent children. JWT vars are
  71. * legacy and ignored. Kept for defense-in-depth even though Seed / Qwen
  72. * providers were retired and the daemon no longer speaks to the shared
  73. * asr-proxy: an upgrading user's shell profile may still export these,
  74. * and stripping them before they reach agent-runtime children costs
  75. * nothing.
  76. */
  77. export const ASR_PROXY_AUTH_ENV_KEYS = [
  78. 'ASR_PROXY_JWT_SECRET',
  79. 'ASR_PROXY_TOKEN',
  80. 'ASR_TENANT_ID',
  81. ] as const;
  82. export type BuildChildEnvMode = 'spawn-daemon' | 'agent-runtime';
  83. const RUNTIME_BOUNDARY_KEY_NAMES = new Set<string>([
  84. ...MANAGED_RUNTIME_KEYS,
  85. ...LEGACY_RUNTIME_ENV_KEYS,
  86. ...PARENT_HINT_KEYS,
  87. ]);
  88. const AGENT_RUNTIME_BOUNDARY_KEY_NAMES = new Set<string>(ASR_PROXY_AUTH_ENV_KEYS);
  89. function stripRuntimeBoundaryKeys(env: NodeJS.ProcessEnv, mode: BuildChildEnvMode): void {
  90. for (const key of Object.keys(env)) {
  91. // Windows environment names are case-insensitive. Normalize here as well so copied
  92. // plain objects cannot bypass the same boundary with differently-cased keys.
  93. const normalizedKey = key.toUpperCase();
  94. if (
  95. normalizedKey.startsWith('__MAVIS_RUNTIME_') ||
  96. normalizedKey.startsWith('__MAVIS_PARENT_') ||
  97. normalizedKey.startsWith('__MAVIS_ORIGIN_XDG_') ||
  98. normalizedKey.startsWith('AGENTARCHON_') ||
  99. normalizedKey.startsWith('AGENT_ARCHON_') ||
  100. normalizedKey === 'MAVIS_AGENT' ||
  101. normalizedKey === 'MAVIS_SESSION' ||
  102. RUNTIME_BOUNDARY_KEY_NAMES.has(normalizedKey) ||
  103. (mode === 'agent-runtime' && AGENT_RUNTIME_BOUNDARY_KEY_NAMES.has(normalizedKey))
  104. ) {
  105. delete env[key];
  106. }
  107. }
  108. }
  109. /**
  110. * Build env for a child process.
  111. *
  112. * @param mode
  113. * - `'spawn-daemon'` — strip runtime and parent boundary keys before starting
  114. * a new daemon. Daemon identity must be passed through args and files.
  115. * - `'agent-runtime'` — strip inherited boundary keys before starting an
  116. * agent framework child; callers may explicitly inject one-hop parent hints.
  117. *
  118. * @param overrides — explicit env vars to set (override inherited values).
  119. * Set a key to `undefined` to remove it from the result.
  120. */
  121. export function buildChildEnv(
  122. mode: BuildChildEnvMode,
  123. overrides: Record<string, string | undefined> = {},
  124. ): NodeJS.ProcessEnv {
  125. const base: NodeJS.ProcessEnv = { ...process.env };
  126. stripRuntimeBoundaryKeys(base, mode);
  127. return { ...base, ...overrides };
  128. }
  129. /**
  130. * Strip runtime-boundary env vars from `process.env` in-place.
  131. *
  132. * Call at daemon startup once explicit args have been consumed. This removes
  133. * inherited legacy runtime identity and one-hop parent hints so they cannot
  134. * affect the daemon's own behavior or leak to grandchildren.
  135. */
  136. export function stripManagedRuntimeEnv(): void {
  137. stripRuntimeBoundaryKeys(process.env, 'agent-runtime');
  138. }
  139. /**
  140. * Strip runtime-boundary env vars from the given env object in place and
  141. * report which keys were removed.
  142. *
  143. * Same semantics as the internal boundary strip used by {@link buildChildEnv};
  144. * exported so other spawn boundaries (e.g. the bash tool subprocess sanitizer
  145. * in `bash-subprocess-env.ts`) can reuse the exact same key lists instead of
  146. * forking them. Only keys that were present with a defined value are reported.
  147. */
  148. export function stripRuntimeBoundaryKeysFrom(
  149. env: NodeJS.ProcessEnv,
  150. mode: BuildChildEnvMode,
  151. ): string[] {
  152. const present = Object.keys(env).filter((key) => env[key] !== undefined);
  153. stripRuntimeBoundaryKeys(env, mode);
  154. return present.filter((key) => !(key in env));
  155. }
  156. export function findLegacyRuntimeEnvKeys(env: NodeJS.ProcessEnv = process.env): string[] {
  157. return Object.keys(env).filter(
  158. (key) =>
  159. key.startsWith('__MAVIS_RUNTIME_') ||
  160. key === 'MAVIS_PORT' ||
  161. key === 'MINIMAX_DATA_DIR' ||
  162. key === 'MAVIS_DATA_DIR' ||
  163. key === 'MAVIS_PROFILE' ||
  164. key === 'MAVIS_SKIP_PID_PORT' ||
  165. key === 'MAVIS_MANAGED_RUNTIME',
  166. );
  167. }