Archon/migrations
Rasmus Widing a1e8a16b82
fix(env): detect and refuse target-repo .env with sensitive keys (#1036)
* fix(env): detect and refuse target-repo .env with sensitive keys (#1034)

Bun auto-loads .env from subprocess CWD regardless of the clean env
passed to Bun.spawn, silently overriding OAuth auth and billing the
wrong API account. This adds a consent-based gate at registration time
and a pre-spawn safety net in both Claude and Codex clients.

Changes:
- Add env-leak-scanner utility that checks 6 auto-loaded .env filenames
  for 7 sensitive keys (ANTHROPIC_API_KEY, OPENAI_API_KEY, etc.)
- Add allow_env_keys boolean column to codebases table (migration 021)
- Gate registerRepoAtPath to reject codebases with sensitive .env keys
  unless explicitly consented via allowEnvKeys flag
- Add pre-spawn check in ClaudeClient and CodexClient sendQuery methods
- Return 422 from POST /api/codebases on env leak detection
- Surface env leak error in web UI with "Allow env keys" checkbox
- Classify EnvLeakError as FATAL in workflow executor

Fixes #1034

* fix: address review findings for env leak scanner PR

- Fix FATAL_PATTERNS 'env leak' pattern that never matched EnvLeakError.message;
  now checks error.name === 'EnvLeakError' directly (immune to message rewording)
- Fix pre-spawn consent lookup for worktree paths: add findCodebaseByPathPrefix()
  and use it as fallback when exact match returns null; prevents opt-in from
  being silently ineffective for workflow-based runs
- Add allow_env_keys column to 000_combined.sql CREATE TABLE and idempotent ALTER
  section to fix fresh PostgreSQL installs
- Remove non-existent --allow-env-keys CLI flag from error message; replace with
  web UI-only instruction
- Narrow isEnvLeakError check from error.message.includes('env') to startsWith('Cannot add codebase')
- Distinguish ENOENT (skip) from EACCES/other errors in scanner catch block;
  unreadable files now surface as findings to avoid silently bypassing the gate
- Use cross-platform grep command instead of macOS-specific sed -i '' syntax
- Add audit log (log.warn) when 422 EnvLeakError is returned from API
- Add pre-spawn gate tests to claude.test.ts and codex.test.ts (4 tests each)
- Add env leak gate tests to clone.test.ts (2 tests)
- Add 422 and allowEnvKeys passthrough tests to api.codebases.test.ts

* simplify: reduce complexity in changed files
2026-04-08 09:43:47 +03:00
..
000_combined.sql fix(env): detect and refuse target-repo .env with sensitive keys (#1036) 2026-04-08 09:43:47 +03:00
001_initial_schema.sql feat: implement telegram + claude mvp with generic architecture 2025-11-10 19:35:50 -06:00
002_command_templates.sql Add global command templates feature (#20) 2025-12-02 10:52:02 +02:00
003_add_worktree.sql Add git worktree support for isolated parallel development (#27) 2025-12-03 10:42:24 +02:00
004_worktree_sharing.sql SQL, documentation, and Docker touch ups 2025-12-05 07:12:12 -06:00
005_isolation_abstraction.sql Add isolation provider abstraction for worktree management (#87) 2025-12-17 11:10:16 +02:00
006_isolation_environments.sql Add unified isolation environment architecture (Phase 2.5) (#92) 2025-12-17 15:57:12 +02:00
007_drop_legacy_columns.sql Drop legacy isolation columns (Phase 4) (#99) 2025-12-17 20:12:11 +02:00
008_workflow_runs.sql Add workflow engine for multi-step AI orchestration 2026-01-02 11:31:04 +02:00
009_workflow_last_activity.sql Fix: Add stale workflow cleanup and defense-in-depth error handling (#237) 2026-01-15 21:31:38 +02:00
010_immutable_sessions.sql feat: Add session state machine with immutable sessions for audit trail (#302) 2026-01-19 22:14:39 +02:00
011_partial_unique_constraint.sql fix: Replace full unique constraint with partial index on isolation environments (#239) 2026-01-30 17:29:53 +02:00
012_workflow_events.sql Archon Web UI: React frontend, web adapter, API routes, workflow events 2026-02-16 09:03:44 +02:00
013_conversation_titles.sql Archon Web UI: React frontend, web adapter, API routes, workflow events 2026-02-16 09:03:44 +02:00
014_message_history.sql Archon Web UI: React frontend, web adapter, API routes, workflow events 2026-02-16 09:03:44 +02:00
015_background_dispatch.sql Archon Web UI: React frontend, web adapter, API routes, workflow events 2026-02-16 09:03:44 +02:00
016_session_ended_reason.sql Store session deactivation reasons in database (#303) (#385) 2026-02-16 13:54:41 +02:00
017_drop_command_templates.sql Deprecate database command templates (#425) 2026-02-17 16:55:17 +02:00
018_fix_workflow_status_default.sql SQLite-first docs, fix Postgres workflow_runs default, rebuild combined schema 2026-02-17 11:23:59 -06:00
019_workflow_resume_path.sql feat: resume failed workflows from prior artifacts on same branch (#440) 2026-02-18 12:29:30 +02:00
020_codebase_env_vars.sql feat: per-project env var management via config and Web UI (#852) 2026-04-06 15:30:45 +03:00
021_add_allow_env_keys_to_codebases.sql fix(env): detect and refuse target-repo .env with sensitive keys (#1036) 2026-04-08 09:43:47 +03:00