Integration · Hono

CORS in Hono: setting up the built-in cors()

Hono is a common router choice for Cloudflare Workers projects, and unlike Express or Fastify it doesn't need a separate CORS package: cors() ships inside the framework itself, under hono/cors. That makes it one line to add and easy to assume is also one line to get right. We installed Hono 4.13.18, ran its middleware against real preflight and actual requests, and found a combination of defaults that a browser will reject outright, with no warning from the middleware that it let you ship it.

· ~7 min read

What cors() sends with no options at all

We wired up a minimal app and called the middleware with nothing:

import { Hono } from 'hono'
import { cors } from 'hono/cors'

const app = new Hono()
app.use('/api/*', cors())
app.get('/api/ping', (c) => c.json({ ok: true }))

export default app

Sending a preflight and then the real request against that app (we drove both through Hono's own request helper, which exercises the exact same fetch-handler code a Worker would run) gives this:

# OPTIONS /api/ping, Origin: https://example.com
HTTP/204
access-control-allow-methods: GET,HEAD,PUT,POST,DELETE,PATCH,QUERY
access-control-allow-origin: *

# GET /api/ping, Origin: https://example.com
HTTP/200
access-control-allow-origin: *
content-type: application/json

{"ok":true}

Two things worth noticing. First, allowMethods defaults to a seven-method list that includes QUERY, the newer HTTP method some frameworks are starting to support alongside GET, rather than mirroring whatever route methods you defined. Second, the preflight carries no Access-Control-Max-Age at all unless you set maxAge yourself, so the browser falls back to its own default preflight cache duration instead of whatever you might assume the framework picked for you.

The combination it lets you ship: wildcard origin plus credentials

Most real APIs restrict origin and often need cookies to go with it, so we tried the config that intuitively reads as "lock this down": just turn credentials on.

app.use('/api/*', cors({ credentials: true }))

Nothing else changed, origin was left at its default. The response:

# GET /api/ping, Origin: https://app.example.com
HTTP/200
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json

That's Access-Control-Allow-Origin: * sitting next to Access-Control-Allow-Credentials: true on the same response. The Fetch spec forbids exactly that pairing on a credentialed request: a browser that sent the request with credentials: 'include' throws the response away the moment it sees a wildcard origin alongside the credentials header, regardless of what the server intended. Hono's middleware doesn't check for this combination or warn about it; it sends whatever the two options tell it to, independently of each other. The fix is to always pass an explicit origin (a string, an array, or a function) whenever credentials: true is set:

app.use('/api/*', cors({
  origin: ['https://app.example.com'],
  credentials: true,
}))

With an explicit allowlist in place, the same request now comes back with the specific origin echoed, not a wildcard, which is what makes the credentialed response valid in the first place. The mechanics of why wildcard and credentials can't mix are covered in more detail in CORS with cookies and credentials.

Allowed headers: set your own list, or let it reflect the request

With an origin allowlist in place but no allowHeaders set, we sent a preflight asking to use two custom headers:

# OPTIONS /api/widgets
# Origin: https://app.example.com
# Access-Control-Request-Headers: x-api-key, content-type
HTTP/204
access-control-allow-headers: x-api-key,content-type
access-control-allow-methods: GET,HEAD,PUT,POST,DELETE,PATCH,QUERY
access-control-allow-origin: https://app.example.com
vary: Origin, Access-Control-Request-Headers

With allowHeaders left empty, the middleware echoes back whatever the browser asked to send in Access-Control-Request-Headers, rather than refusing or defaulting to none. That's convenient for getting a preflight to pass during development, and worth replacing with an explicit list (allowHeaders: ['Content-Type', 'X-Api-Key']) once you know which headers your API needs, so a preflight for a header nothing on your API reads doesn't quietly succeed.

A disallowed origin still gets a 200

Using the restricted config from above (origin: ['https://app.example.com'], credentials: true), we sent the same request from an origin that isn't in the allowlist:

# GET /api/ping, Origin: https://evil.example
HTTP/200
access-control-allow-credentials: true
content-type: application/json
vary: Origin

{"ok":true}

No Access-Control-Allow-Origin header, which is what makes the browser discard the response, but also no 403, no error, and the handler still ran and returned a full response body. Access-Control-Allow-Credentials is even still set, because that header only depends on the credentials option, not on whether the origin matched. Hono's app never refuses the request on the server side; it just withholds the one header the browser actually checks. A non-browser caller, or a browser page that doesn't care about reading the response (an image tag, for instance), gets the response regardless. If the goal is keeping a request from reaching the handler at all for origins you didn't list, that has to be a separate check in your own code, not something cors() does for you.

Through a CORS proxy instead

None of the above applies if there's no backend route to attach cors() to in the first place, which is the usual reason a CORS proxy gets used instead of a Hono app. corsproxy.dev's own Worker sidesteps the wildcard-plus-credentials combination by not having the choice: its Access-Control-Allow-Origin is always * and it never sends Access-Control-Allow-Credentials, which means it's not meant for requests that need cookies or browser-managed auth to reach the target, only for calls that carry their own credentials in the request itself. Where a dedicated backend route makes sense instead of a proxy is covered in when not to use a CORS proxy.