Quick revision
The short answer
Browser apps that authenticate with cookies always keep CSRF protection, preferably through a backend-for-frontend whose session cookie is Secure, HttpOnly and SameSite=Lax or Strict. APIs that accept only bearer tokens in the Authorization header may disable CSRF for those filter chains, and no endpoint may accept both cookies and bearer tokens without CSRF checks. Cookie standards cover Secure, HttpOnly, SameSite and narrow domain and path, and the whole policy ships in the platform security starter with shared integration tests.
What to remember
In this answer, we built a CSRF and cookie policy around one fact: CSRF needs a credential the browser sends automatically. Cookie-authenticated browser apps, ideally through a BFF, keep CSRF on with Secure, HttpOnly, SameSite cookies of narrow scope. Bearer-token APIs for mobile apps and partners disable CSRF on stateless chains that ignore cookies, and no endpoint accepts both without CSRF. The platform starter delivers the two chain types and shared tests enforce them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Browser apps that authenticate with cookies always keep CSRF protection, preferably through a backend-for-frontend whose session cookie is Secure, HttpOnly and SameSite=Lax or Strict. APIs that accept only bearer tokens in the Authorization header may disable CSRF for those filter chains, and no endpoint may accept both cookies and bearer tokens without CSRF checks. Cookie standards cover Secure, HttpOnly, SameSite and narrow domain and path, and the whole policy ships in the platform security starter with shared integration tests.
Why this question is asked
When one backend serves browsers, mobile apps and partners, CSRF decisions tend to be made endpoint by endpoint, by whoever hit a 403 last. The result is a mix of chains with protection, chains without, and a few that accept any kind of credential, which is where attacks happen. Interviewers ask this design question to see whether we can reduce a subtle topic to rules simple enough that teams follow them without becoming CSRF experts. In this answer, we will recap the one fact the whole policy rests on, set rules for each client type, close the mixed-credential gap, define cookie standards, and deliver the policy through the starter with tests.
1The One Fact the Policy Rests On
CSRF is possible whenever the browser attaches a credential automatically. Cookies are attached automatically to requests for their site, even when another site starts the request; so are HTTP Basic credentials the browser remembers. A bearer token in the Authorization header is not, because it is added by our own code, not by the browser.
So the policy does not depend on whether a client is a browser, an app or a partner. It depends on how each request is authenticated. Cookie-authenticated requests need CSRF protection; header-token requests do not. Every rule below follows from that.
2Rules per Client Type
Browser apps authenticate with cookies. The preferred pattern is a backend-for-frontend (BFF): a server-side component that performs the OAuth2 login, keeps the tokens on the server, and gives the browser a session cookie. That chain keeps Spring Security's CSRF protection on, using the SPA handler if the frontend is a single-page app, and the session cookie uses SameSite=Lax, or Strict where the app does not need cross-site links to arrive logged in.
Mobile apps and partner integrations authenticate with bearer tokens obtained through OAuth2 and sent in the Authorization header. Their API chains are stateless, never create or read sessions, and may disable CSRF. Partners never get cookie-based access.
| Client | Credential | CSRF | Cookie rules |
|---|---|---|---|
| Browser app (BFF) | Session cookie | On (SPA handler if SPA) | Secure, HttpOnly, SameSite=Lax |
| Server-rendered web | Session cookie | On (default) | Secure, HttpOnly, SameSite=Lax |
| Mobile app | Bearer token in header | Off for API chain | No cookies |
| Partner integration | Bearer token (client credentials) | Off for API chain | No cookies |
| Any endpoint taking both | Cookie or bearer | On | Not allowed without CSRF |
3Never Mix Cookies and Bearer Tokens Without CSRF
The dangerous case is an endpoint that accepts a bearer token from mobile apps and also a session cookie from the browser app, with CSRF disabled because "it is an API". An attacker's page cannot add the bearer token, but it does not need to: it sends a cross-site request and the browser adds the cookie. The endpoint authenticates the cookie and the CSRF check that would have stopped it is off.
The rule is therefore strict: a filter chain that disables CSRF must not accept any cookie-based authentication. In Spring Security that means the API chain is matched by path, uses oauth2ResourceServer, sets sessions to STATELESS, and has no formLogin, no session-based authentication and no remember-me. The browser app calls its BFF, and the BFF calls the APIs with bearer tokens.
4Cookie Standards
Every authentication cookie is Secure, so it is only sent over HTTPS, and HttpOnly, so JavaScript cannot read it and XSS cannot steal it. It has an explicit SameSite value, Lax by default and Strict where cross-site entry is not needed. SameSite=None is allowed only with a documented reason, such as an embedded third-party flow, and always with CSRF protection.
Scope is kept narrow. A cookie set for the parent domain, such as .example.com, is sent to every subdomain, including marketing sites and test environments that may be less well protected; a cookie without a Domain attribute is sent only to the host that set it. Path is narrowed where an application lives under a prefix. The __Host- name prefix is a good default for session cookies, because browsers then insist on Secure, no Domain and Path=/. The CSRF token cookie for SPAs is the one deliberate exception to HttpOnly, because the SPA must read it; it is not a credential.
5Delivering It Through the Starter
Teams should not have to write any of this. The platform security starter provides two ready-made chain configurations, a browser chain and an API chain, with the CSRF, session and cookie settings above. A team declares which paths are which and adds its authorization rules. Disabling CSRF on a browser chain or enabling sessions on an API chain requires an explicit, reviewed exception.
Shared integration tests come with the starter and run in every service's build: a cross-site POST with only a session cookie must fail; a browser POST with a valid CSRF token must succeed; an API request with only a cookie must get 401; and authentication cookies must carry Secure, HttpOnly and SameSite. These tests turn the policy into something the build enforces.
- Path is an API chain?Matched by /api/**YesBearer token requiredNoBrowser chain
- Bearer token valid?Cookies ignored hereYesServe (no CSRF needed)No401
- Browser chain: CSRF token valid?State-changing methodsYesServeNo403
3 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why prefer a BFF over a SPA holding tokens?
Because tokens in the browser can be stolen by XSS and need careful refresh logic. A BFF keeps tokens on the server, and the browser only holds an HttpOnly cookie. The cost is CSRF protection, which the starter already provides.
Is SameSite=Strict enough to skip the CSRF token?
No. Sibling subdomains count as the same site, older clients and embedded web views behave differently, and policies get relaxed later for a feature. The CSRF token stays as the main control and SameSite is the second layer.
What about webhooks from partners?
They are server-to-server calls without cookies, so CSRF does not apply. They are authenticated with a signature or a bearer token, on an API chain.
7Conclusion
In this answer, we built a CSRF and cookie policy around one fact: CSRF needs a credential the browser sends automatically. Cookie-authenticated browser apps, ideally through a BFF, keep CSRF on with Secure, HttpOnly, SameSite cookies of narrow scope. Bearer-token APIs for mobile apps and partners disable CSRF on stateless chains that ignore cookies, and no endpoint accepts both without CSRF. The platform starter delivers the two chain types and shared tests enforce them.
Open with
I would base the whole policy on one fact: CSRF is only possible when the browser attaches a credential automatically, which means cookies, not bearer tokens in the Authorization header.
Then, if they want more
So the rule is about how each request is authenticated. Browser apps use cookies, and I would prefer a backend-for-frontend that does the OAuth login, keeps the tokens on the server and gives the browser a session cookie. Those chains always keep CSRF protection on, with the SPA handler if it is a single-page app.
Mobile apps and partners use bearer tokens in the header on stateless API chains, and those chains may disable CSRF. The one thing I would forbid is an endpoint that accepts both a cookie and a bearer token with CSRF off, because an attacker's page can just ride on the cookie.
For cookies, the standard is Secure, HttpOnly, an explicit SameSite, Lax by default, no parent-domain scope unless justified, and ideally the __Host- prefix; the SPA's CSRF token cookie is the only one JavaScript may read.
To make it simple for teams, the platform starter ships a browser chain and an API chain with these settings, so teams only map paths and add authorization rules. Shared integration tests in every build check that a cross-site cookie-only POST fails, that an API ignores cookies, and that cookies carry the right attributes.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- OWASP Top Ten
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your company has browser apps, mobile apps and partner integrations calling the same...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/1b79c17719/