Cloudflare Workers Cors Proxy

Published
Length
4 min read

Luis Felipe Dias

All posts

You set up a cloudflare workers cors proxy by deploying an edge script that adds origin headers to responses. This script takes browser requests, sends them to an upstream API, and returns the data with new headers. Teams build this setup when a web app must read data from a third-party domain. Still, broad origin rules often expose backend APIs to unwanted web traffic and abuse. How a cloudflare workers cors proxy works at the edge Web browsers enforce the same-origin rule to protect user da

Database Proxy (ProxySQL, PgBouncer) + CORS Configuration: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

You set up a cloudflare workers cors proxy by deploying an edge script that adds origin headers to responses. This script takes browser requests, sends them to an upstream API, and returns the data with new headers. Teams build this setup when a web app must read data from a third-party domain. Still, broad origin rules often expose backend APIs to unwanted web traffic and abuse.

How a cloudflare workers cors proxy works at the edge

Web browsers enforce the same-origin rule to protect user data across sites. A browser blocks a frontend app from reading responses from a different domain by default. The remote server must send back exact origin headers to allow this cross-site read. Many external APIs and older internal backends do not send these headers at all. Teams place an edge worker between the browser and the remote service to fix this.

The workflow starts when the browser sends a preflight check before the main request. This check asks the edge script which origins, methods, and headers the server accepts. The worker reads this check and replies right away with an approved list of rules. It does not need to wake up the backend server for this first step.

Next, the browser sends the real API call to the edge worker address. The worker strips out browser metadata that might confuse the destination service. It then fetches the data from the target API on behalf of the user. When the target API replies, the worker copies the response payload into a new object. It attaches the required cross-origin headers to this new response and sends it back. The browser sees the matching origin headers and lets the frontend code read the payload.

What edge header rewriting solves and where it falls short

An edge worker solves the immediate browser block without changing the backend server code. Frontend developers can test and ship features fast without waiting for platform teams to update legacy systems. It also keeps traffic fast because the script runs on edge nodes close to the user. Teams can maintain an allowlist of approved web domains right inside the worker logic.

Yet this setup only controls which web domains can read responses in a browser. It does not check who the user is or what the request actually does in flight. Many developers start by setting a wildcard rule that lets any website call the worker. Attackers can abuse an open worker to scrape data or hide their true network address.

Database Proxy (ProxySQL, PgBouncer) + CORS Configuration: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Even with a strict domain list, origin headers are easy to fake outside a browser. Anyone can use a command line tool to send fake origin values to the worker. If the worker holds a shared API key to call the backend, every caller gets that same level of power. The edge script passes valid requests straight through without inspecting the payload for sensitive data. If a backend response includes emails, phone numbers, or card numbers, the worker returns all of it. Teams lack a clear audit log that ties each backend action to a specific team member.

Best practices for locking down cross-origin edge scripts

Teams that run an edge script should lock down how it handles incoming traffic. First, replace any wildcard origin rules with a strict list of known frontend domains. The script should check the incoming origin against this list on every single request. If the origin does not match, the worker should drop the request right away.

Second, restrict the HTTP methods the worker will forward to the backend service. If the web app only needs to read data, block all write and delete calls at the edge. Third, never route internal admin tools or database interfaces through a public header script. Keep public browser work separate from how engineers and internal services interact with core systems.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Why does my browser still block requests when the edge script is active?

The browser is likely failing on the preflight check before the main request runs. Make sure your script replies to options calls with the exact headers and methods your app sends. Also check that error responses from the backend still get the cross-origin headers attached.

Is it safe to put a wildcard origin rule on an edge worker?

A wildcard rule is only safe for public data that requires no login or secret keys. Never use a wildcard if the request includes user cookies, session tokens, or private backend keys. Any outside website could use that open rule to read private data through a visitor's browser.

Can an origin header check replace real user login checks?

No, an origin header only tells you what website a browser is currently displaying. It does not prove who the user is, and command line tools can fake any header value. Always verify user tokens and permissions on the backend before returning private data.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.