🔒 OWASP API Security Top 10

API4:2019 - Lack of Rate Limiting

📋 Overview

Lack of rate limiting is a critical vulnerability that allows endpoints serving data to be called multiple times per second without restriction. This vulnerability becomes particularly dangerous when combined with factors such as data size and request frequency, potentially leading to application-level denial of service (DoS) attacks.

⚠️ Critical Risk Areas

Rate limiting vulnerabilities are especially dangerous on authentication endpoints, where they can enable:

  • Brute Force Attacks: Unlimited password guessing attempts
  • Credential Stuffing: Testing large lists of stolen credentials
  • Application-Level DoS: Resource exhaustion through excessive requests

🎯 Understanding the Vulnerability

When an API is hosted on a server, it requires essential resources including CPU, RAM, network bandwidth, and disk space. The amount of resources consumed is highly dependent on the task being performed. Even lightweight operations can become problematic when executed repeatedly without limitation.

Resource Consumption Factors

Resource Type Impact Attack Vector
CPU High Complex calculations, data processing
Memory (RAM) High Large data retrieval, file operations
Network Bandwidth Medium-High Bulk data transfers, file downloads
Disk Space Medium File creation, logging operations

💡 Real-World Attack Scenarios

Scenario 1: Document Creation Attack

Attack Vector: Exploiting document creation endpoint

POST /api/v1/documents HTTP/1.1 Host: vulnerable-api.com Content-Type: application/json { "content": "Lorem ipsum dolor sit amet...", "fileSize": "50MB", "format": "PDF" }

Attack Process:

Initial request: POST /api/v1/documents with 50MB payload
Repeated requests: Send same POST request 1000 times in rapid succession
Result: Server creates 1000 documents totaling 50GB, exhausting disk space

Document Creation Attack Flow

🔴 Attacker sends POST request
⬇️
📄 Server creates 50MB document
⬇️
🔁 Attacker repeats 1000 times
⬇️
💥 Server disk space exhausted

Scenario 2: Clone Document Vulnerability

Critical Issue: Clone endpoint lacks rate limiting entirely

POST /api/v1/documents/clone HTTP/1.1 Host: vulnerable-api.com Content-Type: application/json { "documentId": "12345", "numberOfCopies": 1 }

Exploitation Method:

Step 1: Create large document (50MB) using POST /api/v1/documents
Step 2: Send clone request: POST /api/v1/documents/clone with documentId
Step 3: Repeat clone requests continuously without restriction
Step 4: Server creates unlimited clones, consuming all available resources

Scenario 3: Parameter Manipulation Attack

High Risk: Manipulating limit parameters to exhaust resources

GET /api/v1/posts?limit=100 HTTP/1.1 Host: vulnerable-api.com

Normal behavior: Returns 100 posts per request

GET /api/v1/posts?limit=999999 HTTP/1.1 Host: vulnerable-api.com

Malicious behavior: Attempts to return 999,999 posts in one request

Attack command: curl -X GET "https://vulnerable-api.com/api/v1/posts?limit=999999"
Repeated attack: for i in {1..1000}; do curl -X GET "https://vulnerable-api.com/api/v1/posts?limit=999999" & done
Result: 1000 concurrent requests each retrieving 999,999 records = Total system failure

Parameter Manipulation Attack Visualization

👤 Normal user: limit=100
⬇️
✅ Server returns 100 posts (acceptable load)

🔴 Attacker: limit=999999
⬇️
⚠️ Server attempts to process 999,999 posts
⬇️
🔁 Repeated 1000 times simultaneously
⬇️
💥 Complete resource exhaustion

🛡️ Prevention and Mitigation Strategies

1. Docker Container Resource Limits

Docker containers provide built-in mechanisms to limit resource consumption, preventing any single container from exhausting server resources.

# Limit container memory usage docker run -m 512m my-api-container # Limit CPU usage (50% of one core) docker run --cpus="0.5" my-api-container # Combined resource limits docker run -m 512m --cpus="0.5" --memory-swap 1g my-api-container
docker run -m 512m --cpus="0.5" --memory-swap 1g --name api-container my-api-image

✅ Benefits of Docker Resource Limits

  • Prevents resource exhaustion at the container level
  • Easy separation of different API services
  • Automatic resource cleanup when limits are exceeded
  • Improved overall system stability

2. Request Rate Limiting Implementation

Implement strict limits on the number of requests a client can make within a specific time period.

