CORS is one of those features that becomes security-sensitive by accident. Instead of Access-Control-Allow-Origin: *, I keep a strict allowlist and echo back the exact origin only when it’s approved. I also handle OPTIONS preflight requests explicitly and set Vary: Origin so caches don’t mix responses across origins. The important detail is credentials: if you ever set Access-Control-Allow-Credentials: true, you must never use wildcard origins. This middleware keeps the logic in one place and makes it easy to audit. In production, I treat the allowlist as configuration and include both local dev origins and the real domains. With this in place, frontend integration becomes predictable and you avoid accidental cross-site exposure.