Skip to main content
Codex-LB includes a built-in firewall system that restricts API access based on client IP addresses. When enabled, only requests from allowed IPs can access the proxy endpoints (/v1/* and /backend-api/codex/*).

How It Works

The firewall operates in two modes:

Allow All

Default mode. No restrictions—any IP can access the API.Active when the firewall allowlist is empty.

Allowlist Active

Restricted mode. Only IPs in the allowlist can access protected endpoints.Activated automatically when you add at least one IP to the allowlist.

Protected Endpoints

The firewall only protects API proxy endpoints:
  • /v1 and /v1/* (OpenAI-compatible API)
  • /backend-api/codex and /backend-api/codex/* (Codex CLI API)
Unprotected endpoints (always accessible):
  • /api/* (dashboard API)
  • /auth/* (OAuth callbacks)
  • / (dashboard frontend)
This ensures you can always access the dashboard to manage the firewall, even if your IP is blocked.

Configuration

Environment Variables

boolean
default:"false"
Trust X-Forwarded-For headers for client IP detection.
Only enable this when Codex-LB is behind a trusted reverse proxy (nginx, Caddy, Cloudflare, etc.).If enabled without a trusted proxy, clients can spoof their IP by setting the X-Forwarded-For header, completely bypassing the firewall.
When to enable:
  • Codex-LB is behind nginx/Caddy/HAProxy/Traefik
  • Codex-LB is behind Cloudflare or another CDN
  • You need to see real client IPs in logs and firewall checks
When to keep disabled:
  • Codex-LB is directly exposed to the internet
  • You don’t use a reverse proxy
string
default:"127.0.0.1/32,::1/128"
Comma-separated list of CIDR ranges for trusted proxy sources.Only requests from these IPs will have their X-Forwarded-For headers trusted. This prevents malicious clients from spoofing IPs.Examples:
See Cloudflare IP Ranges for the full list.

Managing the Allowlist

You can manage the IP allowlist through the dashboard or API.

Dashboard

  1. Navigate to Settings → Firewall
  2. View current mode and allowed IPs
  3. Add or remove IPs as needed

API

Response:
Response:
IPs are automatically normalized. IPv6 addresses like ::ffff:127.0.0.1 are converted to 127.0.0.1.
Response:

IP Detection Logic

Codex-LB resolves the client IP using the following logic:

Without Proxy Headers Trust

With Proxy Headers Trust

This prevents IP spoofing by only trusting X-Forwarded-For when the direct connection comes from a trusted proxy.

Deployment Examples

Direct Exposure (No Proxy)

Add client IPs directly:

Behind nginx (Local)

Add real client IPs (not nginx’s IP):

Behind Cloudflare

Codex-LB will use the real client IP from Cloudflare’s X-Forwarded-For header.

Docker Compose with nginx

Common Scenarios

Now only requests from 203.0.113.42 can access the API.
This restricts API access to local processes only.

Error Messages

IP Forbidden

Cause: Client IP is not in the allowlist. Solution: Add the client’s IP to the allowlist or disable the firewall.

Invalid IP Address

Cause: The provided IP address is not valid. Solution: Verify the IP format (IPv4: 203.0.113.42, IPv6: 2001:db8::1).

IP Already Exists

Cause: The IP is already in the allowlist. Solution: No action needed. The IP is already allowed.

Security Best Practices

Use with API Keys

Combine firewall with API key authentication for defense in depth.

Limit Trusted Proxies

Only add CIDRs you control to FIREWALL_TRUSTED_PROXY_CIDRS.

Regular Audits

Periodically review the allowlist and remove stale IPs.

Monitor Logs

Watch for ip_forbidden errors to detect unauthorized access attempts.

Firewall vs Dashboard Auth

Recommended setup for production:
  1. Enable firewall to restrict API access by IP
  2. Enable dashboard authentication (password + TOTP)
  3. Use API keys for programmatic access
  4. Run behind HTTPS reverse proxy
This provides multiple layers of security.

Troubleshooting

Dashboard Accessible, API Blocked

Expected behavior. The dashboard (/api/*) is not protected by the firewall. Add your IP to access API endpoints:

Real IP Not Detected Behind Proxy

Verify:
  1. FIREWALL_TRUST_PROXY_HEADERS=true
  2. Proxy IP is in FIREWALL_TRUSTED_PROXY_CIDRS
  3. Proxy is sending X-Forwarded-For header
Debug:

Locked Out After Enabling Firewall

You can still access the dashboard at http://localhost:2455 (firewall doesn’t protect dashboard routes).
  1. Go to Settings → Firewall
  2. Remove all IPs to disable allowlist mode
  3. Or add your current IP
If dashboard auth is also locked, access the database directly: