Allowlist and Rate Limit Independence
Core Problem
The allowlist and rate limiting must be two completely independent controls, not a coupled shortcut.
- Allowlist: access control. If an IP is not allowed, return
403 Forbidden. - Rate limiting: request-rate control. If a request exceeds the quota, return
429 Too Many Requests. - Independence: even when an IP is allowed, it still goes through rate limiting.
Quick Reference: Configuration Scenarios
Scenario Matrix
Detailed behavior is documented in IP Allowlist Configuration Scenarios.
Key Points
- No allowlist configuration means all IPs are allowed, not all IPs are denied.
- Allowlisted IPs are still rate limited in the independent design.
- The global allowlist has higher priority than route-level allowlists, but it does not bypass rate limiting.
- The recommended setup for sensitive endpoints is allowlist + rate limiting.
Incorrect Coupled Implementation
Problem Code
Earlier advanced examples mixed allowlist checks into the limiter's skip callback:
Why This Is Wrong
Actual Coupled Behavior
The conclusion is simple: allowlisted IPs are not limited, so the controls are coupled.
Correct Independent Implementation
Core Principle
Use two independent middleware functions:
- The allowlist middleware only verifies the IP.
- The rate-limit middleware only controls request rate.
1. Independent Allowlist Middleware
2. Independent Rate Limit Middleware
The limiter does not inspect the allowlist. All requests that pass authorization are counted.
3. Compose Both Middleware Layers
Actual Independent Behavior
The allowlist only decides whether the IP may enter. It never grants unlimited quota.
Comparison Table
Test Verification
Test 1: Allowlisted IP Is Still Limited
Expected independent result when the limit is 5 requests per minute:
Incorrect coupled result:
Test 2: Non-Allowlisted IP Is Rejected
Expected result:
File Comparison
Usage Recommendations
Public API
All users can access the route, but the route is rate limited.
Admin Console
Only office/admin IPs may access the route, and those IPs are still limited.
Internal API
Only private network ranges may access the route, with a higher quota.
Different Operations with Different Limits
Quick Start
Express Independent Example
Koa Independent Example
Related Documents
- Express independent example:
examples/express-ip-whitelist-independent.js - Koa independent example:
examples/koa-ip-whitelist-independent.js - Coupled examples for comparison:
examples/express-ip-whitelist-advanced.js - Configuration scenarios: IP Allowlist Configuration Scenarios
- Dynamic configuration: Dynamic IP Allowlist Configuration
Summary
Key points:
- The allowlist middleware only verifies IP access.
- The limiter middleware only controls request rate.
- Allowlisted IPs are still rate limited.
- The execution order is allowlist, then rate limiter, then business handler.