CSRF on JSON APIs
A JSON API is not immune to cross-site request forgery if the browser will attach the session cookie and the server changes state without a token.
Diagram
From another site to a cookie-authenticated change
Step 1 of 4
Cookie
The person is signed in. The browser will attach the cookie to requests at your host, according to the cookie’s rules.
Cookie: session=<avery> Content-Type: application/json # A JSON body does not, by itself, prove the person asked.
A state change carries only the session cookie.
The handler writes.
It could cause that send.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
JSON is not a boundary
- What fails
- A content type does not prove the person’s own page made the request. The cookie still rides if the browser sends it.
- Who is affected
- The signed-in person whose settings, payment, or data would change.
- What you would notice
- A POST or PUT that succeeds with only the session cookie.
- What you change
- Require a token header that another origin cannot read.
What SameSite actually covers
- What fails
- Lax and Strict stop some cross-site sends. They are not a substitute for a token, especially where a top-level navigation is allowed or a client does not implement the attribute.
- Who is affected
- The same signed-in person, on browsers and embedded clients that still attach the cookie.
- What you would notice
- A cookie with no SameSite attribute, and no token check in the handler.
- What you change
- Set SameSite, and still check a token on every state change.
Safe methods stay safe
- What fails
- A GET that changes state is forgeable by a link or an image. The method has to mean what HTTP says it means.
- Who is affected
- Anyone who can be induced to open a link while signed in.
- What you would notice
- A handler that writes on GET or HEAD.
- What you change
- Move the change to POST or PUT and require the token there.
The token’s rules
- What fails
- The token has to be tied to the session, unreadable to another origin, and checked with a constant-time compare.
- Who is affected
- The person whose session would be used if the token were global or missing.
- What you would notice
- A token in a URL, a token shared by every user, or a check that only the front end performs.
- What you change
- Issue the token in the session, send it as a header from your own pages, and reject the request on the server when it is absent.
SameSite is a reduction, not a token
SameSite=Lax or Strict tells modern browsers not to attach the cookie on some cross-site requests. That is worth setting. It is not the authorization check. Top-level navigations, older clients, and cookies that were set without the attribute still show up in incident write-ups. Design the handler so a cookie alone is not enough.
GET, HEAD, and OPTIONS must not change state. A link, a prefetch, or an image is allowed to cause those methods. If a GET handler writes, you have made every link a state change. Move the write to a method that is not safe, and require the token there.
What the token has to be
Issue a token when the session starts, store it server-side with the session, and give it to your own pages. Send it back in a header on state-changing calls. Compare it in constant time. Reject missing, empty, and wrong values the same way. Do not put the token in the URL, where it leaks into logs and referrers.
A double-submit cookie can work if you understand the rules, but a token bound to the server-side session is easier to explain. The front end hiding a button is not the control. Another site does not click your button. It causes a request.
What you would notice and change
In review, list every route that writes, pays, invites, or changes an email. For each one, point at the token check. Routes that only mention the session middleware are the finding. Also check that the cookie is Secure, HttpOnly, and SameSite.
Add a test that a state change without the header is rejected, and that a token from a different session is rejected. The people affected are whoever is signed in when their browser sends the request: their email, their payment method, their team membership.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A JSON PATCH changes the account email. It accepts the session cookie and does not look for any other secret. The content type is application/json. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.