Dynamic IP Allowlist Configuration
Important: Understand the Configuration Scenarios First
Before using dynamic allowlist configuration, understand how the allowlist and the limiter interact.
Four Core Questions
1. Only Rate Limiting, No Allowlist
Question: if /internal has a limiter but no allowlist, what happens?
Answer: all IPs can access the route, and all requests are rate limited.
Effect: no allowlist means all IPs are allowed, but they still pass through the limiter.
2. Only Allowlist, No Rate Limiting
Question: if /internal has an allowlist but no limiter, what happens?
Answer: non-allowlisted IPs receive 403 Forbidden; allowlisted IPs have unlimited access. This is not recommended.
Effect:
- Non-allowlisted IP ->
403 Forbidden - Allowlisted IP -> unrestricted access because there is no limiter
3. Both Allowlist and Rate Limiting
Question: if /internal has both an allowlist and a limiter, what happens?
Answer: this is the recommended two-layer setup.
Effect:
- Non-allowlisted IP ->
403 Forbidden - Allowlisted IP, requests 1-500 ->
200 OK - Allowlisted IP, request 501+ ->
429 Too Many Requests
4. Global Allowlist
Question: can an allowlist be global?
Answer: yes, and it is often useful.
Effect:
- Globally allowlisted IPs can pass allowlist validation on every route.
- They still go through each route's rate limit.
Priority: global allowlist > route allowlist.
See IP Allowlist Configuration Scenarios for the full matrix.
Quick Start
Express
Koa
Egg.js
See examples/egg-ip-whitelist-advanced.js for a full Egg.js project-style example.
Configuration Methods
Method 1: Environment Variables
This is usually the best production option because it keeps runtime configuration outside source files.
Method 2: Configuration File
Create config/ip-whitelist.json:
The application can load this file automatically when it exists.
Method 3: Code Configuration
Runtime Management API
View Current Configuration
Response:
Add a Global Allowlist Entry
Response:
Remove a Global Allowlist Entry
Add a Route Allowlist Entry
Usage Scenarios
Scenario 1: Admin Console Only Allows Office IPs
Effect:
- Allowlisted IPs can access the admin route.
- Other IPs receive
403 Forbidden. - Add a limiter on the same route when you also need quota protection.
Scenario 2: Internal API Allows Private Networks
Effect:
- Private addresses such as
10.x.x.x,192.168.x.x, and172.16-31.x.xcan access. - External IPs receive
403 Forbidden.
Scenario 3: Temporarily Authorize a Test IP
Scenario 4: Give VIP Customers Higher Limits
This is dynamic quota selection, not an allowlist bypass. VIP requests are still limited.
Security Recommendations
Protect Management APIs
Management endpoints must require administrator authentication.
Protect Configuration Files
Protect Environment Variables
Use your platform's secret or configuration mechanism where appropriate, such as Kubernetes Secrets, AWS Secrets Manager, or deployment-time environment variables.
Review Regularly
Review allowlist entries at least monthly and remove IPs that are no longer needed.
Related Documents
- Configuration scenarios: IP Allowlist Configuration Scenarios
- Independence model: Allowlist and Rate Limit Independence
- Advanced usage: Advanced Usage
- Basic example:
examples/ip-whitelist-example.js - Express advanced example:
examples/express-ip-whitelist-advanced.js - Koa advanced example:
examples/koa-ip-whitelist-advanced.js - Egg.js advanced example:
examples/egg-ip-whitelist-advanced.js
FAQ
What is the priority between global and route allowlists?
The global allowlist has higher priority. If an IP is globally allowlisted, it passes allowlist validation for every route.
How do I test whether the allowlist works?
If the IP is not allowlisted, the route should return 403 Forbidden.
Is IPv6 supported?
Yes. Configure IPv6 addresses the same way:
How do I use CIDR notation?
Are dynamically added entries persisted?
No. Runtime entries usually live in memory and are lost after restart unless you implement persistence.
Persist entries by writing them to a configuration file, environment-managed config source, or database.