Skip to content

Conversation

@bigph00t
Copy link
Owner

Summary

  • Uses centralized credentials from ~/.claude-mem/.env to prevent API key hijacking
  • Adds EnvManager for secure credential handling
  • Prevents SDK from bypassing Claude Code CLI billing

Issue

Fixes thedotmack#733

Test plan

  • Verify credentials are loaded from ~/.claude-mem/.env
  • Verify project-level .env files cannot override API keys
  • Test that SDK uses centralized credentials correctly

🤖 Generated with Claude Code

…PI key hijacking (thedotmack#733)

This fixes Issue thedotmack#733 where claude-mem would incorrectly use ANTHROPIC_API_KEY from
random project .env files instead of the user's configured Claude Code CLI subscription.

Root cause: The SDK's `query()` function inherits from `process.env` when no `env`
option is passed. When users work in projects with their own .env files containing
API keys, the SDK would discover and use those keys, billing the wrong account.

Solution: Centralized credential management via ~/.claude-mem/.env

Changes:
- Add EnvManager.ts: Centralized credential storage and isolated env builder
- SDKAgent: Pass isolated env to SDK query() that only includes credentials from
  ~/.claude-mem/.env, not random keys from process.env inheritance
- GeminiAgent/OpenRouterAgent: Use getCredential() instead of process.env fallback
- SettingsDefaultsManager: Add CLAUDE_MEM_CLAUDE_AUTH_METHOD setting ('cli' | 'api')

How it works:
1. buildIsolatedEnv() creates a clean environment with only essential system vars
   (PATH, HOME, etc.) and credentials explicitly configured in ~/.claude-mem/.env
2. SDK subprocess runs with this isolated env, never seeing random API keys
3. If no ANTHROPIC_API_KEY is in ~/.claude-mem/.env, Claude Code CLI billing is used
4. Same pattern applied to Gemini/OpenRouter agents for consistency

This ensures claude-mem always uses the user's intended billing method, regardless
of what .env files exist in their working directory.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@greptile-apps
Copy link

greptile-apps bot commented Jan 18, 2026

Greptile Summary

  • Implements centralized credential management through EnvManager to prevent API key hijacking from project-level .env files
  • Adds authentication method configuration (CLAUDE_MEM_CLAUDE_AUTH_METHOD) defaulting to 'cli' to maintain Claude Code CLI billing integrity
  • Updates SDK agents (GeminiAgent, OpenRouterAgent, SDKAgent) to use isolated credentials from ~/.claude-mem/.env instead of process.env

Important Files Changed

Filename Overview
plugin/scripts/worker-service.cjs Critical error: Worker service file is completely empty, breaking core functionality
src/shared/EnvManager.ts New centralized credential manager; provides isolated environment building and secure .env parsing
src/services/worker/SDKAgent.ts Updated to use isolated environment for Agent SDK; prevents credential pollution from project files

Confidence score: 0/5

  • This PR introduces a critical production-breaking bug where the worker service is completely empty
  • Score reduced from 4/5 to 0/5 due to plugin/scripts/worker-service.cjs having zero content, which will cause immediate service failure
  • The worker service is essential infrastructure running on port 37777 that handles AI processing, memory capture, and context injection

Sequence Diagram

sequenceDiagram
    participant User
    participant SDK as "SDKAgent"
    participant EnvManager as "EnvManager"
    participant Settings as "SettingsDefaultsManager"
    participant ClaudeCode as "Claude Code CLI"

    User ->> SDK: "Start memory session"
    SDK ->> Settings: "loadFromFile(~/.claude-mem/settings.json)"
    Settings -->> SDK: "Configuration settings"
    SDK ->> EnvManager: "buildIsolatedEnv()"
    EnvManager ->> EnvManager: "loadClaudeMemEnv(~/.claude-mem/.env)"
    EnvManager -->> SDK: "Isolated environment variables"
    SDK ->> ClaudeCode: "Start with isolated env"
    ClaudeCode -->> SDK: "Memory session responses"
    SDK -->> User: "Memory context generated"
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Comments (2)

  1. plugin/scripts/worker-service.cjs, line 1 (link)

    logic: Worker service file is completely empty - this will break Claude-Mem's core functionality since the worker service handles AI processing and runs the localhost:37777 API. Was this file accidentally emptied during the credential changes, or is this intentional?

  2. src/services/worker/OpenRouterAgent.ts, line 92 (link)

    style: Error message should reference the new credential source - mentions OPENROUTER_API_KEY environment variable but that's no longer checked

7 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

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.

SDK uses ANTHROPIC_API_KEY from .env* files instead of Claude Code CLI

1 participant