This CORS Mistake Exposes Your API (I See It Everywhere)
Alejandro

Alejandro @aserrano

About: Full‑stack JavaScript developer focused on modern web technologies and backend performance. Passionate about Node.js, TypeScript, Cloudflare Workers, and building fast, reliable web applications.

Joined:
Dec 25, 2025

This CORS Mistake Exposes Your API (I See It Everywhere)

Publish Date: Dec 27 '25
1 1

Saw this in 70% of Workers I've reviewed:

headers.set('Access-Control-Allow-Origin', '*');
headers.set('Access-Control-Allow-Credentials', 'true');
Enter fullscreen mode Exit fullscreen mode

Looks harmless, right?

It's a security hole.

The Problem

When you use wildcard (*) with credentials, you're telling browsers:

"Allow ANY website to make authenticated requests to my API."

This means malicious sites can:

  • Read user data
  • Make requests on behalf of logged-in users
  • Steal session tokens

The Fix

Option 1: Use specific origins

const allowedOrigins = ['https://yourdomain.com', 'https://app.yourdomain.com'];
const origin = request.headers.get('Origin');

if (allowedOrigins.includes(origin)) {
  headers.set('Access-Control-Allow-Origin', origin);
  headers.set('Access-Control-Allow-Credentials', 'true');
}
Enter fullscreen mode Exit fullscreen mode

Option 2: Don't use credentials

If you don't need cookies/auth headers:

headers.set('Access-Control-Allow-Origin', '*');
// No credentials header at all
Enter fullscreen mode Exit fullscreen mode

The Rule

Wildcard OR credentials. Never both.

Simple as that.

Have you made this mistake? Don't worry—so have I. And pretty much everyone else when they started.

Comments 1 total

  • Emma Watson
    Emma WatsonDec 27, 2025

    My last salary was $8750, ecom only worked 12 hours a week. My longtime neighbor yr estimated $15,000 and works about 20 hours for seven days. I can't believe how blunt he was when I looked up his information. p1

    This is what I do.................W­­­w­­­w­­­.­­­E­­­a­­­r­­­n­­­5­­­4­­­.­­­C­­­o­­­m

Add comment