Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 4 min read

Our CORS policy is one function that reflects an origin or says nothing, so I probed the live API with nine of them

Munchable has three kinds of client for its API. The web app is same origin, so CORS never applies to it. The native Expo app has no browser at all, so CORS never applies to that either. That leaves one cross-origin call

Munchable has three kinds of client for its API. The web app is same origin, so CORS never applies to it. The native Expo app has no browser at all, so CORS never applies to that either. That leaves one cross-origin caller: the Expo app running in a browser during development, which serves from localhost:8081 and talks to the API on another port.

Route handlers originally emitted no CORS headers, so every preflight from that setup failed. The fix is not in any route. It is one module, lib/cors.ts, applied from the request proxy so that no handler has to remember it. The whole decision is a function that takes an origin and returns a boolean:

export function isAllowedOrigin(origin: string | null): origin is string {
  const url = parseUrl(origin ?? undefined);
  if (!url) return false; // absent, malformed, or the opaque "null" origin
  if (url.origin === SITE.origin || EXTRA_ORIGINS.has(url.origin)) return true;
  if (
    url.protocol === 'https:' &&
    (url.hostname === SITE.hostname || url.hostname.endsWith(`.${SITE.hostname}`))
  ) {
    return true;
  }
  return !IS_PRODUCTION && LOOPBACK_ORIGIN.test(url.origin);
}

If the origin is allowed, the response reflects it back in Access-Control-Allow-Origin. If not, the response carries no CORS headers at all and the browser does the refusing. There is no wildcard, and Access-Control-Allow-Credentials is never sent. The mobile client authenticates with a bearer token, which crosses origins fine without credentials mode, while the cookie-based web session can never be replayed from another origin because browsers will not attach cookies to a cross-origin request unless the server opts in.

Nine origins against the live site

A policy like this is easy to read and easy to get subtly wrong, so I stopped reading it and sent origins at production instead. Anyone can repeat this with curl. The path does not need to exist, because the policy is applied before routing:

curl -s -o /dev/null -D- -H "Origin: https://app.munchable.app" https://munchable.app/api/hello

Here is what came back for each origin, looking only at access-control-allow-origin:

Origin sent Reflected back?
https://munchable.app yes
https://www.munchable.app yes
https://app.munchable.app yes
https://munchable.app.evil.example no
https://evilmunchable.app no
http://munchable.app no
http://localhost:8081 no
null no
https://evil.example no

The two near misses are the ones worth having a test for. munchable.app.evil.example starts with our hostname, and evilmunchable.app ends with it, so a policy written with startsWith or a bare endsWith(hostname) would let one of them in. The code compares to the exact hostname or to . plus the hostname, which is why only genuine subdomains pass. The http:// origin is refused for a different reason: the site origin is https://, and a plain-HTTP page on our host cannot exist anyway because the whole .app TLD is preloaded for HTTPS.

The localhost row is the one I like. The development origin that motivated the whole file is refused in production, because the loopback clause is gated on NODE_ENV. The same module that makes Expo web work on a laptop does nothing for anyone on the internet.

The preflight reflects headers, on purpose

A preflight is the browser asking permission before it sends the real request. I sent one from an allowed origin and asked for a header the API has never heard of:

curl -s -o /dev/null -D- -X OPTIONS \
  -H "Origin: https://app.munchable.app" \
  -H "Access-Control-Request-Method: POST" \
  -H "Access-Control-Request-Headers: authorization,content-type,x-foo" \
  https://munchable.app/api/hello

The answer was 204 with access-control-allow-headers: authorization,content-type,x-foo, an access-control-max-age of 86400, and vary: Origin, Access-Control-Request-Headers. So the server echoes whatever headers were requested, falling back to Authorization, Content-Type when none were. That is looser than a fixed list, and it is a deliberate trade: the origin check is the real gate, and an allowed origin is already trusted with bearer tokens. A header allow-list would only matter for an origin we had decided to trust, and then break the next client that adds a tracing header. The same request from https://evil.example got a 204 with no allow headers at all, which a browser reads as a refusal.

Two smaller details that are easy to miss:

  • Vary: Origin is appended on every API response, allowed or not. The response differs per origin, so a shared cache has to key on it, or the first allowed origin's header would be served to the next caller.
  • Exposed headers are a list, not *. A browser client can read the rate limit headers, Retry-After, ETag and our taxonomy version stamp off a response, and nothing else.

The platform has its own opinion about pages

One result surprised me. The same Origin header sent to a page instead of the API:

curl -s -o /dev/null -D- -H "Origin: https://app.munchable.app" https://munchable.app/about

comes back with access-control-allow-origin: *. That is not our code. Prerendered static pages are served by the platform's CDN, which adds a wildcard for static files. It is harmless for a public page that anyone can already fetch, and it is a useful reminder that "what does my CORS policy say" has two answers on one hostname: the one in your repository for dynamic routes, and the one your host applies to everything it serves as a file. You can look at the page I used, the about page, and at the home page it belongs to.

What I took from it

The policy is a dozen lines of logic and nine rows of evidence, and the rows are the better documentation. The repository has no test for it, and if I added one it would be that table: a list of origins and an expected boolean, with the two lookalike hostnames and the loopback origin in production mode as the first three entries.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.