Skip to content

Security: Agent Credential Leakage in Execution Logs [FIXED] #15

Closed

Description

@vybe

Security Bug: Environment Variable Exposure via Agent Tools

Severity: High
Status: ✅ FIXED in commit 8c05bb3
Reported: 2026-02-13
Fixed: 2026-02-16


Description

Sensitive environment variables (including TRINITY_MCP_API_KEY, OPENAI_API_KEY, and custom credentials from .env) were being exposed in the execution_log stored in the database.

Attack Vectors

Vector Description
Command Leak Agent executes env or printenv via Bash, dumping all secrets to stdout
Direct Retrieval User asks "What is my API key?", agent reads environment and outputs the secret

Impact

In both cases, secrets were:

  1. ❌ Returned to the LLM (leaked to model provider)
  2. ❌ Persisted in the execution_log or response columns in the database
  3. ❌ Displayed in the UI history

Evidence

Verified on Production (2026-02-15):

  • Vector 1: Running "Check environment variables" resulted in a full dump of .env including TRINITY_MCP_API_KEY
  • Vector 2: Execution showed the agent successfully retrieving and displaying API tokens upon request

Example leaked pattern (BEFORE FIX):

TRINITY_MCP_API_KEY=trinity_mcp_...
TRINITY_MCP_URL=http://mcp-server:8080/mcp
OPENAI_API_KEY=sk-...

Fix Implementation

Multi-layer credential sanitization was implemented:

Agent-Side (Primary Layer)

File Change
docker/base-image/agent_server/utils/credential_sanitizer.py NEW - Sanitization utility
docker/base-image/agent_server/services/claude_code.py Sanitizes subprocess output and response text
docker/base-image/agent_server/routers/credentials.py Refreshes sanitizer cache after credential injection

Backend-Side (Defense-in-Depth)

File Change
src/backend/utils/credential_sanitizer.py NEW - Pattern-based sanitization
src/backend/routers/chat.py Sanitizes execution_log, tool_calls, response before DB persistence

Patterns Detected and Redacted

  • OpenAI keys (sk-*, sk-proj-*)
  • Anthropic keys (sk-ant-*)
  • GitHub tokens (ghp_*, gho_*, ghs_*, ghr_*, github_pat_*)
  • Slack tokens (xoxb-*, xoxp-*, xoxa-*)
  • AWS keys (AKIA*)
  • Trinity MCP keys (trinity_mcp_*)
  • Bearer/Basic auth headers
  • Sensitive key=value pairs (*API_KEY*, *SECRET*, *TOKEN*, *PASSWORD*, etc.)

Test Coverage

115 unit tests created - 100% passing

Category Tests
Pattern Detection 30
Key=Value Pairs 16
Nested Structures 18
JSON Sanitization 10
Execution Logs 12
Credential Cache 4
Edge Cases 18
Backend-Specific 3

Test files:

  • tests/unit/test_credential_sanitizer_agent.py (56 tests)
  • tests/unit/test_credential_sanitizer_backend.py (59 tests)

Deployment Required

# Rebuild base image with new sanitization code
./scripts/deploy/build-base-image.sh

# Restart backend
docker-compose restart backend

# Recreate agents to use new base image

QA Verification Checklist

  • Vector 1 (env): Run "Run env" - credentials are redacted (***REDACTED***)
  • Vector 2 (ask): Ask "What is my TRINITY_MCP_API_KEY?" - output is redacted
  • Database Check: SELECT execution_log FROM schedule_executions... contains no raw secrets
  • Regression: Normal agent operations still work

References

  • Bug Report: docs/bugs/AGENT_CREDENTIAL_LEAKAGE.md
  • Commit: 8c05bb3
  • Feature Flows Updated: execution-queue, parallel-headless-execution, credential-injection, execution-log-viewer

Activity

  1. added
    bug
    on Feb 16, 2026
  2. vybe commented on Feb 16, 2026

    @vybe
    ContributorAuthor

    Fixed in commit 8c05bb3. Credential sanitization implemented at agent and backend layers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions