PortGate

Resources · Security guides · Security guide

Session cookies: flags, CSRF tokens, and why CORS is not authentication

Set Secure, HttpOnly and SameSite on session cookies, keep CSRF protection on every state-changing request, and do not treat CORS as an access control.

Check command

curl -sI https://example.com/login | grep -i '^set-cookie'

Copied.

Three settings decide most of what happens when someone tries to ride a logged-in user's session: the cookie flags, CSRF protection, and an accurate understanding of what CORS does.

Cookie flags

Check what your login page sets:

curl -sI https://example.com/login | grep -i '^set-cookie'

A session cookie should carry:

  • Secure: only sent over HTTPS, never over plain HTTP where it can be read in transit.
  • HttpOnly: JavaScript on the page cannot read it, so an XSS bug cannot copy the session out.
  • SameSite=Lax (or Strict): the browser does not attach it to cross-site POST requests or embedded sub-requests.

In Laravel, config/session.php controls these. For production:

SESSION_SECURE_COOKIE=true
SESSION_SAME_SITE=lax

http_only is on by default. Clear and rebuild the config cache after changing them.

CSRF protection still matters

SameSite=Lax blocks most cross-site form posts, but not all: older browsers, same-site subdomains you do not fully control, and top-level GET requests that change state. Keep the framework's CSRF token check on every POST, PUT, PATCH and DELETE that uses the session cookie. In Laravel that is the default; exclude a route only when it is authenticated another way, such as a signed webhook.

Never change state on a GET request. A link, an image tag or a prefetch can trigger it.

CORS is not authentication

CORS headers tell a browser whether a page from another origin may read your response. They do not stop the request from reaching your server, and they do nothing at all against curl, a script or a server-side caller. A policy of Access-Control-Allow-Origin: https://app.example.com does not protect an endpoint that has no authentication.

Two rules follow:

  1. Every API endpoint checks authentication and authorization itself.
  2. Never combine Access-Control-Allow-Credentials: true with an origin you reflect from the request. That lets any website read authenticated responses as the logged-in user.

Quick review

  • Session cookie has Secure, HttpOnly and SameSite=Lax.
  • CSRF tokens are checked on every state-changing request that uses cookies.
  • No GET endpoint changes data.
  • CORS lists exact origins, and no endpoint relies on it for access control.