Safeguard
Security Guides•

Does Rails API mode need CSRF protection?

Token APIs without cookies usually skip CSRF tokens, but cookie sessions change that. How to decide for a Rails API and what a scan checks.

Safeguard Security Team
3 min read

A Rails API that authenticates with a bearer token in a header and sets no cookies generally does not need CSRF tokens, because the forged request has no credential to ride on. If you add cookie sessions, you need protection again. This is reasoning from how the attack works, so confirm it for your design.

How the attack works

The Rails security guide describes cross site request forgery this way: a page on another site can cause a victim's browser to send a request carrying the victim's session cookie, so the target app may act on it as the victim. The browser attaches the cookie automatically. That automatic attachment is the whole attack.

Rails guards against it with a token placed in forms and checked on non GET requests. The guide says new applications enable protect_from_forgery by default.

Where API mode fits

API mode leaves out cookie and session middleware by default, per the Rails API guide. With no cookie to attach, there is nothing for a forged page to piggyback on. Header based credentials, such as a bearer token, are not attached automatically by browsers, which is why token APIs are commonly exempt. The Spring Security documentation makes a similar point: a backend that does not serve browser traffic may disable CSRF protection.

Decision table

Your APICSRF tokens needed?
Token in header, no cookies, no browser sessionsGenerally no
Cookie session set by the same appYes
Mixed: browser app and token clientsYes for the browser paths
Cookies added back to API mode for convenienceYes, treat as a full app

Steps to check your own app

  1. Search the code for cookie and session usage. Look in config for added middleware.
  2. Check how clients authenticate. If any browser flow relies on a cookie, you are in the cookie row.
  3. If cookies exist, enable request forgery protection for those controllers and send the token in the X-CSRF-Token header from your JavaScript, as the guide describes for Turbo and custom requests.
  4. Remember the guide's warning that cross site scripting bypasses CSRF protection, since injected script can read the token.
  5. Run a scan that sends state changing requests without the token and see what is accepted.

Mistakes that cause real findings

  • Turning protection off globally to fix a test, then forgetting it.
  • Persisting login in a long lived cookie while calling the app an API.
  • Allowing wide CORS together with credentials, which undermines the browser's own protections. Our post on CORS credentials and wildcard rules explains the browser rule.

Try it yourself

Click through it at your own pace, or open it full screen. Look for how the scan treats authenticated, state changing requests.

Safeguard's dependency scanning also flags vulnerable gems in the stack, and a free scan is available.

Quick check

Is CSRF a risk for JWT in the Authorization header?

Usually not, since browsers do not add that header on their own. Storing the token in a cookie changes the answer.

Does Rails enable CSRF protection in API mode?

API mode drops cookie handling by default, so the classic cookie based flow is absent. Verify your own configuration.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.