Skip to content

[Security] API keys for AI provider (OpenAI/Anthropic) hardcoded or logged in Go service - credential leakage risk #242

Description

@anshul23102

Problem

Go microservices commonly include API keys in configuration structs that get logged at startup or included in error messages:

// Config logged at startup:
cfg := Config{
    OpenAIKey: os.Getenv("OPENAI_API_KEY"),
    Port:      "8080",
}
log.Printf("Starting service with config: %+v", cfg)
// Output: Starting service with config: {OpenAIKey:sk-xxx... Port:8080}

Or embedded as string constants:

const OPENAI_KEY = "sk-proj-..."  // committed to git

Proposed Fix

  1. Never log structs containing secrets. Use a custom String() method that redacts sensitive fields:
type Config struct {
    OpenAIKey string
    Port      string
}

// Prevents accidental logging of the key:
func (c Config) String() string {
    return fmt.Sprintf("{Port:%s OpenAIKey:[REDACTED]}", c.Port)
}

// Safe to log:
log.Printf("Starting service with config: %s", cfg)
  1. Fail fast if key is missing:
func loadConfig() Config {
    key := os.Getenv("OPENAI_API_KEY")
    if key == "" {
        log.Fatal("OPENAI_API_KEY environment variable is required")
    }
    return Config{OpenAIKey: key, Port: os.Getenv("PORT")}
}
  1. Add .env to .gitignore and run git log --all -S 'sk-' --source to check for past leaks.

Acceptance Criteria

  • No API keys in any source file or committed configuration
  • Config struct has a String() method that redacts all secret fields
  • Service refuses to start if required API keys are absent
  • CI pipeline runs trufflehog or gitleaks to detect accidental secret commits
  • .env.example documents all required secrets without values

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationtriageNeeds maintainer triage.type:docsDocumentation, API docs, examples, or contributor docs.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions