Skip to content

Secrets Protection ​

GhostAI's secrets protection layer detects and protects API keys, credentials, tokens, and other sensitive values before they reach AI models. It sits between AI coding tools (Cursor, Claude Code, Windsurf) and LLM providers, scanning requests and responses in real time.

Overview ​

When developers use AI coding assistants, their code context—including .env files, config files, and source code—is sent to third-party model providers. GhostAI intercepts this traffic, detects secrets using pattern matching and entropy analysis, and applies configurable policies (warn, block, or redact) before forwarding requests.

Key capabilities:

  • 20+ built-in secret patterns for AWS, GitHub, Stripe, OpenAI, database URLs, private keys, and more
  • High-entropy detection for unknown secret formats
  • Policy modes: warn (flag only), block (reject request), redact (tokenize before sending)
  • Secret budget with automatic escalation when limits are exceeded
  • Response scanning to catch hallucinated or regurgitated secrets in LLM output
  • .env guard for environment file content
  • Audit logging for compliance and rotation tracking

Supported Secret Types ​

CategoryTypesConfidence
CloudAWS Access Key, AWS Secret Key, Google API Key, Google OAuth Client ID0.90–0.95
Source ControlGitHub Token (PAT, App), GitLab PAT0.95
AI/MLOpenAI API Key0.90
PaymentStripe API Key (test + live)0.95
CommunicationSlack Token, Slack Webhook, Twilio, SendGrid, Mailchimp0.85–0.95
Package RegistriesNPM Token, PyPI Token0.95
DatabasesPostgreSQL, MySQL, MongoDB, Redis, AMQP connection URLs0.95
AuthenticationJWT, Bearer Tokens, Passwords in URLs0.80–0.85
CryptographicRSA, EC, OpenSSH Private Keys0.99
GenericGeneric API Key patterns, Environment variable secrets0.70–0.85
EntropyUnknown high-entropy strings0.60–0.75

How Detection Works ​

Pattern-Based Detection ​

Each secret type has a regex pattern tuned for known formats:

  • AWS Access Key: AKIA, AGPA, AIDA, etc. + 16 alphanumeric chars
  • GitHub Token: ghp_, ghu_, gho_, etc. + 36+ chars
  • OpenAI Key: sk- + 48 alphanumeric chars
  • Stripe Key: sk_live_ or pk_test_ + 24+ chars
  • Database URL: postgres://, mysql://, etc. with embedded credentials
  • Private Keys: -----BEGIN RSA PRIVATE KEY-----, etc.

Entropy Detection ​

Strings longer than 16 characters with Shannon entropy above a configurable threshold (default 4.5 bits/character) are flagged as potential secrets. Common false positives are filtered:

  • UUIDs (e.g., 550e8400-e29b-41d4-a716-446655440000)
  • Git SHAs (40 hex chars)
  • CSS hex colors (e.g., #ff0000)
  • Base64-encoded non-secret text (decoded and validated)

.env Guard ​

When scanning .env files or text with filename context ending in .env, GhostAI parses KEY=value pairs and flags values for keys known to hold secrets (e.g., API_KEY, PASSWORD, DATABASE_URL, AWS_SECRET_ACCESS_KEY).

Policy Modes ​

ModeBehavior
warnSecrets are detected and flagged in response metadata, but pass through unchanged. Useful for awareness without disruption.
blockRequests containing secrets are rejected with HTTP 400. Strictest mode for maximum protection.
redactSecrets are replaced with reversible tokens (e.g., [OPENAI_KEY_a1b2c3]) before forwarding. Default and recommended.

Secret Budget ​

A per-request secret limit (max_secrets_per_request, default 5) controls policy escalation:

  • Under budget: Use configured policy (warn/block/redact).
  • Over budget in warn mode: Escalates to redact.
  • Over budget in redact mode: Escalates to block.

This prevents accidental exposure when many secrets appear in a single message.

Response Scanning ​

When scan_responses is enabled (default: true), LLM responses are scanned for secrets before being returned. Detected secrets are redacted with [REDACTED_*] markers. This catches:

  • Hallucinated API keys
  • Regurgitated training data
  • Leaked context from other users (in multi-tenant setups)

Setup for AI Tools ​

Cursor ​

  1. Open Cursor Settings → Models
  2. Set Custom API Base URL to http://localhost:8000/v1
  3. Use your GhostAI API key (starts with gst_)

See the MCP Server documentation for MCP and other options.

Claude Code ​

bash
export ANTHROPIC_BASE_URL=http://localhost:8000

Windsurf ​

Set Base URL to http://localhost:8000/v1 and enter your GhostAI API key.

Generic OpenAI SDK ​

python
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="gst_your_key")

Configuration ​

Via API ​

  • GET /api/settings/secrets-policy — Current policy and settings
  • PUT /api/settings/secrets-policy — Update policy, enable/disable detection, set budget, entropy threshold

Via UI ​

Settings → AI Protection → Secrets Protection

Environment Variables ​

VariableDefaultDescription
SECRETS_DETECTION_ENABLEDtrueEnable/disable secrets detection
SECRETS_POLICYredactwarn, block, or redact
SCAN_RESPONSEStrueScan LLM responses for secrets
MAX_SECRETS_PER_REQUEST5Secret budget before escalation
ENABLE_ENTROPY_DETECTIONtrueEnable high-entropy string detection
ENTROPY_THRESHOLD4.5Bits per character threshold (2.0–6.0)

Sensitive File Content Detection ​

The detector can identify sensitive file types without full pattern matching:

File TypeDetection Signal
pem_keyPEM/SSH/PGP private key blocks
terraform_state"sensitive": true in JSON
kubernetes_secretkind: Secret with base64 data blocks
credentials_jsonclient_secret, private_key, api_key in JSON
envMultiple KEY=value lines with secret-like keys

Use detect_sensitive_file_content() for pre-scan classification before full scan_text().

MCP Gateway Secrets Policy ​

The MCP Data Protection Gateway supports three secrets policy modes for agent-to-tool-server traffic:

ModeBehavior
redact (default)Secrets in tool call arguments are replaced with tokens before forwarding to upstream MCP servers
warnTool calls proceed; secrets are flagged in the audit trail for awareness
blockTool calls containing secrets are rejected with an error detailing the secret count

In block mode, the gateway uses scan_tool_call_for_secrets to recursively scan all tool call arguments before forwarding. If secrets are found, the agent receives an error message and the tool call is not executed.

Configure via secrets_policy in the gateway config:

python
gateway = GhostAIMCPGateway.from_config(config, secrets_policy="block")

Secret Vault Mappings ​

GhostAI can map detected secrets to entries in external vault providers, enabling a future flow where secrets are stripped before reaching AI models and re-injected from the vault on the way back.

ProviderStatus
HashiCorp VaultAPI scaffolded
AWS Secrets ManagerAPI scaffolded
1PasswordAPI scaffolded

CRUD endpoints are available at /api/insider-risk/vault-mappings for managing secret-to-vault-path associations.

SOC 2 Type II Certified | BAA Available | HIPAA-Ready