Skip to content

fix: add DoS protection to OpenAI API calls#489

Open
deosha wants to merge 1 commit intoopenai:mainfrom
deosha:fix/add-dos-protection
Open

fix: add DoS protection to OpenAI API calls#489
deosha wants to merge 1 commit intoopenai:mainfrom
deosha:fix/add-dos-protection

Conversation

@deosha
Copy link
Copy Markdown

@deosha deosha commented Jan 12, 2026

Summary

This PR adds DoS protection to the get_chat_completion function to prevent API abuse and cost explosion.

The Problem

The get_chat_completion() function in services/openai.py lacks:

  • Request timeout configuration
  • Token/response limits
  • Input length validation

This allows attackers to send extremely large inputs or trigger expensive API calls without limits.

The Fix

Added configurable protections:

  • OPENAI_TIMEOUT - Request timeout (default: 30 seconds)
  • MAX_TOKENS - Maximum response tokens (default: 500)
  • MAX_INPUT_LENGTH - Maximum input characters (default: 10,000)

All settings are configurable via environment variables for flexibility.

Changes

# Before
response = openai.ChatCompletion.create(model=model, messages=messages)

# After  
if total_input_length > MAX_INPUT_LENGTH:
    raise ValueError(f"Input length exceeds maximum")
    
response = openai.ChatCompletion.create(
    model=model,
    messages=messages,
    max_tokens=max_tokens,
    timeout=timeout,
)

Identified using aisentry, an AI/LLM security scanner for OWASP LLM Top 10 vulnerabilities.

- Add configurable timeout (OPENAI_TIMEOUT env var, default 30s)
- Add max_tokens limit (MAX_TOKENS env var, default 500)
- Add input length validation (MAX_INPUT_LENGTH env var, default 10000 chars)
- Prevents API abuse and cost explosion from large/malicious inputs

Identified using aisentry (https://aisentry.co)
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.

1 participant