skip to content

XSRF Tokens & XSSI Prefix

HttpClient copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header on mutating same-origin requests and strips the )]}', prefix from JSON. Interviewers check you know the server's half.

part ofAngularoverview, primer and where to startread it →
on this pageshow

explore

questions

4

In an Angular app, how do you change the XSRF cookie and header names HttpClient uses, or turn its XSRF handling off, and when is each justified?

level: juniorimportance: should knowfreq 30%

basics

~20 s

Pass withXsrfConfiguration({cookieName, headerName}) to provideHttpClient to match your backend's names, or withNoXsrfProtection() to remove the interceptor. Rename when the backend or a shared domain needs it; disable only when another defence, such as header-sent bearer tokens, makes it unnecessary.

open as a page

After an Angular app's services switch to calling https://api.example.com directly, every POST fails the backend's CSRF check with a 403; why did HttpClient stop sending X-XSRF-TOKEN, and how do you fix it?

level: seniorimportance: should knowfreq 38%

basics

~20 s

HttpClient adds the XSRF header only for URLs on the page's own origin; api.example.com is cross-origin, so it is skipped to avoid leaking the token. Serve the API from the app's origin, or add the header for that one trusted origin.

open as a page

What is the )]}', prefix some servers put in front of JSON responses, and what does Angular's HttpClient do with it?

level: middleimportance: nice to knowfreq 22%

basics

~20 s

It is an XSSI guard: prefixing JSON with )]}' and a newline makes the response a syntax error when a hostile page loads it as a script. HttpClient strips it before parsing JSON, so application code never sees it.

open as a page