Cloudflare Workers KV Allowlist: Edge Design and Trust Boundaries
Use a Cloudflare Worker and KV namespace to enforce an edge allowlist, but treat the origin as untrusted toward Worker headers and verify KV propagation before relying on it for access control.
23 Jun 2026, 08:03 UTC

Problem: enforce an allowlist at the edge before origin contact
You need to block requests from disallowed IP addresses or clients without sending every request to your origin. A Cloudflare Worker can inspect the incoming request at the edge and return a 403 before origin infrastructure is involved. The obvious companion is Workers KV, an eventually consistent key-value store replicated to edge locations. That combination looks simple, but it introduces two operational risks: KV updates are not immediate, and an origin cannot assume a Worker-added header is proof of authorization.
Takeaway: Use one Worker route reading one KV namespace to decide allow or deny at the edge. Do not rely on Worker-injected headers for origin authorization unless the origin verifies a signed token or you enforce mTLS. Verify KV propagation delay and exception behavior in your environment before treating the design as production-ready.
Requirements
- Requests are proxied through Cloudflare so the Worker sees them before the origin.
- Allowlist entries are identified by client IP or another request attribute available at the edge.
- Updates to the list can tolerate propagation delays of seconds to minutes.
- The origin must remain reachable only through Cloudflare, or it must independently verify identity.
Smallest suitable design
A single Worker script bound to a route pattern such as example.com/api/*. The Worker reads one KV namespace, checks the request attribute, returns 403 for misses, and forwards hits to the origin unchanged. No header injection is needed for the access decision itself; the Worker enforces the decision at the edge.
Example Worker code
export default {
async fetch(request, env, ctx) {
const clientIp = request.headers.get("cf-connecting-ip");
if (!clientIp) {
return new Response("Missing client IP", { status: 400 });
}
const allowlist = env.ALLOWLIST_KV;
const key = `allow:${clientIp}`;
const value = await allowlist.get(key);
if (value === null) {
return new Response("Forbidden", { status: 403 });
}
return fetch(request);
},
};
Where to run: Deploy with Wrangler from a local terminal or upload through the Cloudflare dashboard.
Required permissions: A Cloudflare API token with Workers edit and KV edit access for the account, plus zone access to attach the route.
Placeholders: Replace ALLOWLIST_KV with your KV namespace binding name in wrangler.toml, and replace example.com/api/* with your actual route.
Expected checks: A request from an IP present in KV returns the origin response. A request from an IP absent from KV returns 403 without contacting the origin.
Risk: A misconfigured KV key or missing binding can block all traffic or allow all traffic, depending on how you handle errors. Wrap the lookup in explicit logic rather than relying on truthy defaults.
wrangler.toml excerpt
name = "edge-allowlist"
main = "src/index.js"
compatibility_date = "2026-01-01"
[[kv_namespaces]]
binding = "ALLOWLIST_KV"
id = ""
[[routes]]
zone = ""
pattern = ""
Where to run: wrangler deploy in the directory containing wrangler.toml.
Required permissions: Same as above; the token must be able to write KV namespace bindings and routes.
Expected checks: wrangler whoami confirms authentication. wrangler deploy reports the route and KV binding.
Risk: Deploying overwrites the previous Worker version. Keep the previous script available if you need to revert quickly.
Trust and data boundaries
The trust boundary is the Cloudflare edge. The Worker runs inside Cloudflare's network before the request reaches your origin. The origin should not treat any header added by the Worker as authoritative proof of the client's allowlist status. An attacker who knows your origin address can send requests directly, bypassing the Worker entirely.
Data boundary: KV stores the allowlist. Because KV is eventually consistent, a key added in one region may not be readable at another edge location immediately. Treat the KV value as advisory for fast-changing lists, not as a strongly consistent authorization decision.
If the origin must trust the edge decision, use Cloudflare Access or mutual TLS (mTLS) between Cloudflare and the origin, or have the Worker sign a short-lived token that the origin verifies. Restrict origin access to Cloudflare IP ranges or use Argo Tunnel so direct origin access is not possible.
Operational checks
- Deploy a test Worker that returns 403 for a known-missing IP and 200 for a known-allowed IP. Inspect origin logs to confirm allowed requests reach the origin and denied requests do not.
- Write a KV key, then request the edge from multiple regions. Measure how long it takes for the new value to be observed. Use this delay as your minimum propagation window before trusting an update.
- Force an exception in the Worker, for example by referencing an undefined binding, and observe whether Cloudflare returns an error page or forwards the request. Define your default behavior explicitly in a catch block.
- Monitor the Cloudflare dashboard for Worker exceptions and CPU time. Confirm traffic remains within your plan's CPU limits.
Failure modes
- KV read latency adds to edge response time. Under heavy read volume, latency may vary by location.
- CPU limits vary by plan. Complex checks, large payloads, or cryptographic operations in the Worker can exceed the per-request budget and cause failures.
- Unhandled exceptions can prevent the request from reaching the origin or return a Cloudflare error. Do not assume fail-open or fail-closed behavior; test it.
- KV eventual consistency means an allowlist removal may not take effect globally for seconds or minutes. A removed client could still be allowed during that window.
- If the origin is publicly reachable, clients can bypass the Worker and the allowlist entirely.
Conditions that would change the design
- Logic becomes stateful or requires per-client sessions: move to Durable Objects.
- Allowlist changes are frequent and must be consistent immediately: query an origin API or use a strongly consistent store instead of KV.
- The origin requires strong identity verification: use Cloudflare Access or mTLS rather than Worker-injected headers.
- You need to inspect or modify request bodies: offload that work to the origin; Workers are not suited for heavy payload processing.
Limitations and verification
This draft is conservative: verify all behavior in a staging environment before production use. The architecture described here is based on well-established Cloudflare Workers and KV behavior, but specific limits such as CPU time and KV propagation delay vary by plan and change over time. Confirm current limits in the Cloudflare dashboard and documentation before deployment.
Practical verification: after deployment, send requests from an allowed IP and a blocked IP. Check origin access logs to confirm the Worker is filtering as expected. Then add a new IP to KV and check from a different geographic location until the IP is allowed, recording the observed delay.
Rollback
Deploying a Worker changes the active route. To roll back, redeploy the previous Worker version from the Cloudflare dashboard or run wrangler deploy with the prior script. Removing the route from wrangler.toml and redeploying detaches the Worker from the pattern and restores direct origin handling.