StoicBuilder

HandbookXIV the VallumCross-site request forgery

XIVthe Vallum, Defend it

Cross-site request forgery

1 min readUpdated 28 November

In plain words

Cross-site request forgery happens when a page on another site makes a visitor's browser send a request to your app with their cookies attached, so it acts as them without their knowing.

Where you see it

Harbor signs users in with a session cookie, and an old route changes a project's billing email from a plain form POST. If that cookie were sent on cross-site requests, any website a signed-in customer visited could submit that form in the background.

middleware.tsserver
// session cookie: Set-Cookie: hb_session=...; HttpOnly; Secure; SameSite=Lax // every route that changes data: refuse requests started by other sitesconst site = req.headers.get("sec-fetch-site");if (site && site !== "same-origin") { return new Response("Forbidden", { status: 403 });}
SameSite=Lax keeps the cookie off cross-site POSTs, and the Fetch Metadata header lets the server refuse requests another site started.

What goes wrong

  1. Changing data with a GET

    SameSite=Lax still sends cookies when a visitor follows a link from another site, so a GET route like /projects/42/delete can be triggered by a link. Anything that changes data should use POST, PUT, PATCH or DELETE.

  2. SameSite=None on the session cookie

    Setting SameSite=None to make an embed or a cross-site flow work sends the session along with every cross-site request. Keep the session cookie Lax or Strict, and give the cross-site case its own token.

  3. Counting on CORS to stop it

    CORS decides whether another site may read your response, not whether the browser sends the request. A plain form POST still arrives, so check the origin or a CSRF token on the server.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Audit every route that changes data and relies on cookie authentication. Make sure none use GET, the session cookie is HttpOnly, Secure and SameSite=Lax or Strict, and each route checks the Origin or Sec-Fetch-Site header or a CSRF token. List any route that relies on CORS alone.

Quick check

Sign in to earn +10 XP for it

Harbor's session cookie is set with SameSite=None, and its API has strict CORS rules. A malicious page submits a hidden form that POSTs to Harbor's change-email route. What happens if there's no other defence?

Got a prompt that touches Cross-site request forgery?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.