Skip to main content

Security

The AI Gateway provides powerful security features to protect your data and prevent abuse. Block sensitive information from being sent to AI providers, and stop malicious users from exploiting your applications.

Sensitive Data Protection

The Risk

When using AI assistants or building AI-powered apps, sensitive data can accidentally be exposed:
Real scenarios we’ve seen:
  • AWS keys sent in code context to ChatGPT
  • Database passwords included in error messages
  • Customer PII processed by AI for “analysis”
  • API secrets in environment variable debugging

Automatic Detection

The gateway scans all requests for sensitive patterns:
Sensitive data detection
Detected patterns include:
  • AWS Access Keys and Secret Keys
  • GitHub Personal Access Tokens
  • Database connection strings
  • API keys (various providers)
  • Private keys (RSA, SSH, etc.)
  • Credit card numbers
  • Social Security Numbers
  • Email addresses
  • Phone numbers

Blocking Sensitive Data

Configure the gateway to block requests containing sensitive data:
// Configure blocking rules
await leanmcp.gateway.setSecurityRules({
  blockPatterns: [
    { type: 'aws_key', action: 'block' },
    { type: 'api_key', action: 'block' },
    { type: 'password', action: 'warn' },
  ],
  alertOnDetection: true,
});

Action Types

ActionBehavior
blockRequest is rejected, never sent to AI provider
warnRequest proceeds but alert is generated
redactSensitive data is replaced with [REDACTED] before sending
logRequest proceeds, logged for review

Remediation

When sensitive data is detected:
  1. Review the log - see exactly what was exposed
  2. Rotate credentials - change any exposed secrets immediately
  3. Update your code - ensure secrets aren’t in files that get sent to AI
  4. Enable blocking - prevent future exposure
Keep secrets in .env files and ensure .env is in your .gitignore. Most AI assistants respect gitignore patterns.

Blocking Malicious Users

When building AI-powered applications, you need to protect against abuse.

Common Abuse Patterns

Prompt Injection

Users trying to manipulate your AI to bypass restrictions

Cost Attacks

Users making excessive requests to run up your AI costs

Data Extraction

Attempts to extract training data or system prompts

Jailbreaking

Trying to make the AI produce harmful content

User Blocking

Block abusive users instantly:
Block user interface
// Block a user via API
await leanmcp.gateway.blockUser({
  userId: 'abusive-user-123',
  reason: 'Excessive usage and prompt injection attempts',
  duration: 'permanent', // or '24h', '7d', etc.
});
When a blocked user makes a request:
  • Request is immediately rejected
  • No tokens are consumed
  • Event is logged for audit

Unblocking Users

// Unblock a user
await leanmcp.gateway.unblockUser({
  userId: 'user-123',
  reason: 'Issue resolved, user warned',
});

Viewing Blocked Users

Access the block list in your dashboard:
  1. Navigate to AI Gateway > Security > Blocked Users
  2. View all blocked users with reasons and timestamps
  3. Manage blocks (extend, reduce, remove)

Rate Limiting

Prevent abuse with intelligent rate limiting:
// Set rate limits
await leanmcp.gateway.setRateLimit({
  scope: 'per_user',
  limits: {
    requestsPerMinute: 20,
    requestsPerHour: 200,
    tokensPerDay: 500000,
    maxCostPerMonth: 100.00,
  },
  action: 'block', // or 'queue', 'throttle'
});

Rate Limit Strategies

StrategyUse Case
Per UserLimit individual user consumption
Per IPPrevent anonymous abuse
Per API KeyLimit by integration
GlobalOverall service protection

Handling Rate Limits

When users hit limits:
// Client receives 429 response
{
  "error": {
    "code": "rate_limit_exceeded",
    "message": "Too many requests. Please try again in 60 seconds.",
    "retry_after": 60
  }
}

Content Filtering

Block requests based on content:

Input Filtering

// Block certain input patterns
await leanmcp.gateway.setContentFilter({
  inputFilters: [
    { pattern: 'ignore previous instructions', action: 'block' },
    { pattern: 'reveal your system prompt', action: 'block' },
    { pattern: /jailbreak/i, action: 'warn' },
  ]
});

Output Filtering

// Filter AI responses
await leanmcp.gateway.setContentFilter({
  outputFilters: [
    { pattern: 'internal_api_endpoint', action: 'redact' },
    { pattern: /\b\d{4}-\d{4}-\d{4}-\d{4}\b/, action: 'redact' }, // Credit cards
  ]
});

Audit Logging

All security events are logged:
Audit log
Event TypeDetails Logged
blocked_requestUser, reason, request content
sensitive_data_detectedPattern, location, severity
rate_limit_hitUser, limit type, current count
user_blockedUser, reason, admin who blocked
user_unblockedUser, reason, admin who unblocked

Export Audit Logs

For compliance and review:
curl -X GET "https://api.leanmcp.com/gateway/audit-logs?days=30" \
  -H "Authorization: Bearer your-api-key" \
  -o audit-logs.json

Security Alerts

Get notified of security events:

Alert Configuration

await leanmcp.gateway.createSecurityAlert({
  events: ['sensitive_data_detected', 'rate_limit_exceeded', 'suspicious_pattern'],
  channels: {
    email: ['[email protected]'],
    slack: 'https://hooks.slack.com/...',
    webhook: 'https://your-server.com/alerts',
  },
  severity: 'medium', // or 'low', 'high', 'critical'
});

Alert Examples

Security alert example

Best Practices

Begin with ‘warn’ actions to understand what would be blocked, then switch to ‘block’ once tuned.
Check blocked requests weekly to ensure legitimate users aren’t being affected.
Configure security alerts before launch so you’re notified of issues immediately.
Set limits that allow normal use while preventing abuse. Adjust based on observed patterns.
Make sure users know your usage policies and what behavior will result in blocking.

Next Steps