Skip to content

Latest commit

Β 

History

History
475 lines (357 loc) Β· 11.7 KB

File metadata and controls

475 lines (357 loc) Β· 11.7 KB

LIVE SESSION: A2 Rate Limiting Implementation

Session Start: November 3, 2025 - 14:30 UTC Task: A2 - Rate Limiting Verification Estimated Duration: 3-4 hours Target Completion: November 6, 2025


🎬 Session Overview

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:

  1. RateLimitManager class integrated into api_server.py
  2. @rate_limit decorator applied to all vulnerable endpoints
  3. Unit tests created and passing
  4. Brute force protection verified
  5. Configuration documented

🚦 Current Status: READY TO START

Pre-Implementation Checklist βœ…

  • Security audit completed (A1)
  • Implementation guide created (35+ examples)
  • Code templates prepared
  • Test examples provided
  • Configuration templates ready
  • Reference documentation ready

Next Action

β†’ Copy RateLimitManager class to api_server.py


πŸ“ Implementation Roadmap

Phase 1: Code Integration (Est. 1-1.5 hrs)

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

Phase 2: Endpoint Protection (Est. 1-1.5 hrs)

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

Phase 3: Testing (Est. 1-1.5 hrs)

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

Phase 4: Documentation (Est. 0.5 hrs)

Step 13: Update ultron_config.json
Step 14: Document configuration
Step 15: Add comments to code
Step 16: Update API docs
Time: ~30 minutes

πŸ“‹ Detailed Step-by-Step Guide

STEP 1: Add Import Statements ⏳

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 defaultdict

Verification:

python -c "from threading import Lock, RLock; from time import time; from collections import defaultdict; print('βœ… Imports OK')"

STEP 2: Copy RateLimitManager Class ⏳

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)

STEP 3: Copy @rate_limit Decorator ⏳

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 decorator

STEP 4: Initialize Global Rate Limiter ⏳

Location: 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}')"

STEP 5: Apply to /command Endpoint ⏳

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


STEP 6: Apply to All Vulnerable Endpoints ⏳

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.py

Apply rate limiting to:

  1. /api/tools/execute - @rate_limit(calls=50, period=3600)
  2. /api/tools/reload - @rate_limit(calls=20, period=3600)
  3. /api/model/switch - @rate_limit(calls=20, period=3600)
  4. /api/memory/clear - @rate_limit(calls=20, period=3600)
  5. Any other POST/PUT/DELETE endpoints

GET endpoints (if any):

  • @rate_limit(calls=100, period=3600) - More permissive

STEP 7: Syntax Verification ⏳

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).


STEP 8: Unit Testing ⏳

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 -v

Expected result: All tests pass βœ…


STEP 9: Brute Force Testing ⏳

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 -s

Expected result: ~50 requests succeed (200), ~50 blocked (429) βœ…


STEP 10: Performance Verification ⏳

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 -s

Expected result: Average response time < 50ms βœ…


STEP 11: Update Configuration ⏳

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}
  }
}

STEP 12: Documentation ⏳

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 IP

Update 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`

πŸ“Š Progress Dashboard

Completed βœ…

  • Security audit (A1)
  • Implementation guide created
  • Task documentation prepared

In Progress πŸ”„

  • 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

Pending ⏳

  • Code review
  • Staging deployment
  • 24-hour monitoring
  • Production deployment
  • A3: Input Validation (Nov 6-8)

πŸ§ͺ Test Results Tracking

Unit Tests

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 βœ…

Integration Tests

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 βœ…

πŸ’‘ Key Implementation Notes

  1. Token Bucket Algorithm: Refills calls tokens every period seconds
  2. Per-IP Tracking: Uses request.remote_addr for client identification
  3. Thread Safety: Uses RLock for concurrent request handling
  4. HTTP Status: Returns 429 Too Many Requests when limit exceeded
  5. Logging: All limit violations logged for monitoring
  6. Configuration: Per-endpoint overrides possible via ultron_config.json

πŸ“ž Support References

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)