Session Start: November 3, 2025 - 14:30 UTC Task: A2 - Rate Limiting Verification Estimated Duration: 3-4 hours Target Completion: November 6, 2025
This session focuses on implementing the @rate_limit decorator from the Security Decorators Implementation Guide to protect ULTRON API endpoints against brute force attacks.
Key Deliverables:
- RateLimitManager class integrated into api_server.py
- @rate_limit decorator applied to all vulnerable endpoints
- Unit tests created and passing
- Brute force protection verified
- Configuration documented
- Security audit completed (A1)
- Implementation guide created (35+ examples)
- Code templates prepared
- Test examples provided
- Configuration templates ready
- Reference documentation ready
β Copy RateLimitManager class to api_server.py
Step 1: Add imports (threading, time, collections)
Step 2: Copy RateLimitManager class
Step 3: Copy @rate_limit decorator
Step 4: Initialize RATE_LIMITER instance
Time: ~15-20 minutes
Step 5: Apply to /command endpoint
Step 6: Apply to /api/tools/* endpoints
Step 7: Apply to /api/model/* endpoints
Step 8: Apply to /api/memory/* endpoints
Time: ~20-30 minutes
Step 9: Create unit tests
Step 10: Run unit test suite
Step 11: Create brute force test
Step 12: Performance validation
Time: ~45-60 minutes
Step 13: Update ultron_config.json
Step 14: Document configuration
Step 15: Add comments to code
Step 16: Update API docs
Time: ~30 minutes
File: c:\Projects\ultron_agent\api_server.py
Location: After line 10 (after existing imports)
Action: Add threading imports
# Add these imports after existing imports
from threading import Lock, RLock
from time import time
from collections import defaultdictVerification:
python -c "from threading import Lock, RLock; from time import time; from collections import defaultdict; print('β
Imports OK')"Source: SECURITY_DECORATORS_IMPLEMENTATION_GUIDE.md (Section: RateLimitManager)
Location: Insert at line 13 in api_server.py (after AGENT_INSTANCE global)
Size: Approximately 80-100 lines
Key implementation details:
- Thread-safe token bucket algorithm
- Per-IP tracking with defaultdict
- Automatic window reset after period
- Per-millisecond precision tracking
# From SECURITY_DECORATORS_IMPLEMENTATION_GUIDE.md
# Copy the complete RateLimitManager class here
class RateLimitManager:
def __init__(self):
self.limits = defaultdict(lambda: {'tokens': 0, 'last_update': time()})
self.lock = RLock()
# ... (rest of implementation from guide)Source: SECURITY_DECORATORS_IMPLEMENTATION_GUIDE.md (Decorator 1)
Location: Insert after RateLimitManager class (around line 100)
Size: Approximately 50-70 lines
Key implementation details:
- Extract client IP from request
- Check rate limit using token bucket
- Return 429 if limit exceeded
- Log all limit violations
def rate_limit(calls: int = 100, period: int = 3600):
"""Decorator to rate limit endpoint access by IP address"""
def decorator(f: Callable) -> Callable:
@wraps(f)
def decorated(*args: Any, **kwargs: Any):
# ... (rest of implementation from guide)
return decorated
return decoratorLocation: After @rate_limit decorator (around line 160) Action: Create global instance
# Initialize rate limiter
RATE_LIMITER = RateLimitManager()Verification:
python -c "from api_server import RATE_LIMITER; print(f'β
Rate limiter initialized: {RATE_LIMITER}')"File: api_server.py
Location: Around line 180 (find @app.route("/command")
Current:
@app.route("/command", methods=["POST"])
@require_auth
def command() -> Tuple[Dict[str, Any], int]:Change to:
@app.route("/command", methods=["POST"])
@require_auth
@rate_limit(calls=50, period=3600)
def command() -> Tuple[Dict[str, Any], int]:Note: Decorator order is important! Must be: route β auth β rate_limit
Find and apply to these endpoints (use grep to find):
# Find all POST/PUT/DELETE routes
grep -n "@app.route.*methods=\[\"POST\"\|PUT\|DELETE" api_server.pyApply rate limiting to:
/api/tools/execute-@rate_limit(calls=50, period=3600)/api/tools/reload-@rate_limit(calls=20, period=3600)/api/model/switch-@rate_limit(calls=20, period=3600)/api/memory/clear-@rate_limit(calls=20, period=3600)- Any other POST/PUT/DELETE endpoints
GET endpoints (if any):
@rate_limit(calls=100, period=3600)- More permissive
Action: Verify no Python syntax errors
python -m py_compile c:\Projects\ultron_agent\api_server.py
echo "β
Syntax OK"If errors, review decorator syntax (check @wraps import, function signature).
Create file: c:\Projects\ultron_agent\tests\test_rate_limiter.py
Copy test examples from SECURITY_DECORATORS_IMPLEMENTATION_GUIDE.md:
import pytest
from api_server import RATE_LIMITER, rate_limit
class TestRateLimiter:
def test_rate_limiter_creation(self):
"""Test RateLimitManager initialization"""
assert RATE_LIMITER is not None
assert RATE_LIMITER.limits is not None
def test_rate_limit_enforcement(self):
"""Test token bucket algorithm"""
# ... (test implementation from guide)
# ... (more tests from guide)Run tests:
cd c:\Projects\ultron_agent
pytest tests/test_rate_limiter.py -vExpected result: All tests pass β
Create file: c:\Projects\ultron_agent\tests/test_brute_force.py
import requests
import concurrent.futures
def test_brute_force_protection():
"""Verify rate limiter blocks brute force attempts"""
url = "http://localhost:5000/command"
headers = {"Authorization": "Bearer test_token"}
# Send 100 concurrent requests
results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
futures = [
executor.submit(
requests.post,
url,
headers=headers,
json={"command": "test"},
timeout=5
)
for _ in range(100)
]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
# Count responses
success_count = sum(1 for r in results if r.status_code == 200)
throttled_count = sum(1 for r in results if r.status_code == 429)
print(f"β
Success: {success_count}, Throttled: {throttled_count}")
assert throttled_count > 0, "Rate limiter should block some requests"Run test:
pytest tests/test_brute_force.py -v -sExpected result: ~50 requests succeed (200), ~50 blocked (429) β
Create file: c:\Projects\ultron_agent\tests/test_rate_limiter_performance.py
import time
import requests
def test_rate_limit_overhead():
"""Verify rate limiting adds < 50ms overhead"""
url = "http://localhost:5000/command"
headers = {"Authorization": "Bearer test_token"}
# Measure 20 requests
times = []
for _ in range(20):
start = time.time()
response = requests.post(url, headers=headers, json={"command": "test"})
elapsed = time.time() - start
times.append(elapsed)
avg_time = sum(times) / len(times)
print(f"β
Average response time: {avg_time*1000:.1f}ms")
assert avg_time < 0.05, f"Overhead too high: {avg_time*1000}ms"Run test:
pytest tests/test_rate_limiter_performance.py -v -sExpected result: Average response time < 50ms β
File: ultron_config.json
Location: Add new section in "api_server" object
"rate_limiting": {
"enabled": true,
"default_limits": {
"GET": {"calls": 100, "period": 3600},
"POST": {"calls": 50, "period": 3600},
"PUT": {"calls": 50, "period": 3600},
"DELETE": {"calls": 20, "period": 3600}
},
"endpoint_specific": {
"/command": {"calls": 50, "period": 3600},
"/api/tools/reload": {"calls": 20, "period": 3600},
"/api/model/switch": {"calls": 20, "period": 3600}
}
}Add comments to api_server.py:
# Rate limiting configuration - protects against brute force attacks
# Per-IP tracking with 50 requests/hour for commands, 100 req/hr for GETs
@rate_limit(calls=50, period=3600) # 50 requests per hour per IPUpdate README.md with section:
## Rate Limiting
The API implements per-IP rate limiting to protect against brute force attacks:
- GET endpoints: 100 requests/hour
- POST endpoints: 50 requests/hour
- DELETE endpoints: 20 requests/hour
Configure in `ultron_config.json` under `api_server.rate_limiting`- Security audit (A1)
- Implementation guide created
- Task documentation prepared
- Import statements added
- RateLimitManager class integrated
- @rate_limit decorator integrated
- Decorators applied to endpoints
- Unit tests created and passing
- Brute force tests passing
- Performance verified
- Configuration documented
- Code review
- Staging deployment
- 24-hour monitoring
- Production deployment
- A3: Input Validation (Nov 6-8)
Test: test_rate_limiter_creation
Status: β³ PENDING
Expected: PASS β
Test: test_rate_limit_enforcement
Status: β³ PENDING
Expected: PASS β
Test: test_window_reset
Status: β³ PENDING
Expected: PASS β
Test: test_concurrent_requests
Status: β³ PENDING
Expected: PASS β
Test: Brute force protection
Status: β³ PENDING
Expected: 50+ requests blocked β
Test: Performance overhead
Status: β³ PENDING
Expected: < 50ms average β
Test: API endpoint rate limiting
Status: β³ PENDING
Expected: All endpoints protected β
- Token Bucket Algorithm: Refills
callstokens everyperiodseconds - Per-IP Tracking: Uses request.remote_addr for client identification
- Thread Safety: Uses RLock for concurrent request handling
- HTTP Status: Returns 429 Too Many Requests when limit exceeded
- Logging: All limit violations logged for monitoring
- Configuration: Per-endpoint overrides possible via ultron_config.json
Implementation Code: SECURITY_DECORATORS_IMPLEMENTATION_GUIDE.md
Audit Report: SECURITY_AUDIT_A1_DECORATOR_AUDIT.md
Task Guide: A2_RATE_LIMITING_IMPLEMENTATION.md
Reference Index: IMPLEMENTATION_RESOURCES_INDEX.md
Session Status: π’ READY TO START Time Estimate: 3-4 hours Priority: π΄ CRITICAL (Brute force protection) Next Review: After Step 8 (unit tests passing)