23 - IP Security

Overview

OpenAlgo implements IP-based security measures to protect against brute-force attacks, bot abuse, and unauthorized access through automatic detection and banning mechanisms.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                          IP Security Architecture                            │
└──────────────────────────────────────────────────────────────────────────────┘

                             Incoming Request


┌─────────────────────────────────────────────────────────────────────────────┐
│                        Security Middleware                                   │
│                        (WSGI Layer)                                          │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  1. Get Real IP (check proxy headers)                                │   │
│  │     CF-Connecting-IP → X-Real-IP → X-Forwarded-For → remote_addr    │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  2. Check IP Ban List                                                │   │
│  │     - Is IP in ip_bans table?                                        │   │
│  │     - Is ban expired?                                                │   │
│  │     - Is ban permanent?                                              │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│              ┌─────────────────────┴─────────────────────┐                  │
│              │                                           │                   │
│           Banned                                    Not Banned               │
│              │                                           │                   │
│              ▼                                           ▼                   │
│         Return 403                               Continue to App            │
│         Forbidden                                                            │
└─────────────────────────────────────────────────────────────────────────────┘

Detection Mechanisms

1. 404 Error Tracking

Detects bots probing for vulnerabilities.

2. Invalid API Key Tracking

Detects brute-force API key attacks.

Configuration

Security Thresholds

Database Schema

ip_bans Table

error_404_tracker Table

IP Resolution

Proxy Header Priority

Security Middleware

WSGI Implementation

Route Decorator

Admin Interface

Security Dashboard

Route: /logs/security

Manual Ban/Unban

Repeat Offender Escalation

Best Practices

Rate Limiting Integration

IP bans work alongside rate limiting:

Whitelisting

For trusted IPs:

Key Files Reference

File
Purpose

utils/security_middleware.py

WSGI middleware

utils/ip_helper.py

IP resolution

database/traffic_db.py

Ban tables

blueprints/security.py

Security dashboard and routes

Last updated