Most CSRF prevention techniques work by embedding additional authentication data into requests that allows the web application to detect requests from unauthorized locations.
Synchronizer token pattern Synchronizer token pattern (STP) is a technique where a token, a secret and unique value for each request, is embedded by the web application in all HTML forms and verified on the server side. The token may be generated by any method that ensures unpredictability and uniqueness (e.g. using a
hash chain of random seed). This is called an anti-forgery token in
ASP.NET. The attacker is thus unable to place a correct token in their requests to authenticate them. Example of STP set by Django in a HTML form: STP is the most compatible as it only relies on HTML, but introduces some complexity on the server side, due to the burden associated with checking validity of the token on each request. As the token is unique and unpredictable, it also enforces proper sequence of events (e.g. screen 1, then 2, then 3) which raises usability problem (e.g. user opens multiple tabs). It can be relaxed by using per session CSRF token instead of per request CSRF token.
Cookie-to-header token Web applications that use
JavaScript for the majority of their operations may use the following anti-CSRF technique: • On an initial visit without an associated server session, the web application sets a cookie. The cookie typically contains a random token which may remain the same for up to the life of the web session Set-Cookie: __Host-csrf_token=i8XNjC4b8KVok4uw5RftR38Wgp2BFwql; Expires=Thu, 23-Jul-2015 10:25:33 GMT; Max-Age=31449600; Path=/; SameSite=Lax; Secure •
JavaScript operating on the client side reads its value and copies it into a custom
HTTP header sent with each transactional request X-Csrf-Token: i8XNjC4b8KVok4uw5RftR38Wgp2BFwql • The server validates presence and integrity of the token Security of this technique is based on the assumption that only
JavaScript running on the client side of an HTTPS connection to the server that initially set the cookie will be able to read the cookie's value. JavaScript running from a rogue file or email should not be able to successfully read the cookie value to copy into the custom header. Even though the
cookie may be automatically sent with the rogue request, subject to the cookies SameSite policy, the server will still expect a valid
header. The CSRF token itself should be unique and unpredictable. It may be generated randomly, or it may be derived from the
session token using
HMAC: csrf_token = HMAC(session_token, application_secret) The CSRF token cookie must not have
httpOnly flag, as it is intended to be read by
JavaScript by design. This technique is implemented by many modern frameworks, such as
Django and
AngularJS. Because the token remains constant over the whole user session, it works well with
AJAX applications, but does not enforce sequence of events in the web application. The protection provided by this technique can be thwarted if the target website
disables its
same-origin policy using one of the following techniques: • file granting unintended access to Silverlight controls • file granting unintended access to Flash movies
Double Submit Cookie Similarly to the cookie-to-header approach, but without involving JavaScript, a site can set a CSRF token as a cookie, and also insert it as a hidden field in each HTML form. When the form is submitted, the site can check that the cookie token matches the form token. The same-origin policy prevents an attacker from reading or setting cookies on the target domain, so they cannot put a valid token in their crafted form. The advantage of this technique over the Synchronizer pattern is that the token does not need to be stored on the server. However, if the site in question has cookie setting functionality, this protection can be bypassed.
SameSite cookie attribute An additional "SameSite" attribute can be included when the server sets a cookie, instructing the browser on whether to attach the cookie to cross-site requests. If this attribute is set to "strict", then the cookie will only be sent on same-site requests, making CSRF ineffective. However, this requires the browser to recognise and correctly implement the attribute.
Client-side safeguards Browser extensions such as RequestPolicy (for
Mozilla Firefox) or (for both Firefox and
Google Chrome/
Chromium) can prevent CSRF by providing a default-deny policy for cross-site requests. However, this can significantly interfere with the normal operation of many websites. The CsFire extension (also for Firefox) can mitigate the impact of CSRF with less impact on normal browsing, by removing authentication information from cross-site requests. The
NoScript extension for Firefox mitigates CSRF threats by distinguishing trusted from untrusted sites, and removing authentication & payloads from POST requests sent by untrusted sites to trusted ones. The Application Boundary Enforcer module in NoScript also blocks requests sent from internet pages to local sites (e.g. localhost), preventing CSRF attacks on local services (such as uTorrent) or routers. The Self Destructing Cookies extension for Firefox does not directly protect from CSRF, but can reduce the attack window, by deleting cookies as soon as they are no longer associated with an open tab.
Other techniques Various other techniques have been used or proposed for CSRF prevention historically: • Verifying that the request's headers contain X-Requested-With (used by
Ruby on Rails before v2.0 and
Django before v1.2.5), or checking the HTTP Referer header and/or HTTP Origin header. • Checking the
HTTP Referer header to see if the request is coming from an authorized page is commonly used for embedded network devices because it does not increase memory requirements. However, a request that omits the Referer header must be treated as unauthorized because an attacker can suppress the Referer header by issuing requests from FTP or HTTPS URLs. This strict Referer validation may cause issues with browsers or proxies that omit the Referer header for privacy reasons. Also, old versions of Flash (before 9.0.18) allow malicious Flash to generate GET or POST requests with arbitrary HTTP request headers using
CRLF Injection. Similar CRLF injection vulnerabilities in a client can be used to spoof the referrer of an HTTP request. • POST
request method was for a while perceived as immune to trivial CSRF attacks using parameters in URL (using GET method). However, both POST and any other HTTP method can be now easily executed using
XMLHttpRequest. Filtering out unexpected GET requests still prevents some particular attacks, such as cross-site attacks using malicious image URLs or link addresses and cross-site information leakage through <script> elements (
JavaScript hijacking); it also prevents (non-security-related) problems with aggressive
web crawlers and
link prefetching. ==See also==