| name | missing-rate-limiting-anti-pattern |
| description | Security anti-pattern for missing rate limiting (CWE-770). Use when generating or reviewing API endpoints, authentication systems, or public-facing services. Detects absence of request throttling enabling brute force, credential stuffing, and DoS attacks. |
Missing Rate Limiting Anti-Pattern
Severity: High
Summary
Applications fail to restrict action frequency, allowing unlimited requests to endpoints. Enables brute-force attacks, data scraping, and denial-of-service through resource-intensive requests.
The Anti-Pattern
The anti-pattern is exposing endpoints (especially authentication/resource-intensive) without controlling request frequency per user or IP.
BAD Code Example
from flask import request, jsonify
@app.route("/api/login", methods=["POST"])
def login():
username = request.form.get("username")
password = request.form.get("password")
if check_credentials(username, password):
return jsonify({"status": "success", "token": generate_token(username)})
else:
return jsonify({"status": "failed"}), 401
@app.route("/api/search")
def search():
query = request.args.get("q")
results = perform_complex_search(query)
return jsonify(results)
GOOD Code Example
from flask import request, jsonify
from redis import Redis
from functools import wraps
redis = Redis()
def rate_limit(limit, per, scope_func):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
key = f"rate-limit:{scope_func(request)}:{request.endpoint}"
p = redis.pipeline()
p.incr(key)
p.expire(key, per)
count = p.execute()[0]
if count > limit:
return jsonify({"error": "Rate limit exceeded"}), 429
return f(*args, **kwargs)
return decorated_function
return decorator
def get_ip(request):
return request.remote_addr
@app.route("/api/login", methods=["POST"])
@rate_limit(limit=10, per=*, scope_func=get_ip)
():
():
Detection
- Review public endpoints: Examine all endpoints that can be accessed without authentication. Do they have rate limiting?
- Check authentication endpoints: Specifically look at login, password reset, and registration endpoints. These are prime targets for brute-force attacks if not rate-limited.
- Analyze API design: For public APIs, check if there is a documented rate-limiting policy (e.g., in the API documentation).
- Perform testing: Write a simple script to hit a single endpoint in a tight loop. If you don't receive a
429 Too Many Requests status code after a certain number of attempts, the endpoint is likely missing rate limiting.
Prevention
Related Security Patterns & Anti-Patterns
References