// Example: Node.js with Express Rate Limiter const rateLimit = require('express-rate-limit'); const limiter = rateLimit({ windowMs: 1000, // 1 second max: 5, // 5 requests per second message: 'Too many requests. Please try again later.', standardHeaders: true, legacyHeaders: false, }); app.use('/api/', limiter);

Recommended Rate Limits by Endpoint Type

  • Authentication endpoints: 5 requests per minute
  • Data retrieval: 60 requests per minute
  • Data creation: 30 requests per minute
  • File operations: 10 requests per minute

3. Clear Rate Limit Messages

When rate limits are triggered, provide clear, informative messages to users explaining the restriction.

HTTP/1.1 429 Too Many Requests Content-Type: application/json X-RateLimit-Limit: 5 X-RateLimit-Remaining: 0 X-RateLimit-Reset: 1678901234 { "error": "Rate limit exceeded", "message": "You have exceeded the maximum of 5 requests per second. Please wait before trying again.", "retryAfter": 60 }
Example response header: X-RateLimit-Limit: 5
Example response header: X-RateLimit-Remaining: 0
Example response header: X-RateLimit-Reset: 1678901234

4. Maximum Size Input Validation

Every endpoint that accepts size parameters must enforce strict upper limits to prevent abuse.

// Example: Input validation for limit parameter function validateLimitParameter(limit) { const MAX_LIMIT = 1000; const parsedLimit = parseInt(limit); if (isNaN(parsedLimit) || parsedLimit < 1) { return { valid: false, error: 'Invalid limit value' }; } if (parsedLimit> MAX_LIMIT) { return { valid: false, error: `Limit cannot exceed ${MAX_LIMIT}` }; } return { valid: true, value: parsedLimit }; }
Implementation: if (requestedLimit > MAX_LIMIT) { return error(400, "Limit exceeds maximum allowed value") }

Request Validation Flow

📥 Request received: limit=999999
⬇️
🔍 Validation check: Is limit > 1000?
⬇️
❌ Validation failed: limit exceeds maximum
⬇️
✅ Return 400 error: "Limit cannot exceed 1000"

5. Comprehensive Rate Limiting Strategy

Multi-Layer Protection

  • Per-endpoint rate limits: Different limits for different operations
  • Per-user rate limits: Track requests per authenticated user
  • Per-IP rate limits: Prevent single-source attacks
  • Global rate limits: Overall system protection
// Multi-layer rate limiting example const endpointLimits = { '/api/v1/auth/login': { requests: 5, window: 60000 }, // 5 per minute '/api/v1/documents': { requests: 30, window: 60000 }, // 30 per minute '/api/v1/documents/clone': { requests: 10, window: 60000 }, // 10 per minute '/api/v1/posts': { requests: 60, window: 60000 } // 60 per minute };

🔧 Testing for Rate Limiting Vulnerabilities

Manual Testing Commands

Test basic rate limiting: for i in {1..20}; do curl -X GET "https://api.example.com/endpoint"; done
Test authentication rate limiting: for i in {1..50}; do curl -X POST "https://api.example.com/auth/login" -d '{"username":"test","password":"wrong"}'; done
Test with parameter manipulation: curl -X GET "https://api.example.com/api/v1/posts?limit=999999"
Load testing with concurrent requests: ab -n 1000 -c 100 https://api.example.com/endpoint

Automated Testing Tools

Tool Purpose Command Example
Apache Bench Load testing ab -n 1000 -c 50 URL
curl Manual testing curl -X GET URL
Burp Suite Intruder attacks GUI-based testing
OWASP ZAP Automated scanning GUI-based testing

🎯 Key Takeaway

Every single API endpoint MUST have rate limiting implemented.

This is non-negotiable for API security. Pay special attention to authentication endpoints, as they are the most critical targets for brute force attacks.

📚 Summary Checklist

  • ✅ Implement rate limiting on ALL API endpoints
  • ✅ Use Docker container resource limits for isolation
  • ✅ Enforce maximum size limits on all input parameters
  • ✅ Provide clear rate limit messages to users
  • ✅ Implement multi-layer rate limiting (per-user, per-IP, per-endpoint)
  • ✅ Extra security on authentication endpoints
  • ✅ Regular testing and monitoring of rate limits
  • ✅ Log rate limit violations for security monitoring

⚠️ Common Pitfalls to Avoid

  • Assuming low-impact operations don't need rate limiting
  • Forgetting to rate limit clone or copy operations
  • Not validating maximum values for size parameters
  • Inconsistent rate limiting across similar endpoints
  • Failing to monitor and log rate limit violations