Skip to content

fix(git): prevent .env and .mcp.json leak on initialize_github_sync (#458) - #460

Closed
vybe wants to merge 1 commit into
mainfrom
feature/458-gitignore-credentials
Closed

vybe wants to merge 1 commit into
mainfrom
feature/458-gitignore-credentials

Conversation

@vybe

@vybe vybe commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • P0 credential exfiltration fix. initialize_github_sync previously committed .env and .mcp.json to GitHub on every first sync, regardless of whether the workspace had a .gitignore.
  • The hardcoded .gitignore shipped by git_service.initialize_git_in_container covered only shell/cache files and used cat > .gitignore, clobbering any user-supplied rules. The legacy /home/developer/workspace path skipped the .gitignore write entirely.
  • Now: a comprehensive credential deny-list is merged (append-only, idempotent, exact-line deduped) into any pre-existing .gitignore, on both the standard and legacy paths.

Changes

src/backend/services/git_service.py

  • New DEFAULT_GITIGNORE_DENYLIST module constant: credentials first (.env, .env.*, !.env.example, .mcp.json, credentials.json, token.json, *.pem, *.key, *.p12, *.pfx, .aws/credentials, .config/gcloud/), then existing shell/cache entries (preserved for backwards compat).
  • New _build_gitignore_merge_command(git_dir) helper: generates a bash script that appends only missing entries to any existing .gitignore, using grep -qxF for exact-line matching. Inner script body and pattern list are both base64-encoded so shell metacharacters in patterns (!, *, /) can't reach the shell.
  • Call site hoisted out of the if git_dir == "/home/developer": block so the legacy /home/developer/workspace path also gets the deny-list.
  • Merge failure now aborts init with a descriptive error before git add . runs — better than committing credentials on a half-configured agent.

tests/unit/test_github_init_gitignore.py (new, 14 tests)

  • Execute the generated bash against real tmp dirs (catches shell-quoting bugs production would hit).
  • Cover all acceptance criteria: no .gitignore → deny-list written; existing user rules preserved; legacy workspace path covered; idempotency across re-runs; no-trailing-newline edge case; merge runs before git add .; failure aborts init.

docs/memory/feature-flows/github-repo-initialization.md

Test Plan

  • pytest tests/unit/test_github_init_gitignore.py -v → 14/14 pass
  • pytest tests/unit/test_github_init_push.py tests/unit/test_github_pat_recreation.py tests/unit/test_git_pull_branch.py tests/test_github_pat_propagation_unit.py → 31/31 pass (no regressions)
  • Manual smoke: ran the generated bash against a tmp dir — fresh-file case, existing-user-rules case, partial-overlap case, no-trailing-newline case, idempotency re-run all produced correct output.
  • End-to-end verification on a real agent: deploy agent → inject_credentials → initialize_github_sync → inspect initial commit tree — asserts .env and .mcp.json absent. (Cannot run from this sandbox; suggest running before merge.)

Acceptance Criteria (from #458)

  • .env, .env.*, .mcp.json, credentials.json, token.json, *.pem, *.key never committed
  • Existing workspace .gitignore preserved (appended to, not overwritten)
  • Legacy /home/developer/workspace branch also writes the deny-list
  • Regression test: deploy → inject → init → assert no secrets in tree (unit-test equivalent via command inspection + tmp-dir execution)
  • Regression test: pre-existing .gitignore → user rules and deny-list both present

Notes

  • The !.env.example negation ensures committable env templates still work.
  • Shell-injection surface is zero: git_dir is one of two hardcoded constants, and the outer command form is bash -c 'echo <base64> | base64 -d | bash' where the encoded segment is [A-Za-z0-9+/=] only.

Closes #458

🤖 Generated with Claude Code

…458)

initialize_git_in_container used a hardcoded `cat > .gitignore <<EOF`
heredoc with a narrow shell/cache deny-list that did NOT include
`.env`, `.mcp.json`, or any other credential pattern. It also
overwrote any workspace-supplied `.gitignore` and skipped the legacy
/home/developer/workspace path entirely. Agents with credentials
injected via `inject_credentials` therefore leaked `.env` and
`.mcp.json` on the "Initial commit from Trinity Agent" push.

This is a P0 credential exfiltration path that fired on every first
GitHub sync.

Changes (src/backend/services/git_service.py):

- New module constant `DEFAULT_GITIGNORE_DENYLIST`: credentials first
  (`.env`, `.env.*`, `!.env.example`, `.mcp.json`, `credentials.json`,
  `token.json`, `*.pem`, `*.key`, `*.p12`, `*.pfx`, `.aws/credentials`,
  `.config/gcloud/`) then existing shell/cache entries.
- New helper `_build_gitignore_merge_command(git_dir)`: generates a
  bash script that appends missing deny-list entries to any existing
  `.gitignore` using exact-line `grep -qxF` checks. Idempotent.
  Inner script body + pattern payload are both base64-encoded so shell
  metacharacters in patterns (`!`, `*`, `/`) can't reach the shell.
- Call site hoisted out of the `if git_dir == "/home/developer":`
  block so the legacy workspace path also gets the deny-list.
- Merge failure now aborts init with a descriptive error — better
  than commiting credentials on a half-configured agent.

Regression tests (tests/unit/test_github_init_gitignore.py, 14 tests):

- AC #1: deny-list present when no `.gitignore` exists.
- AC #2: pre-existing user rules preserved alongside deny-list.
- AC #3: legacy /home/developer/workspace path also protected.
- Idempotency, managed-header uniqueness, no-trailing-newline edge
  case, command-injection safety, ordering vs `git add .`, failure
  abort behavior.

Closes #458

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@vybe vybe closed this Apr 22, 2026
@vybe
vybe deleted the feature/458-gitignore-credentials branch April 22, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: initialize_github_sync commits .env and .mcp.json despite .gitignore

1 participant